-
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.
Merge pull request #7 from Himenon/dev
updated(code): memorizeとbabel-standaloneのコードを追加
- Loading branch information
Showing
9 changed files
with
230 additions
and
7 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 @@ | ||
--install.ignore-engines true |
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
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
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,26 @@ | ||
import * as React from 'react'; | ||
import * as renderer from 'react-test-renderer'; | ||
import * as OneShot from "../oneshot"; | ||
|
||
|
||
test("parse", () => { | ||
const codeString = OneShot.parse(` | ||
function hello() { | ||
return "Hello World"; | ||
} | ||
`); | ||
expect(codeString).not.toBeNull(); | ||
const hello = new Function(`return ${codeString}`)(); | ||
expect(hello()).toEqual("Hello World"); | ||
}) | ||
|
||
test("parse string that contain JSX", () => { | ||
const codeString = OneShot.parseWithJSX(`<div>Hello World</div>`); | ||
expect(codeString).toEqual('React.createElement("div", null, "Hello World");'); | ||
const componentCreator = new Function('React', `return ${codeString}`); | ||
|
||
const component = componentCreator(React); | ||
const testComponent = renderer.create(component); | ||
const expectComponent = renderer.create(<div>Hello World</div>); | ||
expect(testComponent.toJSON()).toEqual(expectComponent.toJSON()); | ||
}); |
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,14 @@ | ||
// @ts-ignore | ||
import * as standalone from "@babel/standalone"; | ||
// @ts-ignore | ||
import * as transformJSX from '@babel/plugin-transform-react-jsx' | ||
|
||
export const parse = (raw: string): string | null => { | ||
return standalone.transform(raw, {}).code; | ||
} | ||
|
||
export const parseWithJSX = (raw: string): string | null => { | ||
return standalone.transform(raw, { | ||
plugins: [transformJSX], | ||
}).code; | ||
} |
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
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,10 @@ | ||
import { memorize } from "../memorize"; | ||
|
||
test("memorize", () => { | ||
function add(a: number, b: number) { | ||
return a + b; | ||
} | ||
const customAdd = memorize(add); | ||
expect(customAdd(1, 2)).toEqual(3); | ||
expect(customAdd(1, 2)).toEqual(3); | ||
}); |
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,12 @@ | ||
export function memorize<T extends Function>(f: T): any { | ||
const cache: { [key: string]: T } = {} | ||
return function fn() { | ||
const key = arguments.length + Array.prototype.join.call(arguments, ","); | ||
if (key in cache) { | ||
return cache[key] | ||
} else { | ||
// @ts-ignore | ||
return cache[key] = f.apply(this, arguments) | ||
}; | ||
} | ||
} |
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