Socket
Socket
Sign inDemoInstall

telejson

Package Overview
Dependencies
Maintainers
2
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

telejson - npm Package Compare versions

Comparing version 3.1.0 to 3.2.0

5

dist/index.d.ts

@@ -10,2 +10,3 @@ interface Options {

space: number | undefined;
lazyEval: boolean;
}

@@ -17,6 +18,6 @@ export declare const replacer: (options: Options) => (this: any, key: string, value: any) => any;

}
export declare const reviver: () => (this: any, key: string, value: string | ValueContainer) => string | number | symbol | RegExp | Date | ValueContainer | ((...args: any[]) => any) | null;
export declare const reviver: (options: Options) => (this: any, key: string, value: string | ValueContainer) => any;
export declare const isJSON: (input: string) => RegExpMatchArray | null;
export declare const stringify: (data: any, options?: Partial<Options>) => string;
export declare const parse: (data: string) => any;
export declare const parse: (data: string, options?: Partial<Options>) => any;
export {};

48

dist/index.js

@@ -22,2 +22,8 @@ "use strict";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

@@ -85,2 +91,22 @@

});
var convertShorthandMethods = function convertShorthandMethods(key, stringified) {
var fnHead = stringified.slice(0, stringified.indexOf('{'));
var 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;
}
var modifiedHead = fnHead;
modifiedHead = modifiedHead.replace(key, 'function');
return modifiedHead + fnBody;
};
var dateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/;

@@ -128,3 +154,3 @@

if (!stringified.match(/(\[native code\]|WEBPACK_IMPORTED_MODULE|__webpack_exports__|__webpack_require__)/)) {
return "_function_".concat(name, "|").concat(cleanCode(stringified));
return "_function_".concat(name, "|").concat(cleanCode(convertShorthandMethods(key, stringified)));
}

@@ -227,3 +253,3 @@

