Skip to content

Commit

Permalink
Merge branch 'master' of github.com:storybooks/telejson
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelangen committed Nov 21, 2019
2 parents 8317c7b + d939abe commit 208848e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 71 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ Only relevant when using `stringify`.

### reviver

Doesn't take any options right now.
`lazyEval`: When set to false, lazy eval will be disabled. (default true)

Note: disabling lazy eval will affect performance. Consider disabling it only if you truly need to.

## Contributing

Expand Down
37 changes: 33 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ const cleanCode = memoize(10000)(code =>
.trim()
);

const convertShorthandMethods = function(key: string, stringified: string) {
const fnHead = stringified.slice(0, stringified.indexOf('{'));
const fnBody = stringified.slice(stringified.indexOf('{'));

if (fnHead.includes('=>')) {
// This is an arrow function
return stringified;
}

if (fnHead.includes('function')) {
// This is an anonymous function
return stringified;
}

let modifiedHead = fnHead;

modifiedHead = modifiedHead.replace(key, 'function');

return modifiedHead + fnBody;
};

const dateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/;

interface Options {
Expand All @@ -73,6 +94,7 @@ interface Options {
allowClass: boolean;
maxDepth: number;
space: number | undefined;
lazyEval: boolean;
}

export const replacer = function replacer(options: Options) {
Expand Down Expand Up @@ -115,7 +137,7 @@ export const replacer = function replacer(options: Options) {
/(\[native code\]|WEBPACK_IMPORTED_MODULE|__webpack_exports__|__webpack_require__)/
)
) {
return `_function_${name}|${cleanCode(stringified)}`;
return `_function_${name}|${cleanCode(convertShorthandMethods(key, stringified))}`;
}
return `_function_${name}|${(() => {}).toString()}`;
}
Expand Down Expand Up @@ -208,7 +230,7 @@ interface ValueContainer {
[keys: string]: any;
}

