source: frontend/node_modules/stringify-object/readme.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.7 KB
Line 
1# stringify-object [![Build Status](https://secure.travis-ci.org/yeoman/stringify-object.svg?branch=master)](http://travis-ci.org/yeoman/stringify-object)
2
3> Stringify an object/array like JSON.stringify just without all the double-quotes
4
5Useful for when you want to get the string representation of an object in a formatted way.
6
7It also handles circular references and lets you specify quote type.
8
9
10## Install
11
12```
13$ npm install stringify-object
14```
15
16
17## Usage
18
19```js
20const stringifyObject = require('stringify-object');
21
22const obj = {
23 foo: 'bar',
24 'arr': [1, 2, 3],
25 nested: { hello: "world" }
26};
27
28const pretty = stringifyObject(obj, {
29 indent: ' ',
30 singleQuotes: false
31});
32
33console.log(pretty);
34/*
35{
36 foo: "bar",
37 arr: [
38 1,
39 2,
40 3
41 ],
42 nested: {
43 hello: "world"
44 }
45}
46*/
47```
48
49
50## API
51
52### stringifyObject(input, [options])
53
54Circular references will be replaced with `"[Circular]"`.
55
56#### input
57
58Type: `Object` `Array`
59
60#### options
61
62##### indent
63
64Type: `string`<br>
65Default: `\t`
66
67Preferred indentation.
68
69##### singleQuotes
70
71Type: `boolean`<br>
72Default: `true`
73
74Set to false to get double-quoted strings.
75
76##### filter(obj, prop)
77
78Type: `Function`
79
80Expected to return a `boolean` of whether to include the property `prop` of the object `obj` in the output.
81
82##### transform(obj, prop, originalResult)
83
84Type: `Function`<br>
85Default: `undefined`
86
87Expected to return a `string` that transforms the string that resulted from stringifying `obj[prop]`. 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`.
88
89Here's an example that uses the `transform` option to mask fields named "password":
90
91```js
92const obj = {
93 user: 'becky',
94 password: 'secret'
95}
96
97const pretty = stringifyObject(obj, {
98 transform: (obj, prop, originalResult) => {
99 if (prop === 'password') {
100 return originalResult.replace(/\w/g, '*');
101 } else {
102 return originalResult;
103 }
104 }
105});
106
107console.log(pretty);
108/*
109{
110 user: 'becky',
111 password: '******'
112}
113*/
114```
115
116
117##### inlineCharacterLimit
118
119Type: `number`
120
121When set, will inline values up to `inlineCharacterLimit` length for the sake of more terse output.
122
123For example, given the example at the top of the README:
124
125```js
126const obj = {
127 foo: 'bar',
128 'arr': [1, 2, 3],
129 nested: { hello: "world" }
130};
131
132const pretty = stringifyObject(obj, {
133 indent: ' ',
134 singleQuotes: false,
135 inlineCharacterLimit: 12
136});
137
138console.log(pretty);
139/*
140{
141 foo: "bar",
142 arr: [1, 2, 3],
143 nested: {
144 hello: "world"
145 }
146}
147*/
148```
149
150As you can see, `arr` was printed as a one-liner because its string was shorter than 12 characters.
151
152
153## License
154
155BSD-2-Clause © Yeoman team
Note: See TracBrowser for help on using the repository browser.