-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b250633
commit c8e3256
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-core/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...porary_superset_ui/superset-ui/packages/superset-ui-core/src/models/ExtensibleFunction.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* From https://stackoverflow.com/questions/36871299/how-to-extend-function-with-es6-classes | ||
*/ | ||
|
||
export default class ExtensibleFunction extends Function { | ||
constructor(fn) { | ||
return Object.setPrototypeOf(fn, new.target.prototype); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
..._superset_ui/superset-ui/packages/superset-ui-core/test/models/ExtensibleFunction.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import ExtensibleFunction from '../../src/models/ExtensibleFunction'; | ||
|
||
describe('ExtensibleFunction', () => { | ||
class Func1 extends ExtensibleFunction { | ||
constructor(x) { | ||
super(() => x); // closure | ||
} | ||
} | ||
class Func2 extends ExtensibleFunction { | ||
constructor(x) { | ||
super(() => this.x); // arrow function, refer to its own field | ||
this.x = x; | ||
} | ||
|
||
hi() { | ||
return 'hi'; | ||
} | ||
} | ||
class Func3 extends ExtensibleFunction { | ||
constructor(x) { | ||
super(function customName() { | ||
return customName.x; | ||
}); // named function | ||
this.x = x; | ||
} | ||
} | ||
|
||
it('its subclass is an instance of Function', () => { | ||
expect(Func1).toBeInstanceOf(Function); | ||
expect(Func2).toBeInstanceOf(Function); | ||
expect(Func3).toBeInstanceOf(Function); | ||
}); | ||
|
||
const func1 = new Func1(100); | ||
const func2 = new Func2(100); | ||
const func3 = new Func3(100); | ||
|
||
it('an instance of its subclass is executable like regular function', () => { | ||
expect(func1()).toEqual(100); | ||
expect(func2()).toEqual(100); | ||
expect(func3()).toEqual(100); | ||
}); | ||
|
||
it('its subclass behaves like regular class with its own fields and functions', () => { | ||
expect(func2.x).toEqual(100); | ||
expect(func2.hi()).toEqual('hi'); | ||
}); | ||
|
||
it('its subclass can set name by passing named function to its constructor', () => { | ||
expect(func3.name).toEqual('customName'); | ||
}); | ||
}); |