UNPKG

4.2 kBMarkdownView Raw
1# flat [![Build Status](https://github.com/hughsk/flat/actions/workflows/main.yml/badge.svg)](https://github.com/hughsk/flat/actions/workflows/main.yml)
2
3Take a nested Javascript object and flatten it, or unflatten an object with
4delimited keys.
5
6## Installation
7
8``` bash
9$ npm install flat
10```
11
12## Methods
13
14### flatten(original, options)
15
16Flattens the object - it'll return an object one level deep, regardless of how
17nested the original object was:
18
19``` javascript
20import { flatten } from 'flat'
21
22flatten({
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
41Flattening is reversible too, you can call `unflatten` on an object:
42
43``` javascript
44import { unflatten } from 'flat'
45
46unflatten({
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
67Use a custom delimiter for (un)flattening your objects, instead of `.`.
68
69### safe
70
71When enabled, both `flat` and `unflatten` will preserve arrays and their
72contents. This is disabled by default.
73
74``` javascript
75import { flatten } from 'flat'
76
77flatten({
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
100When enabled, arrays will not be created automatically when calling unflatten, like so:
101
102``` javascript
103unflatten({
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
120When enabled, existing keys in the unflattened object may be overwritten if they cannot hold a newly encountered nested value:
121
122```javascript
123unflatten({
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
133Without `overwrite` set to `true`, the `TRAVIS` key would already have been set to a string, thus could not accept the nested `DIR` element.
134
135This only makes sense on ordered arrays, and since we're overwriting data, should be used with care.
136
137
138### maxDepth
139
140Maximum number of nested objects to flatten.
141
142``` javascript
143import { flatten } from 'flat'
144
145flatten({
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
164Transform each part of a flat key before and after flattening.
165
166```javascript
167import { flatten, unflatten } from 'flat'
168
169flatten({
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
189unflatten({
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
215npx flat foo.json
216```
217
218Or install the `flat` command globally:
219
220```sh
221npm i -g flat && flat foo.json
222```
223
224Accepts a filename as an argument:
225
226```sh
227flat foo.json
228```
229
230Also accepts JSON on stdin:
231
232```sh
233cat foo.json | flat
234```