You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
4.1 KiB

3 months ago
  1. # flat [![Build Status](https://secure.travis-ci.org/hughsk/flat.png?branch=master)](http://travis-ci.org/hughsk/flat)
  2. Take a nested Javascript object and flatten it, or unflatten an object with
  3. delimited keys.
  4. ## Installation
  5. ``` bash
  6. $ npm install flat
  7. ```
  8. ## Methods
  9. ### flatten(original, options)
  10. Flattens the object - it'll return an object one level deep, regardless of how
  11. nested the original object was:
  12. ``` javascript
  13. var flatten = require('flat')
  14. flatten({
  15. key1: {
  16. keyA: 'valueI'
  17. },
  18. key2: {
  19. keyB: 'valueII'
  20. },
  21. key3: { a: { b: { c: 2 } } }
  22. })
  23. // {
  24. // 'key1.keyA': 'valueI',
  25. // 'key2.keyB': 'valueII',
  26. // 'key3.a.b.c': 2
  27. // }
  28. ```
  29. ### unflatten(original, options)
  30. Flattening is reversible too, you can call `flatten.unflatten()` on an object:
  31. ``` javascript
  32. var unflatten = require('flat').unflatten
  33. unflatten({
  34. 'three.levels.deep': 42,
  35. 'three.levels': {
  36. nested: true
  37. }
  38. })
  39. // {
  40. // three: {
  41. // levels: {
  42. // deep: 42,
  43. // nested: true
  44. // }
  45. // }
  46. // }
  47. ```
  48. ## Options
  49. ### delimiter
  50. Use a custom delimiter for (un)flattening your objects, instead of `.`.
  51. ### safe
  52. When enabled, both `flat` and `unflatten` will preserve arrays and their
  53. contents. This is disabled by default.
  54. ``` javascript
  55. var flatten = require('flat')
  56. flatten({
  57. this: [
  58. { contains: 'arrays' },
  59. { preserving: {
  60. them: 'for you'
  61. }}
  62. ]
  63. }, {
  64. safe: true
  65. })
  66. // {
  67. // 'this': [
  68. // { contains: 'arrays' },
  69. // { preserving: {
  70. // them: 'for you'
  71. // }}
  72. // ]
  73. // }
  74. ```
  75. ### object
  76. When enabled, arrays will not be created automatically when calling unflatten, like so:
  77. ``` javascript
  78. unflatten({
  79. 'hello.you.0': 'ipsum',
  80. 'hello.you.1': 'lorem',
  81. 'hello.other.world': 'foo'
  82. }, { object: true })
  83. // hello: {
  84. // you: {
  85. // 0: 'ipsum',
  86. // 1: 'lorem',
  87. // },
  88. // other: { world: 'foo' }
  89. // }
  90. ```
  91. ### overwrite
  92. When enabled, existing keys in the unflattened object may be overwritten if they cannot hold a newly encountered nested value:
  93. ```javascript
  94. unflatten({
  95. 'TRAVIS': 'true',
  96. 'TRAVIS.DIR': '/home/travis/build/kvz/environmental'
  97. }, { overwrite: true })
  98. // TRAVIS: {
  99. // DIR: '/home/travis/build/kvz/environmental'
  100. // }
  101. ```
  102. Without `overwrite` set to `true`, the `TRAVIS` key would already have been set to a string, thus could not accept the nested `DIR` element.
  103. This only makes sense on ordered arrays, and since we're overwriting data, should be used with care.
  104. ### maxDepth
  105. Maximum number of nested objects to flatten.
  106. ``` javascript
  107. var flatten = require('flat')
  108. flatten({
  109. key1: {
  110. keyA: 'valueI'
  111. },
  112. key2: {
  113. keyB: 'valueII'
  114. },
  115. key3: { a: { b: { c: 2 } } }
  116. }, { maxDepth: 2 })
  117. // {
  118. // 'key1.keyA': 'valueI',
  119. // 'key2.keyB': 'valueII',
  120. // 'key3.a': { b: { c: 2 } }
  121. // }
  122. ```
  123. ### transformKey
  124. Transform each part of a flat key before and after flattening.
  125. ```javascript
  126. var flatten = require('flat')
  127. var unflatten = require('flat').unflatten
  128. flatten({
  129. key1: {
  130. keyA: 'valueI'
  131. },
  132. key2: {
  133. keyB: 'valueII'
  134. },
  135. key3: { a: { b: { c: 2 } } }
  136. }, {
  137. transformKey: function(key){
  138. return '__' + key + '__';
  139. }
  140. })
  141. // {
  142. // '__key1__.__keyA__': 'valueI',
  143. // '__key2__.__keyB__': 'valueII',
  144. // '__key3__.__a__.__b__.__c__': 2
  145. // }
  146. unflatten({
  147. '__key1__.__keyA__': 'valueI',
  148. '__key2__.__keyB__': 'valueII',
  149. '__key3__.__a__.__b__.__c__': 2
  150. }, {
  151. transformKey: function(key){
  152. return key.substring(2, key.length - 2)
  153. }
  154. })
  155. // {
  156. // key1: {
  157. // keyA: 'valueI'
  158. // },
  159. // key2: {
  160. // keyB: 'valueII'
  161. // },
  162. // key3: { a: { b: { c: 2 } } }
  163. // }
  164. ```
  165. ## Command Line Usage
  166. `flat` is also available as a command line tool. You can run it with
  167. [`npx`](https://ghub.io/npx):
  168. ```sh
  169. npx flat foo.json
  170. ```
  171. Or install the `flat` command globally:
  172. ```sh
  173. npm i -g flat && flat foo.json
  174. ```
  175. Accepts a filename as an argument:
  176. ```sh
  177. flat foo.json
  178. ```
  179. Also accepts JSON on stdin:
  180. ```sh
  181. cat foo.json | flat
  182. ```