Skip to content

Commit

Permalink
update(add): 検証内容の追加
Browse files Browse the repository at this point in the history
  • Loading branch information
Himenon committed Oct 13, 2018
1 parent 9c2f4fa commit 4452fbc
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
20 changes: 20 additions & 0 deletions src/js-function/__tests__/onestho.test.ts
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);
});
7 changes: 7 additions & 0 deletions src/js-function/oneshot.ts
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;');
};
6 changes: 6 additions & 0 deletions src/react-function/__tests__/oneshot.test.tsx
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);
});
28 changes: 11 additions & 17 deletions src/react-function/oneshot.tsx
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}`);
}
2 changes: 1 addition & 1 deletion src/styled-components/oneshot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const createExample = () => {
body {
font-family: sans-serif;
}
`;
]`;

const Title = styled.h1`
font-size: 1.5em;
Expand Down

0 comments on commit 4452fbc

Please sign in to comment.