javascript-stringify
Advanced tools
Comparing version 2.0.1 to 2.1.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.arrayToString = void 0; | ||
/** | ||
* Stringify an array of values. | ||
*/ | ||
exports.arrayToString = (array, space, next) => { | ||
const arrayToString = (array, space, next) => { | ||
// Map array values to their stringified values with correct indentation. | ||
@@ -19,2 +20,3 @@ const values = array | ||
}; | ||
exports.arrayToString = arrayToString; | ||
//# sourceMappingURL=array.js.map |
@@ -11,3 +11,3 @@ import { Next, ToString } from "./types"; | ||
*/ | ||
export declare const USED_METHOD_KEY: WeakSet<Function>; | ||
export declare const USED_METHOD_KEY: WeakSet<(...args: unknown[]) => unknown>; | ||
/** | ||
@@ -25,3 +25,3 @@ * Stringify a function. | ||
export declare class FunctionParser { | ||
fn: Function; | ||
fn: (...args: unknown[]) => unknown; | ||
indent: string; | ||
@@ -37,3 +37,3 @@ next: Next; | ||
hadKeyword: boolean; | ||
constructor(fn: Function, indent: string, next: Next, key?: string | undefined); | ||
constructor(fn: (...args: unknown[]) => unknown, indent: string, next: Next, key?: string | undefined); | ||
stringify(): string; | ||
@@ -53,4 +53,2 @@ getPrefix(): string; | ||
* based on whether or not a `function` keyword is consumed. | ||
* | ||
* @return {boolean} | ||
*/ | ||
@@ -67,5 +65,5 @@ tryParsePrefixTokens(): boolean; | ||
* simple as it can be. As a consequence, some things that are one token in | ||
* JavaScript, like decimal number literals or most multicharacter operators | ||
* JavaScript, like decimal number literals or most multi-character operators | ||
* like '&&', are split into more than one token here. However, awareness of | ||
* some multicharacter sequences like '=>' is necessary, so we match the few | ||
* some multi-character sequences like '=>' is necessary, so we match the few | ||
* of them that we care about.) | ||
@@ -72,0 +70,0 @@ */ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.FunctionParser = exports.dedentFunction = exports.functionToString = exports.USED_METHOD_KEY = void 0; | ||
const quote_1 = require("./quote"); | ||
@@ -11,3 +12,3 @@ /** | ||
/* Empty. */ | ||
} | ||
}, | ||
}[" "] | ||
@@ -20,3 +21,3 @@ .toString() | ||
AsyncFunction: "async function ", | ||
AsyncGeneratorFunction: "async function* " | ||
AsyncGeneratorFunction: "async function* ", | ||
}; | ||
@@ -27,3 +28,3 @@ const METHOD_PREFIXES = { | ||
AsyncFunction: "async ", | ||
AsyncGeneratorFunction: "async *" | ||
AsyncGeneratorFunction: "async *", | ||
}; | ||
@@ -39,3 +40,3 @@ const TOKENS_PRECEDING_REGEXPS = new Set(("case delete else in instanceof new return throw typeof void " + | ||
*/ | ||
exports.functionToString = (fn, space, next, key) => { | ||
const functionToString = (fn, space, next, key) => { | ||
const name = typeof key === "string" ? key : undefined; | ||
@@ -47,2 +48,3 @@ // Track in function parser for object stringify to avoid duplicate output. | ||
}; | ||
exports.functionToString = functionToString; | ||
/** | ||
@@ -126,3 +128,2 @@ * Rewrite a stringified function to remove initial indentation. | ||
} | ||
// tslint:disable-next-line no-switch-case-fall-through | ||
case "()": | ||
@@ -133,3 +134,2 @@ if (this.fnString.substr(this.pos, 2) === "=>") { | ||
this.pos = offset; | ||
// tslint:disable-next-line no-switch-case-fall-through | ||
case '"': | ||
@@ -174,4 +174,2 @@ case "'": | ||
* based on whether or not a `function` keyword is consumed. | ||
* | ||
* @return {boolean} | ||
*/ | ||
@@ -186,3 +184,2 @@ tryParsePrefixTokens() { | ||
posPrev = this.pos; | ||
// tslint:disable-next-line no-switch-case-fall-through | ||
case "Function": | ||
@@ -199,3 +196,2 @@ if (this.consumeSyntax() === "function") { | ||
return false; | ||
// tslint:disable-next-line no-switch-case-fall-through | ||
case "GeneratorFunction": | ||
@@ -219,5 +215,5 @@ let token = this.consumeSyntax(); | ||
* simple as it can be. As a consequence, some things that are one token in | ||
* JavaScript, like decimal number literals or most multicharacter operators | ||
* JavaScript, like decimal number literals or most multi-character operators | ||
* like '&&', are split into more than one token here. However, awareness of | ||
* some multicharacter sequences like '=>' is necessary, so we match the few | ||
* some multi-character sequences like '=>' is necessary, so we match the few | ||
* of them that we care about.) | ||
@@ -224,0 +220,0 @@ */ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.stringify = void 0; | ||
const stringify_1 = require("./stringify"); | ||
@@ -19,3 +20,3 @@ const quote_1 = require("./quote"); | ||
let valueCount = 0; | ||
const { maxDepth = 100, references = false, skipUndefinedProperties = false, maxValues = 100000 } = options; | ||
const { maxDepth = 100, references = false, skipUndefinedProperties = false, maxValues = 100000, } = options; | ||
// Wrap replacer function to support falling back on supported stringify. | ||
@@ -48,3 +49,4 @@ const valueToString = replacerToString(replacer); | ||
unpack.set(path.slice(1), tracking.get(value)); | ||
return; // Avoid serializing referenced nodes on an expression. | ||
// Use `undefined` as temporaray stand-in for referenced nodes | ||
return valueToString(undefined, space, onNext, key); | ||
} | ||
@@ -51,0 +53,0 @@ // Track encountered nodes. |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.objectToString = void 0; | ||
const quote_1 = require("./quote"); | ||
@@ -9,6 +10,11 @@ const function_1 = require("./function"); | ||
*/ | ||
exports.objectToString = (value, space, next, key) => { | ||
const objectToString = (value, space, next, key) => { | ||
// Support buffer in all environments. | ||
if (typeof Buffer === "function" && Buffer.isBuffer(value)) { | ||
return `new Buffer(${next(value.toString())})`; | ||
return `Buffer.from(${next(value.toString("base64"))}, 'base64')`; | ||
} | ||
// Support `global` under test environments that don't print `[object global]`. | ||
if (typeof global === "object" && value === global) { | ||
return globalToString(value, space, next, key); | ||
} | ||
// Use the internal object string to select stringify method. | ||
@@ -18,6 +24,7 @@ const toString = OBJECT_TYPES[Object.prototype.toString.call(value)]; | ||
}; | ||
exports.objectToString = objectToString; | ||
/** | ||
* Stringify an object of keys and values. | ||
*/ | ||
const rawObjectToString = (obj, indent, next) => { | ||
const rawObjectToString = (obj, indent, next, key) => { | ||
const eol = indent ? "\n" : ""; | ||
@@ -84,4 +91,4 @@ const space = indent ? " " : ""; | ||
"[object global]": globalToString, | ||
"[object Window]": globalToString | ||
"[object Window]": globalToString, | ||
}; | ||
//# sourceMappingURL=object.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.stringifyPath = exports.quoteKey = exports.isValidVariableName = exports.IS_VALID_IDENTIFIER = exports.quoteString = void 0; | ||
/** | ||
@@ -21,3 +22,3 @@ * Match all characters that need to be escaped in a string. Modified from | ||
['"', '\\"'], | ||
["\\", "\\\\"] | ||
["\\", "\\\\"], | ||
]); | ||
@@ -24,0 +25,0 @@ /** |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.toString = void 0; | ||
const quote_1 = require("./quote"); | ||
@@ -25,3 +26,3 @@ const object_1 = require("./object"); | ||
object: object_1.objectToString, | ||
function: function_1.functionToString | ||
function: function_1.functionToString, | ||
}; | ||
@@ -31,3 +32,3 @@ /** | ||
*/ | ||
exports.toString = (value, space, next, key) => { | ||
const toString = (value, space, next, key) => { | ||
if (value === null) | ||
@@ -37,2 +38,3 @@ return "null"; | ||
}; | ||
exports.toString = toString; | ||
//# sourceMappingURL=stringify.js.map |
{ | ||
"name": "javascript-stringify", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"description": "Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`", | ||
"license": "MIT", | ||
"repository": "https://github.com/blakeembrey/javascript-stringify.git", | ||
"author": { | ||
"name": "Blake Embrey", | ||
"email": "hello@blakeembrey.com", | ||
"url": "http://blakeembrey.me" | ||
}, | ||
"homepage": "https://github.com/blakeembrey/javascript-stringify", | ||
"bugs": { | ||
"url": "https://github.com/blakeembrey/javascript-stringify/issues" | ||
}, | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"format": "ts-scripts format", | ||
"lint": "ts-scripts lint", | ||
"prepare": "ts-scripts install && ts-scripts build", | ||
"specs": "ts-scripts specs", | ||
"test": "ts-scripts test" | ||
}, | ||
"files": [ | ||
"dist/" | ||
], | ||
"scripts": { | ||
"prettier": "prettier --write", | ||
"format": "npm run prettier -- \"{.,src/**}/*.{js,ts,json,md,yml,css}\"", | ||
"lint": "tslint \"src/**/*.ts\" --project tsconfig.json", | ||
"build": "rimraf dist/ && tsc", | ||
"specs": "jest --coverage", | ||
"test": "npm run lint && npm run build && npm run specs", | ||
"prepare": "npm run build" | ||
}, | ||
"repository": "https://github.com/blakeembrey/javascript-stringify.git", | ||
"keywords": [ | ||
@@ -28,35 +38,4 @@ "stringify", | ||
], | ||
"author": { | ||
"name": "Blake Embrey", | ||
"email": "hello@blakeembrey.com", | ||
"url": "http://blakeembrey.me" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/blakeembrey/javascript-stringify/issues" | ||
}, | ||
"homepage": "https://github.com/blakeembrey/javascript-stringify", | ||
"jest": { | ||
"roots": [ | ||
"<rootDir>/src/" | ||
], | ||
"transform": { | ||
"\\.tsx?$": "ts-jest" | ||
} | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"*.{js,ts,json,md,yml,css}": [ | ||
"npm run prettier", | ||
"git add" | ||
] | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"devDependencies": { | ||
"@borderless/ts-scripts": "^0.4.1", | ||
"@types/jest": "^24.0.9", | ||
@@ -66,14 +45,9 @@ "@types/node": "^11.10.4", | ||
"fast-check": "^1.12.0", | ||
"husky": "^1.3.1", | ||
"jest": "^24.0.0", | ||
"lint-staged": "^8.1.1", | ||
"prettier": "^1.16.1", | ||
"rimraf": "^2.5.4", | ||
"semver": "^5.6.0", | ||
"ts-jest": "^24.0.0", | ||
"tslint": "^5.0.0", | ||
"tslint-config-prettier": "^1.17.0", | ||
"tslint-config-standard": "^8.0.0", | ||
"typescript": "^3.3.1" | ||
"typescript": "^4.2.4" | ||
}, | ||
"types": "dist/index.d.ts", | ||
"ts-scripts": { | ||
"project": "tsconfig.build.json" | ||
} | ||
} |
@@ -5,4 +5,4 @@ # JavaScript Stringify | ||
[![NPM downloads][downloads-image]][downloads-url] | ||
[![Build status][travis-image]][travis-url] | ||
[![Test coverage][coveralls-image]][coveralls-url] | ||
[![Build status][build-image]][build-url] | ||
[![Build coverage][coverage-image]][coverage-url] | ||
@@ -17,6 +17,2 @@ > Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`. | ||
--- | ||
<a href="https://www.buymeacoffee.com/blakeembrey" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" ></a> | ||
## Usage | ||
@@ -54,3 +50,2 @@ | ||
*/ | ||
stringify({ "some-key": 10 }); // "{'some-key':10}" | ||
@@ -61,3 +56,2 @@ | ||
*/ | ||
stringify([/.+/gi, new Number(10), new Date()]); // "[/.+/gi,new Number(10),new Date(1406623295732)]" | ||
@@ -68,3 +62,2 @@ | ||
*/ | ||
var obj = { x: 10 }; | ||
@@ -79,3 +72,2 @@ obj.circular = obj; | ||
*/ | ||
stringify({ a: 2 }, null, " "); // "{\n a: 2\n}" | ||
@@ -87,4 +79,3 @@ stringify({ uno: 1, dos: 2 }, null, "\t"); // "{\n\tuno: 1,\n\tdos: 2\n}" | ||
*/ | ||
stringify(["test", "string"], function(value, indent, stringify) { | ||
stringify(["test", "string"], function (value, indent, stringify) { | ||
if (typeof value === "string") { | ||
@@ -99,2 +90,24 @@ return '"' + value.replace(/"/g, '\\"') + '"'; | ||
## Formatting | ||
You can use your own code formatter on the result of `javascript-stringify`. Here is an example using [eslint](https://www.npmjs.com/package/eslint): | ||
```javascript | ||
const { CLIEngine } = require("eslint"); | ||
const { stringify } = require("javascript-stringify"); | ||
const { APP_ROOT_PATH, ESLINTRC_FILE_PATH } = require("./constants"); | ||
const ESLINT_CLI = new CLIEngine({ | ||
fix: true, | ||
cwd: APP_ROOT_PATH, | ||
configFile: ESLINTRC_FILE_PATH, | ||
}); | ||
module.exports = (objectToStringify) => { | ||
return ESLINT_CLI.executeOnText(stringify(objectToStringify)).results[0] | ||
.output; | ||
}; | ||
``` | ||
## License | ||
@@ -104,9 +117,9 @@ | ||
[npm-image]: https://img.shields.io/npm/v/javascript-stringify.svg?style=flat | ||
[npm-image]: https://img.shields.io/npm/v/javascript-stringify | ||
[npm-url]: https://npmjs.org/package/javascript-stringify | ||
[downloads-image]: https://img.shields.io/npm/dm/javascript-stringify.svg?style=flat | ||
[downloads-image]: https://img.shields.io/npm/dm/javascript-stringify | ||
[downloads-url]: https://npmjs.org/package/javascript-stringify | ||
[travis-image]: https://img.shields.io/travis/blakeembrey/javascript-stringify.svg?style=flat | ||
[travis-url]: https://travis-ci.org/blakeembrey/javascript-stringify | ||
[coveralls-image]: https://img.shields.io/coveralls/blakeembrey/javascript-stringify.svg?style=flat | ||
[coveralls-url]: https://coveralls.io/r/blakeembrey/javascript-stringify?branch=master | ||
[build-image]: https://img.shields.io/github/workflow/status/blakeembrey/javascript-stringify/CI/main | ||
[build-url]: https://github.com/blakeembrey/javascript-stringify/actions/workflows/ci.yml?query=branch%3Amain | ||
[coverage-image]: https://img.shields.io/codecov/c/gh/blakeembrey/javascript-stringify | ||
[coverage-url]: https://codecov.io/gh/blakeembrey/javascript-stringify |
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
7
117
0
71857
24
759