6befc84a805d2a14f0a087a2fddedf233003801de924b3969a281f7c5b07168f47aa4637828d3650aae160a52cd7fbc9c0e04c92bb2ef852a56579e134abc2 2.7 KB

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