Comparing version 3.2.0 to 3.3.0
@@ -24,3 +24,3 @@ "use strict"; | ||
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 _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } | ||
@@ -118,130 +118,134 @@ 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; } | ||
return function replace(key, value) { | ||
// very first iteration | ||
if (key === '') { | ||
keys = ['root']; | ||
objects = [{ | ||
keys: 'root', | ||
value: value | ||
}]; | ||
stack = []; | ||
return value; | ||
} // From the JSON.stringify's doc: | ||
// "The object in which the key was found is provided as the replacer's this parameter." thus one can control the depth | ||
try { | ||
// very first iteration | ||
if (key === '') { | ||
keys = ['root']; | ||
objects = [{ | ||
keys: 'root', | ||
value: value | ||
}]; | ||
stack = []; | ||
return value; | ||
} // From the JSON.stringify's doc: | ||
// "The object in which the key was found is provided as the replacer's this parameter." thus one can control the depth | ||
while (stack.length && this !== stack[0]) { | ||
stack.shift(); | ||
keys.pop(); | ||
} | ||
if ((0, _isRegex["default"])(value)) { | ||
if (!options.allowRegExp) { | ||
return undefined; | ||
while (stack.length && this !== stack[0]) { | ||
stack.shift(); | ||
keys.pop(); | ||
} | ||
return "_regexp_".concat(value.flags, "|").concat(value.source); | ||
} | ||
if ((0, _isRegex["default"])(value)) { | ||
if (!options.allowRegExp) { | ||
return undefined; | ||
} | ||
if ((0, _isFunction["default"])(value)) { | ||
if (!options.allowFunction) { | ||
return undefined; | ||
return "_regexp_".concat(value.flags, "|").concat(value.source); | ||
} | ||
var name = value.name; | ||
var stringified = value.toString(); | ||
if ((0, _isFunction["default"])(value)) { | ||
if (!options.allowFunction) { | ||
return undefined; | ||
} | ||
if (!stringified.match(/(\[native code\]|WEBPACK_IMPORTED_MODULE|__webpack_exports__|__webpack_require__)/)) { | ||
return "_function_".concat(name, "|").concat(cleanCode(convertShorthandMethods(key, stringified))); | ||
var name = value.name; | ||
var stringified = value.toString(); | ||
if (!stringified.match(/(\[native code\]|WEBPACK_IMPORTED_MODULE|__webpack_exports__|__webpack_require__)/)) { | ||
return "_function_".concat(name, "|").concat(cleanCode(convertShorthandMethods(key, stringified))); | ||
} | ||
return "_function_".concat(name, "|").concat(function () {}.toString()); | ||
} | ||
return "_function_".concat(name, "|").concat(function () {}.toString()); | ||
} | ||
if ((0, _isSymbol["default"])(value)) { | ||
if (!options.allowSymbol) { | ||
return undefined; | ||
} | ||
if ((0, _isSymbol["default"])(value)) { | ||
if (!options.allowSymbol) { | ||
return undefined; | ||
return "_symbol_".concat(value.toString().slice(7, -1)); | ||
} | ||
return "_symbol_".concat(value.toString().slice(7, -1)); | ||
} | ||
if (typeof value === 'string' && dateFormat.test(value)) { | ||
if (!options.allowDate) { | ||
return undefined; | ||
} | ||
if (typeof value === 'string' && dateFormat.test(value)) { | ||
if (!options.allowDate) { | ||
return undefined; | ||
return "_date_".concat(value); | ||
} | ||
return "_date_".concat(value); | ||
} | ||
if (value === undefined) { | ||
if (!options.allowUndefined) { | ||
return undefined; | ||
} | ||
if (value === undefined) { | ||
if (!options.allowUndefined) { | ||
return undefined; | ||
return '_undefined_'; | ||
} | ||
return '_undefined_'; | ||
} | ||
if (typeof value === 'number') { | ||
if (value === -Infinity) { | ||
return '_-Infinity_'; | ||
} | ||
if (typeof value === 'number') { | ||
if (value === -Infinity) { | ||
return '_-Infinity_'; | ||
if (value === Infinity) { | ||
return '_Infinity_'; | ||
} | ||
if (Number.isNaN(value)) { | ||
return '_NaN_'; | ||
} | ||
return value; | ||
} | ||
if (value === Infinity) { | ||
return '_Infinity_'; | ||
if (typeof value === 'string') { | ||
return value; | ||
} | ||
if (Number.isNaN(value)) { | ||
return '_NaN_'; | ||
if (typeof value === 'boolean') { | ||
return value; | ||
} | ||
return value; | ||
} | ||
if (stack.length >= options.maxDepth) { | ||
if (Array.isArray(value)) { | ||
return "[Array(".concat(value.length, ")]"); | ||
} | ||
if (typeof value === 'string') { | ||
return value; | ||
} | ||
if (typeof value === 'boolean') { | ||
return value; | ||
} | ||
if (stack.length >= options.maxDepth) { | ||
if (Array.isArray(value)) { | ||
return "[Array(".concat(value.length, ")]"); | ||
return '[Object]'; | ||
} | ||
return '[Object]'; | ||
} | ||
var found = objects.find(function (o) { | ||
return o.value === value; | ||
}); | ||
var found = objects.find(function (o) { | ||
return o.value === value; | ||
}); | ||
if (!found) { | ||
if (value && isObject(value) && value.constructor && value.constructor.name && value.constructor.name !== 'Object') { | ||
if (!options.allowClass) { | ||
return undefined; | ||
} | ||
if (!found) { | ||
if (value && isObject(value) && value.constructor && value.constructor.name && value.constructor.name !== 'Object') { | ||
if (!options.allowClass) { | ||
return undefined; | ||
try { | ||
Object.assign(value, { | ||
'_constructor-name_': value.constructor.name | ||
}); | ||
} catch (e) {// immutable objects can't be written to and throw | ||
// we could make a deep copy but if the user values the correct instance name, | ||
// the user should make the deep copy themselves. | ||
} | ||
} | ||
try { | ||
Object.assign(value, { | ||
'_constructor-name_': value.constructor.name | ||
}); | ||
} catch (e) {// immutable objects can't be written to and throw | ||
// we could make a deep copy but if the user values the correct instance name, | ||
// the user should make the deep copy themselves. | ||
} | ||
} | ||
keys.push(key); | ||
stack.unshift(value); | ||
objects.push({ | ||
keys: keys.join('.'), | ||
value: value | ||
}); | ||
return value; | ||
} // actually, here's the only place where the keys keeping is useful | ||
keys.push(key); | ||
stack.unshift(value); | ||
objects.push({ | ||
keys: keys.join('.'), | ||
value: value | ||
}); | ||
return value; | ||
} // actually, here's the only place where the keys keeping is useful | ||
return "_duplicate_".concat(found.keys); | ||
return "_duplicate_".concat(found.keys); | ||
} catch (e) { | ||
return undefined; | ||
} | ||
}; | ||
@@ -428,3 +432,5 @@ }; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var mergedOptions = Object.assign({}, defaultOptions, options); | ||
var mergedOptions = _objectSpread({}, defaultOptions, {}, options); | ||
var result = JSON.parse(data, reviver(mergedOptions)); | ||
@@ -431,0 +437,0 @@ mutator()(result); |
{ | ||
"name": "telejson", | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"description": "", | ||
@@ -51,6 +51,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"@babel/cli": "^7.7.0", | ||
"@babel/core": "^7.7.2", | ||
"@babel/preset-env": "^7.7.1", | ||
"@babel/preset-typescript": "^7.7.2", | ||
"@babel/cli": "^7.7.4", | ||
"@babel/core": "^7.7.4", | ||
"@babel/preset-env": "^7.7.4", | ||
"@babel/preset-typescript": "^7.7.4", | ||
"@storybook/eslint-config-storybook": "^2.1.0", | ||
@@ -62,3 +62,3 @@ "@types/jest": "^24.0.23", | ||
"common-tags": "^1.8.0", | ||
"eslint": "^6.6.0", | ||
"eslint": "^6.7.1", | ||
"jest": "^24.9.0", | ||
@@ -65,0 +65,0 @@ "prettier": "^1.19.1", |
194
src/index.ts
@@ -106,121 +106,125 @@ import isRegExp from 'is-regex'; | ||
return function replace(this: any, key: string, value: any) { | ||
// very first iteration | ||
if (key === '') { | ||
keys = ['root']; | ||
objects = [{ keys: 'root', value }]; | ||
stack = []; | ||
return value; | ||
} | ||
// From the JSON.stringify's doc: | ||
// "The object in which the key was found is provided as the replacer's this parameter." thus one can control the depth | ||
while (stack.length && this !== stack[0]) { | ||
stack.shift(); | ||
keys.pop(); | ||
} | ||
if (isRegExp(value)) { | ||
if (!options.allowRegExp) { | ||
return undefined; | ||
try { | ||
// very first iteration | ||
if (key === '') { | ||
keys = ['root']; | ||
objects = [{ keys: 'root', value }]; | ||
stack = []; | ||
return value; | ||
} | ||
return `_regexp_${value.flags}|${value.source}`; | ||
} | ||
if (isFunction(value)) { | ||
if (!options.allowFunction) { | ||
return undefined; | ||
// From the JSON.stringify's doc: | ||
// "The object in which the key was found is provided as the replacer's this parameter." thus one can control the depth | ||
while (stack.length && this !== stack[0]) { | ||
stack.shift(); | ||
keys.pop(); | ||
} | ||
const { name } = value; | ||
const stringified = value.toString(); | ||
if ( | ||
!stringified.match( | ||
/(\[native code\]|WEBPACK_IMPORTED_MODULE|__webpack_exports__|__webpack_require__)/ | ||
) | ||
) { | ||
return `_function_${name}|${cleanCode(convertShorthandMethods(key, stringified))}`; | ||
if (isRegExp(value)) { | ||
if (!options.allowRegExp) { | ||
return undefined; | ||
} | ||
return `_regexp_${value.flags}|${value.source}`; | ||
} | ||
return `_function_${name}|${(() => {}).toString()}`; | ||
} | ||
if (isSymbol(value)) { | ||
if (!options.allowSymbol) { | ||
return undefined; | ||
if (isFunction(value)) { | ||
if (!options.allowFunction) { | ||
return undefined; | ||
} | ||
const { name } = value; | ||
const stringified = value.toString(); | ||
if ( | ||
!stringified.match( | ||
/(\[native code\]|WEBPACK_IMPORTED_MODULE|__webpack_exports__|__webpack_require__)/ | ||
) | ||
) { | ||
return `_function_${name}|${cleanCode(convertShorthandMethods(key, stringified))}`; | ||
} | ||
return `_function_${name}|${(() => {}).toString()}`; | ||
} | ||
return `_symbol_${value.toString().slice(7, -1)}`; | ||
} | ||
if (typeof value === 'string' && dateFormat.test(value)) { | ||
if (!options.allowDate) { | ||
return undefined; | ||
if (isSymbol(value)) { | ||
if (!options.allowSymbol) { | ||
return undefined; | ||
} | ||
return `_symbol_${value.toString().slice(7, -1)}`; | ||
} | ||
return `_date_${value}`; | ||
} | ||
if (value === undefined) { | ||
if (!options.allowUndefined) { | ||
return undefined; | ||
if (typeof value === 'string' && dateFormat.test(value)) { | ||
if (!options.allowDate) { | ||
return undefined; | ||
} | ||
return `_date_${value}`; | ||
} | ||
return '_undefined_'; | ||
} | ||
if (typeof value === 'number') { | ||
if (value === -Infinity) { | ||
return '_-Infinity_'; | ||
if (value === undefined) { | ||
if (!options.allowUndefined) { | ||
return undefined; | ||
} | ||
return '_undefined_'; | ||
} | ||
if (value === Infinity) { | ||
return '_Infinity_'; | ||
} | ||
if (Number.isNaN(value)) { | ||
return '_NaN_'; | ||
} | ||
return value; | ||
} | ||
if (typeof value === 'number') { | ||
if (value === -Infinity) { | ||
return '_-Infinity_'; | ||
} | ||
if (value === Infinity) { | ||
return '_Infinity_'; | ||
} | ||
if (Number.isNaN(value)) { | ||
return '_NaN_'; | ||
} | ||
if (typeof value === 'string') { | ||
return value; | ||
} | ||
return value; | ||
} | ||
if (typeof value === 'boolean') { | ||
return value; | ||
} | ||
if (typeof value === 'string') { | ||
return value; | ||
} | ||
if (stack.length >= options.maxDepth) { | ||
if (Array.isArray(value)) { | ||
return `[Array(${value.length})]`; | ||
if (typeof value === 'boolean') { | ||
return value; | ||
} | ||
return '[Object]'; | ||
} | ||
const found = objects.find(o => o.value === value); | ||
if (!found) { | ||
if ( | ||
value && | ||
isObject(value) && | ||
value.constructor && | ||
value.constructor.name && | ||
value.constructor.name !== 'Object' | ||
) { | ||
if (!options.allowClass) { | ||
return undefined; | ||
if (stack.length >= options.maxDepth) { | ||
if (Array.isArray(value)) { | ||
return `[Array(${value.length})]`; | ||
} | ||
return '[Object]'; | ||
} | ||
try { | ||
Object.assign(value, { '_constructor-name_': value.constructor.name }); | ||
} catch (e) { | ||
// immutable objects can't be written to and throw | ||
// we could make a deep copy but if the user values the correct instance name, | ||
// the user should make the deep copy themselves. | ||
const found = objects.find(o => o.value === value); | ||
if (!found) { | ||
if ( | ||
value && | ||
isObject(value) && | ||
value.constructor && | ||
value.constructor.name && | ||
value.constructor.name !== 'Object' | ||
) { | ||
if (!options.allowClass) { | ||
return undefined; | ||
} | ||
try { | ||
Object.assign(value, { '_constructor-name_': value.constructor.name }); | ||
} catch (e) { | ||
// immutable objects can't be written to and throw | ||
// we could make a deep copy but if the user values the correct instance name, | ||
// the user should make the deep copy themselves. | ||
} | ||
} | ||
keys.push(key); | ||
stack.unshift(value); | ||
objects.push({ keys: keys.join('.'), value }); | ||
return value; | ||
} | ||
keys.push(key); | ||
stack.unshift(value); | ||
objects.push({ keys: keys.join('.'), value }); | ||
return value; | ||
// actually, here's the only place where the keys keeping is useful | ||
return `_duplicate_${found.keys}`; | ||
} catch (e) { | ||
return undefined; | ||
} | ||
// actually, here's the only place where the keys keeping is useful | ||
return `_duplicate_${found.keys}`; | ||
}; | ||
@@ -376,3 +380,3 @@ }; | ||
export const parse = (data: string, options: Partial<Options> = {}) => { | ||
const mergedOptions: Options = Object.assign({}, defaultOptions, options); | ||
const mergedOptions: Options = { ...defaultOptions, ...options}; | ||
const result = JSON.parse(data, reviver(mergedOptions)); | ||
@@ -379,0 +383,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
319781
15
980