1 | # flat [](https://github.com/hughsk/flat/actions/workflows/main.yml)
|
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 | import { flatten } from '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 `unflatten` on an object:
|
42 |
|
43 | ``` javascript
|
44 | import { unflatten } from 'flat'
|
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 | import { flatten } from '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 | import { flatten } from '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 | import { flatten, unflatten } from 'flat'
|
168 |
|
169 | flatten({
|
170 | key1: {
|
171 | keyA: 'valueI'
|
172 | },
|
173 | key2: {
|
174 | keyB: 'valueII'
|
175 | },
|
176 | key3: { a: { b: { c: 2 } } }
|
177 | }, {
|
178 | transformKey: function(key){
|
179 | return '__' + key + '__';
|
180 | }
|
181 | })
|
182 |
|
183 | // {
|
184 | // '__key1__.__keyA__': 'valueI',
|
185 | // '__key2__.__keyB__': 'valueII',
|
186 | // '__key3__.__a__.__b__.__c__': 2
|
187 | // }
|
188 |
|
189 | unflatten({
|
190 | '__key1__.__keyA__': 'valueI',
|
191 | '__key2__.__keyB__': 'valueII',
|
192 | '__key3__.__a__.__b__.__c__': 2
|
193 | }, {
|
194 | transformKey: function(key){
|
195 | return key.substring(2, key.length - 2)
|
196 | }
|
197 | })
|
198 |
|
199 | // {
|
200 | // key1: {
|
201 | // keyA: 'valueI'
|
202 | // },
|
203 | // key2: {
|
204 | // keyB: 'valueII'
|
205 | // },
|
206 | // key3: { a: { b: { c: 2 } } }
|
207 | // }
|
208 | ```
|
209 |
|
210 | ## Command Line Usage
|
211 |
|
212 | `flat` is also available as a command line tool. You can run it with [`npx`](https://docs.npmjs.com/cli/v8/commands/npx):
|
213 |
|
214 | ```sh
|
215 | npx flat foo.json
|
216 | ```
|
217 |
|
218 | Or install the `flat` command globally:
|
219 |
|
220 | ```sh
|
221 | npm i -g flat && flat foo.json
|
222 | ```
|
223 |
|
224 | Accepts a filename as an argument:
|
225 |
|
226 | ```sh
|
227 | flat foo.json
|
228 | ```
|
229 |
|
230 | Also accepts JSON on stdin:
|
231 |
|
232 | ```sh
|
233 | cat foo.json | flat
|
234 | ```
|