Stringify is to `eval` as `JSON.stringify` is to `JSON.parse`
Installations
npm install javascript-stringify
Releases
Use `Buffer.from`
Published on 14 Apr 2021
Custom Function Properties
Published on 05 Nov 2019
ESNext Function Syntaxes + TypeScript
Published on 06 Mar 2019
Handle `Error`, `Map` and `Set`
Published on 23 Feb 2017
Skip Undefined Properties
Published on 02 Nov 2016
Max Values
Published on 19 Aug 2016
Developer
blakeembrey
Developer Guide
Module System
CommonJS
Min. Node Version
Typescript Support
Yes
Node Version
15.14.0
NPM Version
7.9.0
Statistics
140 Stars
81 Commits
16 Forks
5 Watching
1 Branches
8 Contributors
Updated on 29 Sept 2024
Languages
TypeScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
400,043,117
Last day
-4.1%
403,676
Compared to previous day
Last week
2.7%
2,294,433
Compared to previous week
Last month
12.4%
9,416,664
Compared to previous month
Last year
3.5%
97,061,238
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
JavaScript Stringify
Stringify is to
eval
asJSON.stringify
is toJSON.parse
.
Installation
npm install javascript-stringify --save
Usage
1import { stringify } from "javascript-stringify";
The API is similar JSON.stringify
:
value
The value to convert to a stringreplacer
A function that alters the behavior of the stringification processspace
A string or number that's used to insert white space into the output for readability purposesoptions
- maxDepth (number, default: 100) The maximum depth of values to stringify
- maxValues (number, default: 100000) The maximum number of values to stringify
- references (boolean, default: false) Restore circular/repeated references in the object (uses IIFE)
- skipUndefinedProperties (boolean, default: false) Omits
undefined
properties instead of restoring asundefined
Examples
1stringify({}); // "{}" 2stringify(true); // "true" 3stringify("foo"); // "'foo'" 4 5stringify({ x: 5, y: 6 }); // "{x:5,y:6}" 6stringify([1, 2, 3, "string"]); // "[1,2,3,'string']" 7 8stringify({ a: { b: { c: 1 } } }, null, null, { maxDepth: 2 }); // "{a:{b:{}}}" 9 10/** 11 * Invalid key names are automatically stringified. 12 */ 13stringify({ "some-key": 10 }); // "{'some-key':10}" 14 15/** 16 * Some object types and values can remain identical. 17 */ 18stringify([/.+/gi, new Number(10), new Date()]); // "[/.+/gi,new Number(10),new Date(1406623295732)]" 19 20/** 21 * Unknown or circular references are removed. 22 */ 23var obj = { x: 10 }; 24obj.circular = obj; 25 26stringify(obj); // "{x:10}" 27stringify(obj, null, null, { references: true }); // "(function(){var x={x:10};x.circular=x;return x;}())" 28 29/** 30 * Specify indentation - just like `JSON.stringify`. 31 */ 32stringify({ a: 2 }, null, " "); // "{\n a: 2\n}" 33stringify({ uno: 1, dos: 2 }, null, "\t"); // "{\n\tuno: 1,\n\tdos: 2\n}" 34 35/** 36 * Add custom replacer behaviour - like double quoted strings. 37 */ 38stringify(["test", "string"], function (value, indent, stringify) { 39 if (typeof value === "string") { 40 return '"' + value.replace(/"/g, '\\"') + '"'; 41 } 42 43 return stringify(value); 44}); 45//=> '["test","string"]'
Formatting
You can use your own code formatter on the result of javascript-stringify
. Here is an example using eslint:
1const { CLIEngine } = require("eslint"); 2const { stringify } = require("javascript-stringify"); 3 4const { APP_ROOT_PATH, ESLINTRC_FILE_PATH } = require("./constants"); 5 6const ESLINT_CLI = new CLIEngine({ 7 fix: true, 8 cwd: APP_ROOT_PATH, 9 configFile: ESLINTRC_FILE_PATH, 10}); 11 12module.exports = (objectToStringify) => { 13 return ESLINT_CLI.executeOnText(stringify(objectToStringify)).results[0] 14 .output; 15};
License
MIT
No vulnerabilities found.
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Info: Found linked content: SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1
- Info: Found text in security policy: SECURITY.md:1
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
project is fuzzed
Details
- Info: TypeScriptPropertyBasedTesting integration found: src/index.spec.ts:1
Reason
Found 6/25 approved changesets -- score normalized to 2
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:17: update your workflow using https://app.stepsecurity.io/secureworkflow/blakeembrey/javascript-stringify/ci.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/blakeembrey/javascript-stringify/ci.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/blakeembrey/javascript-stringify/ci.yml/main?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:30: update your workflow using https://app.stepsecurity.io/secureworkflow/blakeembrey/javascript-stringify/ci.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yml:28
- Info: 0 out of 3 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 third-party GitHubAction dependencies pinned
- Info: 1 out of 2 npmCommand dependencies pinned
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'main'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 11 are checked with a SAST tool
Reason
21 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
- Warn: Project is vulnerable to: GHSA-j8xg-fqg3-53r7
- Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-m95q-7qp3-xv42
Score
4.1
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More