-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
5 changed files
with
45 additions
and
18 deletions.
There are no files selected for viewing
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,20 @@ | ||
import * as OneShot from '../oneshot'; | ||
|
||
test('sum function', () => { | ||
expect(OneShot.sum(1, 2)).toEqual(3); | ||
expect(OneShot.sum(2, 2)).toEqual(4); | ||
expect(OneShot.sum(-1, -6)).toEqual(-7); | ||
expect(OneShot.sum(-5, 5)).toEqual(0); | ||
}); | ||
|
||
test('引数を1つだけ受け取る関数', () => { | ||
// @ts-ignore | ||
global.x = 10; | ||
expect(OneShot.argFunction(0)).toEqual(10); | ||
}); | ||
|
||
test('globalな値をそのまま返す関数', () => { | ||
// @ts-ignore | ||
global.y = 10; | ||
expect(OneShot.simpleReturnFunction()()).toEqual(10); | ||
}); |
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,7 @@ | ||
export const sum = new Function('a', 'b', 'return a + b;'); | ||
|
||
export const argFunction = new Function('a', 'return a + x;'); | ||
|
||
export const simpleReturnFunction = () => { | ||
return new Function('return y;'); | ||
}; |
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,6 @@ | ||
import * as OneShot from '../oneshot'; | ||
|
||
test.skip('StringからFunctionを生成する', () => { | ||
const maker = OneShot.createComponent(); | ||
expect(maker()).toEqual(OneShot.MyComponent); | ||
}); |
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 |
---|---|---|
@@ -1,21 +1,15 @@ | ||
// @ts-ignore | ||
import * as transformJSX from '@babel/plugin-transform-react-jsx'; | ||
// @ts-ignore | ||
import * as babel from '@babel/standalone'; | ||
import * as React from 'react'; | ||
|
||
const parse = (raw: string): string | null => | ||
babel.transform(raw, { | ||
plugins: [transformJSX], | ||
}).code; | ||
export interface MyComponentProps { | ||
text: string; | ||
} | ||
|
||
const wrap = (jsx: string) => `<React.Fragment>${jsx}</React.Fragment>`; | ||
export class MyComponent extends React.Component<MyComponentProps, {}> { | ||
public render() { | ||
return <div>${this.props.text}</div>; | ||
} | ||
} | ||
|
||
export const toComponent = (jsx: string) => { | ||
const el = parse(wrap(jsx)); | ||
const keys = Object.keys(scope); | ||
const values = keys.map((key) => scope[key]); | ||
const createFunction = new Function('React', ...keys, `return props => ${el}`); | ||
const Comp = createFunction(React, ...values); | ||
return Comp; | ||
}; | ||
export function createComponent() { | ||
return new Function('React', 'MyComponent', `return props => ${MyComponent}`); | ||
} |
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