var reviver = function reviver() {
var reviver = function reviver(options) {
var refs = [];

@@ -274,5 +300,10 @@ var root;

_name = _ref3[1],
source = _ref3[2]; // lazy eval of the function
source = _ref3[2];
if (!options.lazyEval) {
// eslint-disable-next-line no-eval
return eval("(".concat(source, ")"));
} // lazy eval of the function
var result = function result() {

@@ -354,3 +385,4 @@ // eslint-disable-next-line no-eval

allowUndefined: true,
allowSymbol: true
allowSymbol: true,
lazyEval: true
};

@@ -360,3 +392,5 @@

var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var mergedOptions = Object.assign({}, defaultOptions, options);
var mergedOptions = _objectSpread({}, defaultOptions, {}, options);
return JSON.stringify(data, replacer(mergedOptions), options.space);

@@ -398,3 +432,5 @@ };

var parse = function parse(data) {
var result = JSON.parse(data, reviver());
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var mergedOptions = Object.assign({}, defaultOptions, options);
var result = JSON.parse(data, reviver(mergedOptions));
mutator()(result);

@@ -401,0 +437,0 @@ return result;

{
"name": "telejson",
"version": "3.1.0",
"version": "3.2.0",
"description": "",

@@ -45,3 +45,3 @@ "keywords": [

"is-regex": "^1.0.4",
"is-symbol": "^1.0.2",
"is-symbol": "^1.0.3",
"isobject": "^4.0.0",

@@ -52,17 +52,17 @@ "lodash": "^4.17.15",

"devDependencies": {
"@babel/cli": "^7.6.2",
"@babel/core": "^7.6.2",
"@babel/preset-env": "^7.6.2",
"@babel/preset-typescript": "^7.6.0",
"@storybook/eslint-config-storybook": "^1.0.0",
"@types/jest": "^24.0.18",
"@types/lodash": "^4.14.141",
"@babel/cli": "^7.7.0",
"@babel/core": "^7.7.2",
"@babel/preset-env": "^7.7.1",
"@babel/preset-typescript": "^7.7.2",
"@storybook/eslint-config-storybook": "^2.1.0",
"@types/jest": "^24.0.23",
"@types/lodash": "^4.14.149",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "^24.9.0",
"common-tags": "^1.8.0",
"eslint": "^5.0.0",
"eslint": "^6.6.0",
"jest": "^24.9.0",
"prettier": "^1.18.2",
"prettier": "^1.19.1",
"regenerator-runtime": "^0.13.3",
"typescript": "^3.6.3"
"typescript": "^3.7.2"
},

@@ -69,0 +69,0 @@ "publishConfig": {

@@ -122,4 +122,6 @@ # TeleJSON

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

@@ -126,0 +128,0 @@

@@ -65,2 +65,23 @@ import isRegExp from 'is-regex';

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$/;

@@ -77,2 +98,3 @@

space: number | undefined;
lazyEval: boolean;
}

@@ -120,3 +142,3 @@

) {
return `_function_${name}|${cleanCode(stringified)}`;
return `_function_${name}|${cleanCode(convertShorthandMethods(key, stringified))}`;
}

@@ -214,3 +236,3 @@ return `_function_${name}|${(() => {}).toString()}`;

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

@@ -256,2 +278,7 @@ let root: any;

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

@@ -319,6 +346,7 @@ const result = (...args: any[]) => {

allowSymbol: true,
lazyEval: true,
};
export const stringify = (data: any, options: Partial<Options> = {}) => {
const mergedOptions: Options = Object.assign({}, defaultOptions, options);
const mergedOptions: Options = { ...defaultOptions, ...options };
return JSON.stringify(data, replacer(mergedOptions), options.space);

@@ -352,4 +380,5 @@ };

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));

@@ -356,0 +385,0 @@ mutator()(result);

@@ -53,2 +53,5 @@ import * as src from '../src/index';

fn3,
fn4(x) {
return x * x;
},
date,

@@ -61,14 +64,14 @@ foo: new Foo(),

const tests = ({stringify, parse}) => {
const tests = ({ stringify, parse }) => {
test('sanity', () => {
expect(true).toBe(true);
});
test('stringify', () => {
let stringified;
expect(() => (stringified = stringify(data))).not.toThrow();
expect(stringified).toMatchSnapshot();
});
test('parse', () => {

@@ -79,3 +82,3 @@ const stringified = stringify(data);

expect(parsed).toMatchSnapshot();
// test the regex

@@ -86,11 +89,11 @@ expect(parsed.regex1.exec).toBeDefined();

expect('aaa-Foo-foo-bbb'.replace(parsed.regex3, 'BAR')).toBe('aaa-BAR-foo-bbb');
// test the date
expect(parsed.date).toBeInstanceOf(Date);
expect(parsed.date.getFullYear()).toBe(2018);
// test cyclic
expect(parsed.cyclic.cyclic.cyclic.cyclic).toBeDefined();
expect(parsed.cyclic.cyclic.cyclic.cyclic).toBe(parsed);
// test Foo instance

@@ -101,3 +104,3 @@ expect(parsed.foo).toBeDefined();

});
test('maxDepth', () => {

@@ -108,7 +111,7 @@ const stringifiedDefault = stringify(data);

const parsedMax5 = parse(stringifiedMax5);
expect(parsedDefault.nested.a.b.c.d.e.f.g.h.i).toBeDefined();
expect(parsedDefault.nested.a.b.c.d.e.f.g.h.i.j).toBeDefined();
expect(parsedDefault.nested.a.b.c.d.e.f.g.h.i.j.k).not.toBeDefined();
expect(parsedMax5.nested.a.b.c.d).toBeDefined();

@@ -118,13 +121,13 @@ expect(parsedMax5.nested.a.b.c.d.e).toBeDefined();

});
test('space', () => {
const stringifiedSpaced = stringify(data, { space: 2 });
expect(stringifiedSpaced).toMatchSnapshot();
});
test('stringify the global object', () => {
expect(() => stringify(global, { maxDepth: 10000 })).not.toThrow();
});
test('check duplicate value', () => {

@@ -138,3 +141,3 @@ const Fruit = {

const parsed = parse(stringified);
expect(stringified).toEqual('{"apple":true,"parent":{},"cyclic":"_duplicate_root"}');

@@ -145,9 +148,9 @@ expect(parsed.cyclic.cyclic.cyclic.cyclic).toBeDefined();

});
test('check constructor value', () => {
const data = { ConstructorFruit: new Foo() };
const stringified = stringify(data);
const parsed = parse(stringified);
expect(stringified).toEqual('{"ConstructorFruit":{"_constructor-name_":"Foo"}}');

@@ -158,3 +161,3 @@ expect(parsed.ConstructorFruit).toBeDefined();

});
test('check function value', () => {

@@ -165,6 +168,6 @@ const Fruit = function(value) {

const data = { FunctionFruit: Fruit };
const stringified = stringify(data);
const parsed = parse(stringified);
expect(stringified).toEqual(

@@ -178,19 +181,19 @@ '{"FunctionFruit":"_function_Fruit|function Fruit(value) {return [value, \'apple\'];}"}'

});
test('check regExp value', () => {
const data = { RegExpFruit: /test/g };
const stringified = stringify(data);
const parsed = parse(stringified);
expect(stringified).toEqual('{"RegExpFruit":"_regexp_g|test"}');
expect(parsed).toMatchObject(data);
});
test('check date value', () => {
const data = { DateFruit: new Date('01.01.2019') };
const stringified = stringify(data);
const parsed = parse(stringified);
expect(stringified).toEqual('{"DateFruit":"_date_2019-01-01T00:00:00.000Z"}');

@@ -200,49 +203,49 @@ expect(parsed).toMatchObject(data);

});
test('check symbol value', () => {
const data = { SymbleFruit: Symbol('apple') };
const stringified = stringify(data);
const parsed = parse(stringified);
expect(stringified).toEqual('{"SymbleFruit":"_symbol_apple"}');
expect(parsed.SymbleFruit.toString()).toEqual('Symbol(apple)');
});
test('check minus Infinity value', () => {
const data = { InfinityFruit: -Infinity };
const stringified = stringify(data);
const parsed = parse(stringified);
expect(stringified).toEqual('{"InfinityFruit":"_-Infinity_"}');
expect(parsed).toMatchObject(data);
});
test('check Infinity value', () => {
const data = { InfinityFruit: Infinity };
const stringified = stringify(data);
const parsed = parse(stringified);
expect(stringified).toEqual('{"InfinityFruit":"_Infinity_"}');
expect(parsed).toMatchObject(data);
});
test('check NaN value', () => {
const data = { NaNFruit: NaN };
const stringified = stringify(data);
const parsed = parse(stringified);
expect(stringified).toEqual('{"NaNFruit":"_NaN_"}');
expect(parsed).toMatchObject(data);
});
test('check undefined value', () => {
const data = { undefinedFruit: undefined };
const stringified = stringify(data);
const parsed = parse(stringified);
expect(stringified).toEqual('{"undefinedFruit":"_undefined_"}');

@@ -252,3 +255,3 @@ expect(parsed.undefinedFruit).toEqual(undefined);

});
test('primitives should not be deduplicated', () => {

@@ -272,6 +275,6 @@ const data = {

};
const stringified = stringify(data);
const parsed = parse(stringified);
expect(stringified).toEqual(

@@ -282,3 +285,3 @@ '{"bool":true,"a":1,"b":"1","c":{"bool":true,"c":1,"d":3,"e":"3","f":{"bool":true,"c":"1","d":3,"e":"3"}}}'

});
test('bug', () => {

@@ -293,29 +296,28 @@ const data = {

},
f: [1,2,3,4,5],
f: [1, 2, 3, 4, 5],
g: undefined,
h: null,
i: () => {},
j: function(){}
j: function() {},
};
const stringified = stringify(data);
expect(stringified).toMatch("{\"a\":1,\"b\":\"2\",\"c\":\"_NaN_\",\"d\":true,\"e\":{\"1\":\"_undefined_\"},\"f\":[1,2,3,4,5],\"g\":\"_undefined_\",\"h\":null,\"i\":\"_function_i|function i() {}\",\"j\":\"_function_j|function j() {}\"}")
expect(stringified).toMatch(
'{"a":1,"b":"2","c":"_NaN_","d":true,"e":{"1":"_undefined_"},"f":[1,2,3,4,5],"g":"_undefined_","h":null,"i":"_function_i|function i() {}","j":"_function_j|function j() {}"}'
);
const parsed = parse(stringified);
Object.entries(parsed).forEach((k, v) => {
expect(data[k]).toEqual(parsed[k]);
})
});
});
}
};
describe('Source', () => {
tests(src);
});
describe('Source',() => {
tests(src)
})
describe('Dist',() => {
tests(dist)
})
describe('Dist', () => {
tests(dist);
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc