@nuxtjs/devalue
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -7,5 +7,15 @@ var consola = require('consola'); | ||
var objectProtoOwnPropertyNames = Object.getOwnPropertyNames(Object.prototype).sort().join('\0'); | ||
// workaround to disable warnings, see https://github.com/nuxt/nuxt.js/issues/4026 for details | ||
var defaultLogLevel = process.env.NUXT_ENV_DEVALUE_LOG_LEVEL || 'warn'; | ||
var logLimit = parseInt(process.env.NUXT_ENV_DEVALUE_LOG_LIMIT) || 99; | ||
function devalue(value, level) { | ||
if (level === void 0) { level = 'warn'; } | ||
if (level === void 0) { level = defaultLogLevel; } | ||
var counts = new Map(); | ||
var logNum = 0; | ||
function log(message) { | ||
if (logNum < logLimit) { | ||
consola[level](message); | ||
logNum += 1; | ||
} | ||
} | ||
function walk(thing) { | ||
@@ -43,7 +53,7 @@ if (typeof thing === 'function') { | ||
if (typeof thing.toJSON !== "function") { | ||
consola[level]("Cannot stringify arbitrary non-POJOs " + thing.constructor.name); | ||
log("Cannot stringify arbitrary non-POJOs " + thing.constructor.name); | ||
} | ||
} | ||
else if (Object.getOwnPropertySymbols(thing).length > 0) { | ||
consola[level]("Cannot stringify POJOs with symbolic keys " + Object.getOwnPropertySymbols(thing)); | ||
log("Cannot stringify POJOs with symbolic keys " + Object.getOwnPropertySymbols(thing).map(function (symbol) { return symbol.toString(); })); | ||
} | ||
@@ -50,0 +60,0 @@ else { |
@@ -7,192 +7,202 @@ (function (global, factory) { | ||
var consola = require('consola'); | ||
var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$'; | ||
var reserved = /^(?:do|if|in|for|int|let|new|try|var|byte|case|char|else|enum|goto|long|this|void|with|await|break|catch|class|const|final|float|short|super|throw|while|yield|delete|double|export|import|native|return|switch|throws|typeof|boolean|default|extends|finally|package|private|abstract|continue|debugger|function|volatile|interface|protected|transient|implements|instanceof|synchronized)$/; | ||
var unsafe = /[<>\/\u2028\u2029]/g; | ||
var escaped = { '<': '\\u003C', '>': '\\u003E', '/': '\\u002F', '\u2028': '\\u2028', '\u2029': '\\u2029' }; | ||
var objectProtoOwnPropertyNames = Object.getOwnPropertyNames(Object.prototype).sort().join('\0'); | ||
function devalue(value, level) { | ||
if (level === void 0) { level = 'warn'; } | ||
var counts = new Map(); | ||
function walk(thing) { | ||
if (typeof thing === 'function') { | ||
consola[level]("Cannot stringify a function " + thing.name); | ||
return; | ||
} | ||
if (counts.has(thing)) { | ||
counts.set(thing, counts.get(thing) + 1); | ||
return; | ||
} | ||
counts.set(thing, 1); | ||
if (!isPrimitive(thing)) { | ||
var type = getType(thing); | ||
switch (type) { | ||
case 'Number': | ||
case 'String': | ||
case 'Boolean': | ||
case 'Date': | ||
case 'RegExp': | ||
return; | ||
case 'Array': | ||
thing.forEach(walk); | ||
break; | ||
case 'Set': | ||
case 'Map': | ||
Array.from(thing).forEach(walk); | ||
break; | ||
default: | ||
var proto = Object.getPrototypeOf(thing); | ||
if (proto !== Object.prototype && | ||
proto !== null && | ||
Object.getOwnPropertyNames(proto).sort().join('\0') !== objectProtoOwnPropertyNames) { | ||
if (typeof thing.toJSON !== "function") { | ||
consola[level]("Cannot stringify arbitrary non-POJOs " + thing.constructor.name); | ||
} | ||
} | ||
else if (Object.getOwnPropertySymbols(thing).length > 0) { | ||
consola[level]("Cannot stringify POJOs with symbolic keys " + Object.getOwnPropertySymbols(thing)); | ||
} | ||
else { | ||
Object.keys(thing).forEach(function (key) { return walk(thing[key]); }); | ||
} | ||
} | ||
} | ||
} | ||
walk(value); | ||
var names = new Map(); | ||
Array.from(counts) | ||
.filter(function (entry) { return entry[1] > 1; }) | ||
.sort(function (a, b) { return b[1] - a[1]; }) | ||
.forEach(function (entry, i) { | ||
names.set(entry[0], getName(i)); | ||
}); | ||
function stringify(thing) { | ||
if (names.has(thing)) { | ||
return names.get(thing); | ||
} | ||
if (isPrimitive(thing)) { | ||
return stringifyPrimitive(thing); | ||
} | ||
var type = getType(thing); | ||
switch (type) { | ||
case 'Number': | ||
case 'String': | ||
case 'Boolean': | ||
return "Object(" + stringify(thing.valueOf()) + ")"; | ||
case 'RegExp': | ||
return thing.toString(); | ||
case 'Date': | ||
return "new Date(" + thing.getTime() + ")"; | ||
case 'Array': | ||
var members = thing.map(function (v, i) { return i in thing ? stringify(v) : ''; }); | ||
var tail = thing.length === 0 || (thing.length - 1 in thing) ? '' : ','; | ||
return "[" + members.join(',') + tail + "]"; | ||
case 'Set': | ||
case 'Map': | ||
return "new " + type + "([" + Array.from(thing).map(stringify).join(',') + "])"; | ||
default: | ||
var thingToSerialize_1 = thing.toJSON ? thing.toJSON() : thing; | ||
var obj = "{" + Object.keys(thingToSerialize_1).map(function (key) { return safeKey(key) + ":" + stringify(thingToSerialize_1[key]); }).join(',') + "}"; | ||
var proto = Object.getPrototypeOf(thingToSerialize_1); | ||
if (proto === null) { | ||
return Object.keys(thingToSerialize_1).length > 0 | ||
? "Object.assign(Object.create(null)," + obj + ")" | ||
: "Object.create(null)"; | ||
} | ||
return obj; | ||
} | ||
} | ||
var str = stringify(value); | ||
if (names.size) { | ||
var params_1 = []; | ||
var statements_1 = []; | ||
var values_1 = []; | ||
names.forEach(function (name, thing) { | ||
params_1.push(name); | ||
if (isPrimitive(thing)) { | ||
values_1.push(stringifyPrimitive(thing)); | ||
return; | ||
} | ||
var type = getType(thing); | ||
switch (type) { | ||
case 'Number': | ||
case 'String': | ||
case 'Boolean': | ||
values_1.push("Object(" + stringify(thing.valueOf()) + ")"); | ||
break; | ||
case 'RegExp': | ||
values_1.push(thing.toString()); | ||
break; | ||
case 'Date': | ||
values_1.push("new Date(" + thing.getTime() + ")"); | ||
break; | ||
case 'Array': | ||
values_1.push("Array(" + thing.length + ")"); | ||
thing.forEach(function (v, i) { | ||
statements_1.push(name + "[" + i + "]=" + stringify(v)); | ||
}); | ||
break; | ||
case 'Set': | ||
values_1.push("new Set"); | ||
statements_1.push(name + "." + Array.from(thing).map(function (v) { return "add(" + stringify(v) + ")"; }).join('.')); | ||
break; | ||
case 'Map': | ||
values_1.push("new Map"); | ||
statements_1.push(name + "." + Array.from(thing).map(function (_a) { | ||
var k = _a[0], v = _a[1]; | ||
return "set(" + stringify(k) + ", " + stringify(v) + ")"; | ||
}).join('.')); | ||
break; | ||
default: | ||
values_1.push(Object.getPrototypeOf(thing) === null ? 'Object.create(null)' : '{}'); | ||
Object.keys(thing).forEach(function (key) { | ||
statements_1.push("" + name + safeProp(key) + "=" + stringify(thing[key])); | ||
}); | ||
} | ||
}); | ||
statements_1.push("return " + str); | ||
return "(function(" + params_1.join(',') + "){" + statements_1.join(';') + "}(" + values_1.join(',') + "))"; | ||
} | ||
else { | ||
return str; | ||
} | ||
} | ||
function getName(num) { | ||
var name = ''; | ||
do { | ||
name = chars[num % chars.length] + name; | ||
num = ~~(num / chars.length) - 1; | ||
} while (num >= 0); | ||
return reserved.test(name) ? name + "_" : name; | ||
} | ||
function isPrimitive(thing) { | ||
return Object(thing) !== thing; | ||
} | ||
function escape(char) { | ||
return escaped[char]; | ||
} | ||
function stringifyPrimitive(thing) { | ||
if (typeof thing === 'string') | ||
return JSON.stringify(thing).replace(unsafe, escape); | ||
if (thing === void 0) | ||
return 'void 0'; | ||
if (thing === 0 && 1 / thing < 0) | ||
return '-0'; | ||
var str = String(thing); | ||
if (typeof thing === 'number') | ||
return str.replace(/^(-)?0\./, '$1.'); | ||
return str; | ||
} | ||
function getType(thing) { | ||
return Object.prototype.toString.call(thing).slice(8, -1); | ||
} | ||
function safeKey(key) { | ||
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? key : JSON.stringify(key); | ||
} | ||
function safeProp(key) { | ||
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? "." + key : "[" + JSON.stringify(key) + "]"; | ||
} | ||
var consola = require('consola'); | ||
var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$'; | ||
var reserved = /^(?:do|if|in|for|int|let|new|try|var|byte|case|char|else|enum|goto|long|this|void|with|await|break|catch|class|const|final|float|short|super|throw|while|yield|delete|double|export|import|native|return|switch|throws|typeof|boolean|default|extends|finally|package|private|abstract|continue|debugger|function|volatile|interface|protected|transient|implements|instanceof|synchronized)$/; | ||
var unsafe = /[<>\/\u2028\u2029]/g; | ||
var escaped = { '<': '\\u003C', '>': '\\u003E', '/': '\\u002F', '\u2028': '\\u2028', '\u2029': '\\u2029' }; | ||
var objectProtoOwnPropertyNames = Object.getOwnPropertyNames(Object.prototype).sort().join('\0'); | ||
// workaround to disable warnings, see https://github.com/nuxt/nuxt.js/issues/4026 for details | ||
var defaultLogLevel = process.env.NUXT_ENV_DEVALUE_LOG_LEVEL || 'warn'; | ||
var logLimit = parseInt(process.env.NUXT_ENV_DEVALUE_LOG_LIMIT) || 99; | ||
function devalue(value, level) { | ||
if (level === void 0) { level = defaultLogLevel; } | ||
var counts = new Map(); | ||
var logNum = 0; | ||
function log(message) { | ||
if (logNum < logLimit) { | ||
consola[level](message); | ||
logNum += 1; | ||
} | ||
} | ||
function walk(thing) { | ||
if (typeof thing === 'function') { | ||
consola[level]("Cannot stringify a function " + thing.name); | ||
return; | ||
} | ||
if (counts.has(thing)) { | ||
counts.set(thing, counts.get(thing) + 1); | ||
return; | ||
} | ||
counts.set(thing, 1); | ||
if (!isPrimitive(thing)) { | ||
var type = getType(thing); | ||
switch (type) { | ||
case 'Number': | ||
case 'String': | ||
case 'Boolean': | ||
case 'Date': | ||
case 'RegExp': | ||
return; | ||
case 'Array': | ||
thing.forEach(walk); | ||
break; | ||
case 'Set': | ||
case 'Map': | ||
Array.from(thing).forEach(walk); | ||
break; | ||
default: | ||
var proto = Object.getPrototypeOf(thing); | ||
if (proto !== Object.prototype && | ||
proto !== null && | ||
Object.getOwnPropertyNames(proto).sort().join('\0') !== objectProtoOwnPropertyNames) { | ||
if (typeof thing.toJSON !== "function") { | ||
log("Cannot stringify arbitrary non-POJOs " + thing.constructor.name); | ||
} | ||
} | ||
else if (Object.getOwnPropertySymbols(thing).length > 0) { | ||
log("Cannot stringify POJOs with symbolic keys " + Object.getOwnPropertySymbols(thing).map(function (symbol) { return symbol.toString(); })); | ||
} | ||
else { | ||
Object.keys(thing).forEach(function (key) { return walk(thing[key]); }); | ||
} | ||
} | ||
} | ||
} | ||
walk(value); | ||
var names = new Map(); | ||
Array.from(counts) | ||
.filter(function (entry) { return entry[1] > 1; }) | ||
.sort(function (a, b) { return b[1] - a[1]; }) | ||
.forEach(function (entry, i) { | ||
names.set(entry[0], getName(i)); | ||
}); | ||
function stringify(thing) { | ||
if (names.has(thing)) { | ||
return names.get(thing); | ||
} | ||
if (isPrimitive(thing)) { | ||
return stringifyPrimitive(thing); | ||
} | ||
var type = getType(thing); | ||
switch (type) { | ||
case 'Number': | ||
case 'String': | ||
case 'Boolean': | ||
return "Object(" + stringify(thing.valueOf()) + ")"; | ||
case 'RegExp': | ||
return thing.toString(); | ||
case 'Date': | ||
return "new Date(" + thing.getTime() + ")"; | ||
case 'Array': | ||
var members = thing.map(function (v, i) { return i in thing ? stringify(v) : ''; }); | ||
var tail = thing.length === 0 || (thing.length - 1 in thing) ? '' : ','; | ||
return "[" + members.join(',') + tail + "]"; | ||
case 'Set': | ||
case 'Map': | ||
return "new " + type + "([" + Array.from(thing).map(stringify).join(',') + "])"; | ||
default: | ||
var thingToSerialize_1 = thing.toJSON ? thing.toJSON() : thing; | ||
var obj = "{" + Object.keys(thingToSerialize_1).map(function (key) { return safeKey(key) + ":" + stringify(thingToSerialize_1[key]); }).join(',') + "}"; | ||
var proto = Object.getPrototypeOf(thingToSerialize_1); | ||
if (proto === null) { | ||
return Object.keys(thingToSerialize_1).length > 0 | ||
? "Object.assign(Object.create(null)," + obj + ")" | ||
: "Object.create(null)"; | ||
} | ||
return obj; | ||
} | ||
} | ||
var str = stringify(value); | ||
if (names.size) { | ||
var params_1 = []; | ||
var statements_1 = []; | ||
var values_1 = []; | ||
names.forEach(function (name, thing) { | ||
params_1.push(name); | ||
if (isPrimitive(thing)) { | ||
values_1.push(stringifyPrimitive(thing)); | ||
return; | ||
} | ||
var type = getType(thing); | ||
switch (type) { | ||
case 'Number': | ||
case 'String': | ||
case 'Boolean': | ||
values_1.push("Object(" + stringify(thing.valueOf()) + ")"); | ||
break; | ||
case 'RegExp': | ||
values_1.push(thing.toString()); | ||
break; | ||
case 'Date': | ||
values_1.push("new Date(" + thing.getTime() + ")"); | ||
break; | ||
case 'Array': | ||
values_1.push("Array(" + thing.length + ")"); | ||
thing.forEach(function (v, i) { | ||
statements_1.push(name + "[" + i + "]=" + stringify(v)); | ||
}); | ||
break; | ||
case 'Set': | ||
values_1.push("new Set"); | ||
statements_1.push(name + "." + Array.from(thing).map(function (v) { return "add(" + stringify(v) + ")"; }).join('.')); | ||
break; | ||
case 'Map': | ||
values_1.push("new Map"); | ||
statements_1.push(name + "." + Array.from(thing).map(function (_a) { | ||
var k = _a[0], v = _a[1]; | ||
return "set(" + stringify(k) + ", " + stringify(v) + ")"; | ||
}).join('.')); | ||
break; | ||
default: | ||
values_1.push(Object.getPrototypeOf(thing) === null ? 'Object.create(null)' : '{}'); | ||
Object.keys(thing).forEach(function (key) { | ||
statements_1.push("" + name + safeProp(key) + "=" + stringify(thing[key])); | ||
}); | ||
} | ||
}); | ||
statements_1.push("return " + str); | ||
return "(function(" + params_1.join(',') + "){" + statements_1.join(';') + "}(" + values_1.join(',') + "))"; | ||
} | ||
else { | ||
return str; | ||
} | ||
} | ||
function getName(num) { | ||
var name = ''; | ||
do { | ||
name = chars[num % chars.length] + name; | ||
num = ~~(num / chars.length) - 1; | ||
} while (num >= 0); | ||
return reserved.test(name) ? name + "_" : name; | ||
} | ||
function isPrimitive(thing) { | ||
return Object(thing) !== thing; | ||
} | ||
function escape(char) { | ||
return escaped[char]; | ||
} | ||
function stringifyPrimitive(thing) { | ||
if (typeof thing === 'string') | ||
return JSON.stringify(thing).replace(unsafe, escape); | ||
if (thing === void 0) | ||
return 'void 0'; | ||
if (thing === 0 && 1 / thing < 0) | ||
return '-0'; | ||
var str = String(thing); | ||
if (typeof thing === 'number') | ||
return str.replace(/^(-)?0\./, '$1.'); | ||
return str; | ||
} | ||
function getType(thing) { | ||
return Object.prototype.toString.call(thing).slice(8, -1); | ||
} | ||
function safeKey(key) { | ||
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? key : JSON.stringify(key); | ||
} | ||
function safeProp(key) { | ||
return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? "." + key : "[" + JSON.stringify(key) + "]"; | ||
} | ||
return devalue; | ||
return devalue; | ||
}))); |
{ | ||
"name": "@nuxtjs/devalue", | ||
"description": "Gets the job done when JSON.stringify can't", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"repository": "nuxt-community/devalue", | ||
@@ -17,12 +17,13 @@ "main": "dist/devalue.umd.js", | ||
"devDependencies": { | ||
"@types/mocha": "^2.2.44", | ||
"@types/node": "^8.0.53", | ||
"glob": "^7.1.2", | ||
"mocha": "^4.0.1", | ||
"rollup": "^0.52.0", | ||
"rollup-plugin-typescript": "^0.8.1", | ||
"@types/mocha": "^5.2.5", | ||
"@types/node": "^10.11.7", | ||
"glob": "^7.1.3", | ||
"mocha": "^5.2.0", | ||
"rollup": "^0.66.6", | ||
"rollup-plugin-typescript": "^1.0.0", | ||
"rollup-plugin-virtual": "^1.0.1", | ||
"sander": "^0.6.0", | ||
"ts-node": "^3.3.0", | ||
"typescript": "^2.6.2" | ||
"ts-node": "^7.0.1", | ||
"tslib": "^1.9.3", | ||
"typescript": "^3.1.3" | ||
}, | ||
@@ -29,0 +30,0 @@ "scripts": { |
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances 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
22917
405
11
5