javascript-stringify

WebJar for javascript-stringify

License

License

MIT
Categories

Categories

JavaScript Languages
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

javascript-stringify
Last Version

Last Version

1.6.0
Release Date

Release Date

Type

Type

jar
Description

Description

javascript-stringify
WebJar for javascript-stringify
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/blakeembrey/javascript-stringify

Download javascript-stringify

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/javascript-stringify/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>javascript-stringify</artifactId>
    <version>1.6.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/javascript-stringify/
implementation 'org.webjars.npm:javascript-stringify:1.6.0'
// https://jarcasting.com/artifacts/org.webjars.npm/javascript-stringify/
implementation ("org.webjars.npm:javascript-stringify:1.6.0")
'org.webjars.npm:javascript-stringify:jar:1.6.0'
<dependency org="org.webjars.npm" name="javascript-stringify" rev="1.6.0">
  <artifact name="javascript-stringify" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='javascript-stringify', version='1.6.0')
)
libraryDependencies += "org.webjars.npm" % "javascript-stringify" % "1.6.0"
[org.webjars.npm/javascript-stringify "1.6.0"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

JavaScript Stringify

NPM version NPM downloads Build status Test coverage

Stringify is to eval as JSON.stringify is to JSON.parse.

Installation

npm install javascript-stringify --save

Buy Me A Coffee

Usage

import { stringify } from "javascript-stringify";

The API is similar JSON.stringify:

  • value The value to convert to a string
  • replacer A function that alters the behavior of the stringification process
  • space A string or number that's used to insert white space into the output for readability purposes
  • options
    • 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 as undefined

Examples

stringify({}); // "{}"
stringify(true); // "true"
stringify("foo"); // "'foo'"

stringify({ x: 5, y: 6 }); // "{x:5,y:6}"
stringify([1, 2, 3, "string"]); // "[1,2,3,'string']"

stringify({ a: { b: { c: 1 } } }, null, null, { maxDepth: 2 }); // "{a:{b:{}}}"

/**
 * Invalid key names are automatically stringified.
 */

stringify({ "some-key": 10 }); // "{'some-key':10}"

/**
 * Some object types and values can remain identical.
 */

stringify([/.+/gi, new Number(10), new Date()]); // "[/.+/gi,new Number(10),new Date(1406623295732)]"

/**
 * Unknown or circular references are removed.
 */

var obj = { x: 10 };
obj.circular = obj;

stringify(obj); // "{x:10}"
stringify(obj, null, null, { references: true }); // "(function(){var x={x:10};x.circular=x;return x;}())"

/**
 * Specify indentation - just like `JSON.stringify`.
 */

stringify({ a: 2 }, null, " "); // "{\n a: 2\n}"
stringify({ uno: 1, dos: 2 }, null, "\t"); // "{\n\tuno: 1,\n\tdos: 2\n}"

/**
 * Add custom replacer behaviour - like double quoted strings.
 */

stringify(["test", "string"], function(value, indent, stringify) {
  if (typeof value === "string") {
    return '"' + value.replace(/"/g, '\\"') + '"';
  }

  return stringify(value);
});
//=> '["test","string"]'

Formatting

You can use your own code formatter on the result of javascript-stringify. Here is an example using eslint:

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

MIT

Versions

Version
1.6.0