export const reviver = function reviver() {
export const reviver = function reviver(options: Options) {
const refs: { target: string; container: { [keys: string]: any }; replacement: string }[] = [];
let root: any;

Expand Down Expand Up @@ -249,6 +271,11 @@ export const reviver = function reviver() {
if (typeof value === 'string' && value.startsWith('_function_')) {
const [, name, source] = value.match(/_function_([^|]*)\|(.*)/) || [];

if (!options.lazyEval) {
// eslint-disable-next-line no-eval
return eval(`(${source})`);
}

// lazy eval of the function
const result = (...args: any[]) => {
// eslint-disable-next-line no-eval
Expand Down Expand Up @@ -311,6 +338,7 @@ const defaultOptions: Options = {
allowClass: true,
allowUndefined: true,
allowSymbol: true,
lazyEval: true,
};

export const stringify = (data: any, options: Partial<Options> = {}) => {
Expand Down Expand Up @@ -343,8 +371,9 @@ const mutator = () => {
};
};

export const parse = (data: string) => {
const result = JSON.parse(data, reviver());
export const parse = (data: string, options: Partial<Options> = {}) => {
const mergedOptions: Options = Object.assign({}, defaultOptions, options);
const result = JSON.parse(data, reviver(mergedOptions));

mutator()(result);

Expand Down
8 changes: 6 additions & 2 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Object {
"fn1": [Function],
"fn2": [Function],
"fn3": [Function],
"fn4": [Function],
"foo": Foo {},
"nested": Object {
"a": Object {
Expand Down Expand Up @@ -43,6 +44,7 @@ exports[`Dist space 1`] = `
\\"fn1\\": \\"_function_fn1|function fn1(x) {return x + x;}\\",
\\"fn2\\": \\"_function_x|function x(x) {return x - x;}\\",
\\"fn3\\": \\"_function_fn3|function fn3() {return x / x;}\\",
\\"fn4\\": \\"_function_fn4|function fn4(x) {return x * x;}\\",
\\"date\\": \\"_date_2018-01-01T00:00:00.000Z\\",
\\"foo\\": {
\\"_constructor-name_\\": \\"Foo\\"
Expand Down Expand Up @@ -72,7 +74,7 @@ exports[`Dist space 1`] = `
}"
`;

exports[`Dist stringify 1`] = `"{\\"regex1\\":\\"_regexp_|foo\\",\\"regex2\\":\\"_regexp_g|foo\\",\\"regex3\\":\\"_regexp_i|foo\\",\\"fn1\\":\\"_function_fn1|function fn1(x) {return x + x;}\\",\\"fn2\\":\\"_function_x|function x(x) {return x - x;}\\",\\"fn3\\":\\"_function_fn3|function fn3() {return x / x;}\\",\\"date\\":\\"_date_2018-01-01T00:00:00.000Z\\",\\"foo\\":{\\"_constructor-name_\\":\\"Foo\\"},\\"nested\\":{\\"a\\":{\\"b\\":{\\"c\\":{\\"d\\":{\\"e\\":{\\"f\\":{\\"g\\":{\\"h\\":{\\"i\\":{\\"j\\":\\"[Object]\\"}}}}}}}}}},\\"cyclic\\":\\"_duplicate_root\\"}"`;
exports[`Dist stringify 1`] = `"{\\"regex1\\":\\"_regexp_|foo\\",\\"regex2\\":\\"_regexp_g|foo\\",\\"regex3\\":\\"_regexp_i|foo\\",\\"fn1\\":\\"_function_fn1|function fn1(x) {return x + x;}\\",\\"fn2\\":\\"_function_x|function x(x) {return x - x;}\\",\\"fn3\\":\\"_function_fn3|function fn3() {return x / x;}\\",\\"fn4\\":\\"_function_fn4|function fn4(x) {return x * x;}\\",\\"date\\":\\"_date_2018-01-01T00:00:00.000Z\\",\\"foo\\":{\\"_constructor-name_\\":\\"Foo\\"},\\"nested\\":{\\"a\\":{\\"b\\":{\\"c\\":{\\"d\\":{\\"e\\":{\\"f\\":{\\"g\\":{\\"h\\":{\\"i\\":{\\"j\\":\\"[Object]\\"}}}}}}}}}},\\"cyclic\\":\\"_duplicate_root\\"}"`;

exports[`Source parse 1`] = `
Object {
Expand All @@ -81,6 +83,7 @@ Object {
"fn1": [Function],
"fn2": [Function],
"fn3": [Function],
"fn4": [Function],
"foo": Foo {},
"nested": Object {
"a": Object {
Expand Down Expand Up @@ -117,6 +120,7 @@ exports[`Source space 1`] = `
\\"fn1\\": \\"_function_fn1|function fn1(x) {return x + x;}\\",
\\"fn2\\": \\"_function_x|function x(x) {return x - x;}\\",
\\"fn3\\": \\"_function_fn3|function fn3() {return x / x;}\\",
\\"fn4\\": \\"_function_fn4|function fn4(x) {return x * x;}\\",
\\"date\\": \\"_date_2018-01-01T00:00:00.000Z\\",
\\"foo\\": {
\\"_constructor-name_\\": \\"Foo\\"
Expand Down Expand Up @@ -146,4 +150,4 @@ exports[`Source space 1`] = `
}"
`;

exports[`Source stringify 1`] = `"{\\"regex1\\":\\"_regexp_|foo\\",\\"regex2\\":\\"_regexp_g|foo\\",\\"regex3\\":\\"_regexp_i|foo\\",\\"fn1\\":\\"_function_fn1|function fn1(x) {return x + x;}\\",\\"fn2\\":\\"_function_x|function x(x) {return x - x;}\\",\\"fn3\\":\\"_function_fn3|function fn3() {return x / x;}\\",\\"date\\":\\"_date_2018-01-01T00:00:00.000Z\\",\\"foo\\":{\\"_constructor-name_\\":\\"Foo\\"},\\"nested\\":{\\"a\\":{\\"b\\":{\\"c\\":{\\"d\\":{\\"e\\":{\\"f\\":{\\"g\\":{\\"h\\":{\\"i\\":{\\"j\\":\\"[Object]\\"}}}}}}}}}},\\"cyclic\\":\\"_duplicate_root\\"}"`;
exports[`Source stringify 1`] = `"{\\"regex1\\":\\"_regexp_|foo\\",\\"regex2\\":\\"_regexp_g|foo\\",\\"regex3\\":\\"_regexp_i|foo\\",\\"fn1\\":\\"_function_fn1|function fn1(x) {return x + x;}\\",\\"fn2\\":\\"_function_x|function x(x) {return x - x;}\\",\\"fn3\\":\\"_function_fn3|function fn3() {return x / x;}\\",\\"fn4\\":\\"_function_fn4|function fn4(x) {return x * x;}\\",\\"date\\":\\"_date_2018-01-01T00:00:00.000Z\\",\\"foo\\":{\\"_constructor-name_\\":\\"Foo\\"},\\"nested\\":{\\"a\\":{\\"b\\":{\\"c\\":{\\"d\\":{\\"e\\":{\\"f\\":{\\"g\\":{\\"h\\":{\\"i\\":{\\"j\\":\\"[Object]\\"}}}}}}}}}},\\"cyclic\\":\\"_duplicate_root\\"}"`;
Loading

0 comments on commit 208848e

Please sign in to comment.