| 1 | # stringify-object [](http://travis-ci.org/yeoman/stringify-object)
|
|---|
| 2 |
|
|---|
| 3 | > Stringify an object/array like JSON.stringify just without all the double-quotes
|
|---|
| 4 |
|
|---|
| 5 | Useful for when you want to get the string representation of an object in a formatted way.
|
|---|
| 6 |
|
|---|
| 7 | It 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
|
|---|
| 20 | const stringifyObject = require('stringify-object');
|
|---|
| 21 |
|
|---|
| 22 | const obj = {
|
|---|
| 23 | foo: 'bar',
|
|---|
| 24 | 'arr': [1, 2, 3],
|
|---|
| 25 | nested: { hello: "world" }
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | const pretty = stringifyObject(obj, {
|
|---|
| 29 | indent: ' ',
|
|---|
| 30 | singleQuotes: false
|
|---|
| 31 | });
|
|---|
| 32 |
|
|---|
| 33 | console.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 |
|
|---|
| 54 | Circular references will be replaced with `"[Circular]"`.
|
|---|
| 55 |
|
|---|
| 56 | #### input
|
|---|
| 57 |
|
|---|
| 58 | Type: `Object` `Array`
|
|---|
| 59 |
|
|---|
| 60 | #### options
|
|---|
| 61 |
|
|---|
| 62 | ##### indent
|
|---|
| 63 |
|
|---|
| 64 | Type: `string`<br>
|
|---|
| 65 | Default: `\t`
|
|---|
| 66 |
|
|---|
| 67 | Preferred indentation.
|
|---|
| 68 |
|
|---|
| 69 | ##### singleQuotes
|
|---|
| 70 |
|
|---|
| 71 | Type: `boolean`<br>
|
|---|
| 72 | Default: `true`
|
|---|
| 73 |
|
|---|
| 74 | Set to false to get double-quoted strings.
|
|---|
| 75 |
|
|---|
| 76 | ##### filter(obj, prop)
|
|---|
| 77 |
|
|---|
| 78 | Type: `Function`
|
|---|
| 79 |
|
|---|
| 80 | Expected 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 |
|
|---|
| 84 | Type: `Function`<br>
|
|---|
| 85 | Default: `undefined`
|
|---|
| 86 |
|
|---|
| 87 | Expected 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 |
|
|---|
| 89 | Here's an example that uses the `transform` option to mask fields named "password":
|
|---|
| 90 |
|
|---|
| 91 | ```js
|
|---|
| 92 | const obj = {
|
|---|
| 93 | user: 'becky',
|
|---|
| 94 | password: 'secret'
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | const 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 |
|
|---|
| 107 | console.log(pretty);
|
|---|
| 108 | /*
|
|---|
| 109 | {
|
|---|
| 110 | user: 'becky',
|
|---|
| 111 | password: '******'
|
|---|
| 112 | }
|
|---|
| 113 | */
|
|---|
| 114 | ```
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 | ##### inlineCharacterLimit
|
|---|
| 118 |
|
|---|
| 119 | Type: `number`
|
|---|
| 120 |
|
|---|
| 121 | When set, will inline values up to `inlineCharacterLimit` length for the sake of more terse output.
|
|---|
| 122 |
|
|---|
| 123 | For example, given the example at the top of the README:
|
|---|
| 124 |
|
|---|
| 125 | ```js
|
|---|
| 126 | const obj = {
|
|---|
| 127 | foo: 'bar',
|
|---|
| 128 | 'arr': [1, 2, 3],
|
|---|
| 129 | nested: { hello: "world" }
|
|---|
| 130 | };
|
|---|
| 131 |
|
|---|
| 132 | const pretty = stringifyObject(obj, {
|
|---|
| 133 | indent: ' ',
|
|---|
| 134 | singleQuotes: false,
|
|---|
| 135 | inlineCharacterLimit: 12
|
|---|
| 136 | });
|
|---|
| 137 |
|
|---|
| 138 | console.log(pretty);
|
|---|
| 139 | /*
|
|---|
| 140 | {
|
|---|
| 141 | foo: "bar",
|
|---|
| 142 | arr: [1, 2, 3],
|
|---|
| 143 | nested: {
|
|---|
| 144 | hello: "world"
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 | */
|
|---|
| 148 | ```
|
|---|
| 149 |
|
|---|
| 150 | As you can see, `arr` was printed as a one-liner because its string was shorter than 12 characters.
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 | ## License
|
|---|
| 154 |
|
|---|
| 155 | BSD-2-Clause © Yeoman team
|
|---|