source: imaps-frontend/node_modules/flat/README.md

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.1 KB
Line 
1# flat [![Build Status](https://secure.travis-ci.org/hughsk/flat.png?branch=master)](http://travis-ci.org/hughsk/flat)
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
20var flatten = require('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 `flatten.unflatten()` on an object:
42
43``` javascript
44var unflatten = require('flat').unflatten
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
75var flatten = require('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
143var flatten = require('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
167var flatten = require('flat')
168var unflatten = require('flat').unflatten
169
170flatten({
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
190unflatten({
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
217npx flat foo.json
218```
219
220Or install the `flat` command globally:
221
222```sh
223npm i -g flat && flat foo.json
224```
225
226Accepts a filename as an argument:
227
228```sh
229flat foo.json
230```
231
232Also accepts JSON on stdin:
233
234```sh
235cat foo.json | flat
236```
Note: See TracBrowser for help on using the repository browser.