javascript-stringify
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -1,5 +0,5 @@ | ||
import { Next } from "./types"; | ||
import { ToString } from "./types"; | ||
/** | ||
* Stringify an array of values. | ||
*/ | ||
export declare function arrayToString(array: any[], space: string, next: Next): string; | ||
export declare const arrayToString: ToString; |
@@ -6,3 +6,3 @@ "use strict"; | ||
*/ | ||
function arrayToString(array, space, next) { | ||
exports.arrayToString = (array, space, next) => { | ||
// Map array values to their stringified values with correct indentation. | ||
@@ -17,9 +17,5 @@ const values = array | ||
.join(space ? ",\n" : ","); | ||
// Wrap the array in newlines if we have indentation set. | ||
if (space && values) { | ||
return "[\n" + values + "\n]"; | ||
} | ||
return "[" + values + "]"; | ||
} | ||
exports.arrayToString = arrayToString; | ||
const eol = space && values ? "\n" : ""; | ||
return `[${eol}${values}${eol}]`; | ||
}; | ||
//# sourceMappingURL=array.js.map |
@@ -1,2 +0,2 @@ | ||
import { Next } from "./types"; | ||
import { Next, ToString } from "./types"; | ||
declare const FUNCTION_PREFIXES: { | ||
@@ -9,5 +9,9 @@ Function: string; | ||
/** | ||
* Track function parser usage. | ||
*/ | ||
export declare const USED_METHOD_KEY: WeakSet<Function>; | ||
/** | ||
* Stringify a function. | ||
*/ | ||
export declare function functionToString(fn: Function, space: string, next: Next): string; | ||
export declare const functionToString: ToString; | ||
/** | ||
@@ -24,3 +28,3 @@ * Rewrite a stringified function to remove initial indentation. | ||
next: Next; | ||
key?: string | number | symbol | undefined; | ||
key?: string | undefined; | ||
fnString: string; | ||
@@ -33,3 +37,3 @@ fnType: keyof typeof FUNCTION_PREFIXES; | ||
hadKeyword: boolean; | ||
constructor(fn: Function, indent: string, next: Next, key?: string | number | symbol | undefined); | ||
constructor(fn: Function, indent: string, next: Next, key?: string | undefined); | ||
stringify(): string; | ||
@@ -36,0 +40,0 @@ getPrefix(): string; |
@@ -30,8 +30,15 @@ "use strict"; | ||
/** | ||
* Track function parser usage. | ||
*/ | ||
exports.USED_METHOD_KEY = new WeakSet(); | ||
/** | ||
* Stringify a function. | ||
*/ | ||
function functionToString(fn, space, next) { | ||
return new FunctionParser(fn, space, next).stringify(); | ||
} | ||
exports.functionToString = functionToString; | ||
exports.functionToString = (fn, space, next, key) => { | ||
const name = typeof key === "string" ? key : undefined; | ||
// Track in function parser for object stringify to avoid duplicate output. | ||
if (name !== undefined) | ||
exports.USED_METHOD_KEY.add(fn); | ||
return new FunctionParser(fn, space, next, name).stringify(); | ||
}; | ||
/** | ||
@@ -38,0 +45,0 @@ * Rewrite a stringified function to remove initial indentation. |
@@ -32,5 +32,5 @@ "use strict"; | ||
if (key === undefined) | ||
return valueToString(value, space, onNext); | ||
return valueToString(value, space, onNext, key); | ||
path.push(key); | ||
const result = builder(value); | ||
const result = builder(value, key === ROOT_SENTINEL ? undefined : key); | ||
path.pop(); | ||
@@ -40,3 +40,3 @@ return result; | ||
const builder = references | ||
? (value) => { | ||
? (value, key) => { | ||
if (value !== null && | ||
@@ -54,5 +54,5 @@ (typeof value === "object" || | ||
} | ||
return valueToString(value, space, onNext); | ||
return valueToString(value, space, onNext, key); | ||
} | ||
: (value) => { | ||
: (value, key) => { | ||
// Stop on recursion. | ||
@@ -62,3 +62,3 @@ if (stack.has(value)) | ||
stack.add(value); | ||
const result = valueToString(value, space, onNext); | ||
const result = valueToString(value, space, onNext, key); | ||
stack.delete(value); | ||
@@ -89,6 +89,6 @@ return result; | ||
return stringify_1.toString; | ||
return (value, space, next) => { | ||
return replacer(value, space, (value) => stringify_1.toString(value, space, next)); | ||
return (value, space, next, key) => { | ||
return replacer(value, space, (value) => stringify_1.toString(value, space, next, key), key); | ||
}; | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -125,3 +125,3 @@ "use strict"; | ||
}); | ||
describe("should not take the names of their keys", cases(["{name:function () {}}", "{'tricky name':function () {}}"])); | ||
describe("omit the names of their keys", cases(["{name:function () {}}", "{'tricky name':function () {}}"])); | ||
}); | ||
@@ -148,2 +148,5 @@ describe("native instances", () => { | ||
}); | ||
describeIf("BigInt", typeof BigInt === "function", () => { | ||
it("should stringify", test(BigInt("10"), "BigInt('10')")); | ||
}); | ||
describe("Error", () => { | ||
@@ -483,2 +486,24 @@ it("should stringify", test(new Error("test"), "new Error('test')")); | ||
}); | ||
it("should support object functions", () => { | ||
function makeRaw(str) { | ||
const fn = () => { | ||
/* Noop. */ | ||
}; | ||
fn.__expression = str; | ||
return fn; | ||
} | ||
const result = index_1.stringify({ | ||
"no-console": makeRaw(`process.env.NODE_ENV === 'production' ? 'error' : 'off'`), | ||
"no-debugger": makeRaw(`process.env.NODE_ENV === 'production' ? 'error' : 'off'`) | ||
}, (val, indent, stringify) => { | ||
if (val && val.__expression) { | ||
return val.__expression; | ||
} | ||
return stringify(val); | ||
}, 2); | ||
expect(result).toEqual(`{ | ||
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off', | ||
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off' | ||
}`); | ||
}); | ||
}); | ||
@@ -485,0 +510,0 @@ describe("max depth", () => { |
@@ -1,5 +0,5 @@ | ||
import { Next } from "./types"; | ||
import { ToString } from "./types"; | ||
/** | ||
* Stringify an object of keys and values. | ||
* Transform an object into a string. | ||
*/ | ||
export declare function objectToString(obj: any, indent: string, next: Next): string; | ||
export declare const objectToString: ToString; |
@@ -5,17 +5,25 @@ "use strict"; | ||
const function_1 = require("./function"); | ||
const array_1 = require("./array"); | ||
/** | ||
* Transform an object into a string. | ||
*/ | ||
exports.objectToString = (value, space, next, key) => { | ||
if (typeof Buffer === "function" && Buffer.isBuffer(value)) { | ||
return `new Buffer(${next(value.toString())})`; | ||
} | ||
// Use the internal object string to select stringify method. | ||
const toString = OBJECT_TYPES[Object.prototype.toString.call(value)]; | ||
return toString ? toString(value, space, next, key) : undefined; | ||
}; | ||
/** | ||
* Stringify an object of keys and values. | ||
*/ | ||
function objectToString(obj, indent, next) { | ||
const rawObjectToString = (obj, indent, next) => { | ||
const eol = indent ? "\n" : ""; | ||
const space = indent ? " " : ""; | ||
// Iterate over object keys and concat string together. | ||
const values = Object.keys(obj) | ||
.reduce(function (values, key) { | ||
if (typeof obj[key] === "function") { | ||
const parser = new function_1.FunctionParser(obj[key], indent, next, key); | ||
const result = parser.stringify(); | ||
values.push(indent + result.split("\n").join(`\n${indent}`)); | ||
return values; | ||
} | ||
const result = next(obj[key], key); | ||
const fn = obj[key]; | ||
const result = next(fn, key); | ||
// Omit `undefined` object entries. | ||
@@ -26,3 +34,8 @@ if (result === undefined) | ||
const value = result.split("\n").join(`\n${indent}`); | ||
values.push(`${indent}${quote_1.quoteKey(key, next)}:${indent ? " " : ""}${value}`); | ||
// Skip `key` prefix for function parser. | ||
if (function_1.USED_METHOD_KEY.has(fn)) { | ||
values.push(`${indent}${value}`); | ||
return values; | ||
} | ||
values.push(`${indent}${quote_1.quoteKey(key, next)}:${space}${value}`); | ||
return values; | ||
@@ -35,4 +48,40 @@ }, []) | ||
return `{${eol}${values}${eol}}`; | ||
} | ||
exports.objectToString = objectToString; | ||
}; | ||
/** | ||
* Stringify global variable access. | ||
*/ | ||
const globalToString = (value, space, next) => { | ||
return `Function(${next("return this")})()`; | ||
}; | ||
/** | ||
* Convert JavaScript objects into strings. | ||
*/ | ||
const OBJECT_TYPES = { | ||
"[object Array]": array_1.arrayToString, | ||
"[object Object]": rawObjectToString, | ||
"[object Error]": (error, space, next) => { | ||
return `new Error(${next(error.message)})`; | ||
}, | ||
"[object Date]": (date) => { | ||
return `new Date(${date.getTime()})`; | ||
}, | ||
"[object String]": (str, space, next) => { | ||
return `new String(${next(str.toString())})`; | ||
}, | ||
"[object Number]": (num) => { | ||
return `new Number(${num})`; | ||
}, | ||
"[object Boolean]": (bool) => { | ||
return `new Boolean(${bool})`; | ||
}, | ||
"[object Set]": (set, space, next) => { | ||
return `new Set(${next(Array.from(set))})`; | ||
}, | ||
"[object Map]": (map, space, next) => { | ||
return `new Map(${next(Array.from(map))})`; | ||
}, | ||
"[object RegExp]": String, | ||
"[object global]": globalToString, | ||
"[object Window]": globalToString | ||
}; | ||
//# sourceMappingURL=object.js.map |
@@ -1,5 +0,5 @@ | ||
import { Next } from "./types"; | ||
import { ToString } from "./types"; | ||
/** | ||
* Stringify a value recursively. | ||
*/ | ||
export declare function toString(value: any, space: string, next: Next): string | undefined; | ||
export declare const toString: ToString; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const quote_1 = require("./quote"); | ||
const array_1 = require("./array"); | ||
const object_1 = require("./object"); | ||
@@ -21,44 +20,8 @@ const function_1 = require("./function"); | ||
}, | ||
undefined: String | ||
}; | ||
/** | ||
* Stringify global variable access. | ||
*/ | ||
function globalToString(value, space, next) { | ||
return `Function(${next("return this")})()`; | ||
} | ||
/** | ||
* Convert JavaScript objects into strings. | ||
*/ | ||
const OBJECT_TYPES = { | ||
"[object Array]": array_1.arrayToString, | ||
"[object Object]": object_1.objectToString, | ||
"[object Error]": function (error, space, next) { | ||
return `new Error(${next(error.message)})`; | ||
bigint: (value, space, next) => { | ||
return `BigInt(${next(String(value))})`; | ||
}, | ||
"[object Date]": function (date) { | ||
return `new Date(${date.getTime()})`; | ||
}, | ||
"[object String]": function (str, space, next) { | ||
return `new String(${next(str.toString())})`; | ||
}, | ||
"[object Number]": function (num) { | ||
return `new Number(${num})`; | ||
}, | ||
"[object Boolean]": function (bool) { | ||
return `new Boolean(${bool})`; | ||
}, | ||
"[object Set]": function (set, space, next) { | ||
return `new Set(${next(Array.from(set))})`; | ||
}, | ||
"[object Map]": function (map, space, next) { | ||
return `new Map(${next(Array.from(map))})`; | ||
}, | ||
"[object RegExp]": String, | ||
"[object Function]": function_1.functionToString, | ||
"[object GeneratorFunction]": function_1.functionToString, | ||
"[object AsyncFunction]": function_1.functionToString, | ||
"[object AsyncGeneratorFunction]": function_1.functionToString, | ||
"[object global]": globalToString, | ||
"[object Window]": globalToString | ||
undefined: String, | ||
object: object_1.objectToString, | ||
function: function_1.functionToString | ||
}; | ||
@@ -68,21 +31,7 @@ /** | ||
*/ | ||
function toString(value, space, next) { | ||
exports.toString = (value, space, next, key) => { | ||
if (value === null) | ||
return "null"; | ||
const typeOf = typeof value; | ||
if (PRIMITIVE_TYPES.hasOwnProperty(typeOf)) { | ||
return PRIMITIVE_TYPES[typeOf](value, space, next); | ||
} | ||
// Handle buffer objects before object types (node < 6 was an object, node >= 6 is a `Uint8Array`). | ||
if (typeof Buffer === "function" && Buffer.isBuffer(value)) { | ||
return `new Buffer(${next(value.toString())})`; | ||
} | ||
// Use the internal object string to select stringify method. | ||
const toString = Object.prototype.toString.call(value); | ||
// Convert objects into strings. | ||
if (OBJECT_TYPES.hasOwnProperty(toString)) { | ||
return OBJECT_TYPES[toString](value, space, next); | ||
} | ||
} | ||
exports.toString = toString; | ||
return PRIMITIVE_TYPES[typeof value](value, space, next, key); | ||
}; | ||
//# sourceMappingURL=stringify.js.map |
@@ -8,2 +8,2 @@ /** | ||
*/ | ||
export declare type ToString = (value: any, space: string, next: Next) => string | undefined; | ||
export declare type ToString = (value: any, space: string, next: Next, key: PropertyKey | undefined) => string | undefined; |
{ | ||
"name": "javascript-stringify", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
143300
1275
2