5.0.0 • Published 1 year ago

stringify-object v5.0.0

Weekly downloads
5,966,606
License
BSD-2-Clause
Repository
github
Last release
1 year ago

stringify-object

Stringify an object/array like JSON.stringify just without all the double-quotes

Useful for when you want to get the string representation of an object in a formatted way.

It also handles circular references and lets you specify quote type.

Install

npm install stringify-object

Usage

import stringifyObject from 'stringify-object';

const object = {
	foo: 'bar',
	'arr': [1, 2, 3],
	nested: {
		hello: "world"
	}
};

const pretty = stringifyObject(object, {
	indent: '  ',
	singleQuotes: false
});

console.log(pretty);
/*
{
	foo: "bar",
	arr: [
		1,
		2,
		3
	],
	nested: {
		hello: "world"
	}
}
*/

API

stringifyObject(input, options?)

Circular references will be replaced with "[Circular]".

Object keys are only quoted when necessary, for example, {'foo-bar': true}.

input

Type: object | Array

options

Type: object

indent

Type: string\ Default: \t

Preferred indentation.

singleQuotes

Type: boolean\ Default: true

Set to false to get double-quoted strings.

filter(object, property)

Type: Function

Expected to return a boolean of whether to include the property property of the object object in the output.

transform(object, property, originalResult)

Type: Function\ Default: undefined

Expected to return a string that transforms the string that resulted from stringifying object[property]. This can be used to detect special types of objects that need to be stringified in a particular way. The transform function might return an alternate string in this case, otherwise returning the originalResult.

Here's an example that uses the transform option to mask fields named "password":

import stringifyObject from 'stringify-object';

const object = {
	user: 'becky',
	password: 'secret'
};

const pretty = stringifyObject(object, {
	transform: (object, property, originalResult) => {
		if (property === 'password') {
			return originalResult.replace(/\w/g, '*');
		}

		return originalResult;
	}
});

console.log(pretty);
/*
{
	user: 'becky',
	password: '******'
}
*/
inlineCharacterLimit

Type: number

When set, will inline values up to inlineCharacterLimit length for the sake of more terse output.

For example, given the example at the top of the README:

import stringifyObject from 'stringify-object';

const object = {
	foo: 'bar',
	'arr': [1, 2, 3],
	nested: {
		hello: "world"
	}
};

const pretty = stringifyObject(object, {
	indent: '  ',
	singleQuotes: false,
	inlineCharacterLimit: 12
});

console.log(pretty);
/*
{
	foo: "bar",
	arr: [1, 2, 3],
	nested: {
		hello: "world"
	}
}
*/

As you can see, arr was printed as a one-liner because its string was shorter than 12 characters.

workbox-buildarchetype-library@bradtech/widgets@gooddata/react-components@gooddata/sdk-model@docusaurus-azuread/mdx-loaderrestory-cliportfolio-grahqlbb-chatgenerator-jean-module@texttree/demo-bsa-reference-rclwenke-browserify-csswenke-cssifywenke-cssify-watchify@redoc/redoc-progenerator-nbgenrempl-apollo-development-tools@jttechnic/interpretergenerator-rb-jsgulp-pipelinehahnbee-test@newhorizon-tech/dd-npm-package-templatehahnbee-test-clientworkonflow-bot-client@infinitebrahmanuniverse/nolb-stringi@cipherstash/stashjs@redocly/reference-docs-libsklif-ui-kitsklif-api@everything-registry/sub-chunk-2837@pxwlab/with-katana@pxwlab/next-katana-plugin@314oner_npm/universal-components-libraryp149-tablesklif-ui@bem/sdk.bemjson-to-decl@cc-dev-kit-test/console-components-lib-publisher@compositor/x0@cobalt-engine/cobower@cisdi/code-editor@cratosw/mdx-loader@culling/remark-loader@culling/corecustome-package-amincuked-zombiecssifyd3-dagdag-4-z2decoder-api-testerdeep-diff-highlightdigs-commondiscordjs-con-selfcowlogconfig-genconfig-mancer@digital-taco/react-draftcode-testerchewerconsolemockconsole2terminal@binaryprovider/charlatandiscord.js-byconcss-to-radiumcss-to-cssinjsdeku-element-to-jsx-stringdepot-build-devpff.jsplaykit-js-hls-sondqpicker.jspickupbiz-npm-packagedocusaurus-mdx-loader-v2q-iportfolio-commonportfolio-graphql@coralproject/mdx-book@comp/md-loader@compositor/jsx-loader@compositor/lab@compositor/md-loader@component-controls/blocks@cybernaut/utilscca-scripts@cloud20x/cli@colombalink/basedui@blorajs/umi-build-dev@bluejay/sequelize-service@bluejay/service@bluejay/access-controlchocolog@davidbabel/react-element-to-jsx-string@darcytech/webpack-starter@hashnode/httpsnippetrooibos-roku-arthur@hashicorp/next-hashicorproku-asset-bundlerroku-log-bsc-pluginenb-faviconsry-go-generator@huygn/react-showroomesbehavior
5.0.0

1 year ago

4.0.1

2 years ago

4.0.0

3 years ago

3.3.0

6 years ago

3.2.2

6 years ago

3.2.1

7 years ago

3.2.0

7 years ago

3.1.0

7 years ago

3.0.0

7 years ago

2.4.0

8 years ago

2.3.1

8 years ago

2.3.0

9 years ago

2.2.0

9 years ago

2.1.0

9 years ago

2.0.0

9 years ago

1.0.1

9 years ago

1.0.0

10 years ago

0.2.1

10 years ago

0.2.0

10 years ago

0.1.8

10 years ago

0.1.7

11 years ago

0.1.6

11 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago