5.0.0 • Published 2 years ago

stringify-object v5.0.0

Weekly downloads
5,966,606
License
BSD-2-Clause
Repository
github
Last release
2 years 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@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-pluginp149-tablesklif-uitailwind-vector-effecttailwindcss-language-servicetailwind-monaco-integrationwodax-umi-build-devwmata-clientworkerpack-cliworkbox-clix-zerox0v5saasify-codegenshadcxshadcnshadcn-ngserverless-securerequirejs-config-filerequirejs-config-mergeresponsis-gantt-task-reactreactofy-css-libraryremark-mdx-metadatarn-circular-chartreactotron-core-uireactotron-approku-asset-bundlerroku-log-bsc-pluginteapackage-tatespoorman297staple-serverspellaphonetestd3svavatarvite-plugin-contentstackvt-playgroundvue-compmentvite-docusaurusvenus-generatorvcloudcam-playkit-js-hlsvue-dev-clonezeroheight-clireal-service-providersractivoremitan-nodelib-errorlogzio-node-debuglint-committedlint-staged-fomatter-loglint-staged-offlinelint-staged-outsidelaoitdev-core-pluginshapi-waypointergoodteditor-web-componentsjest-transformer-mdjest-transformer-mdxjest-prosemirrorjaywalkerjsedajson-2-promjson2expressjson-schema-yup-transformeri18n-nextiotor-lmisv-rx-radium-loadermcs-basemicroend-componentlitepie-datepicker-gabemokihi-jsmonaco-tailwindcssmpd-parser-1mpp_design_sysjulien-easy-modal
5.0.0

2 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.3.0

6 years ago

3.2.2

7 years ago

3.2.1

7 years ago

3.2.0

8 years ago

3.1.0

8 years ago

3.0.0

8 years ago

2.4.0

9 years ago

2.3.1

9 years ago

2.3.0

9 years ago

2.2.0

9 years ago

2.1.0

9 years ago

2.0.0

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.2.1

11 years ago

0.2.0

11 years ago

0.1.8

11 years ago

0.1.7

11 years ago

0.1.6

11 years ago

0.1.5

11 years ago

0.1.4

12 years ago

0.1.3

12 years ago

0.1.2

12 years ago

0.1.1

12 years ago

0.1.0

12 years ago