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