Gathering detailed insights and metrics for flat
Gathering detailed insights and metrics for flat
Gathering detailed insights and metrics for flat
Gathering detailed insights and metrics for flat
🚂 Flatten/unflatten nested Javascript objects
npm install flat
100
Supply Chain
100
Quality
80
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,785 Stars
124 Commits
196 Forks
20 Watching
1 Branches
26 Contributors
Updated on 23 Nov 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-2.7%
3,316,106
Compared to previous day
Last week
3.6%
17,792,293
Compared to previous week
Last month
14%
72,860,432
Compared to previous month
Last year
42.4%
718,200,723
Compared to previous year
1
Take a nested Javascript object and flatten it, or unflatten an object with delimited keys.
1$ npm install flat
Flattens the object - it'll return an object one level deep, regardless of how nested the original object was:
1import { flatten } from 'flat' 2 3flatten({ 4 key1: { 5 keyA: 'valueI' 6 }, 7 key2: { 8 keyB: 'valueII' 9 }, 10 key3: { a: { b: { c: 2 } } } 11}) 12 13// { 14// 'key1.keyA': 'valueI', 15// 'key2.keyB': 'valueII', 16// 'key3.a.b.c': 2 17// }
Flattening is reversible too, you can call unflatten
on an object:
1import { unflatten } from 'flat' 2 3unflatten({ 4 'three.levels.deep': 42, 5 'three.levels': { 6 nested: true 7 } 8}) 9 10// { 11// three: { 12// levels: { 13// deep: 42, 14// nested: true 15// } 16// } 17// }
Use a custom delimiter for (un)flattening your objects, instead of .
.
When enabled, both flat
and unflatten
will preserve arrays and their
contents. This is disabled by default.
1import { flatten } from 'flat' 2 3flatten({ 4 this: [ 5 { contains: 'arrays' }, 6 { preserving: { 7 them: 'for you' 8 }} 9 ] 10}, { 11 safe: true 12}) 13 14// { 15// 'this': [ 16// { contains: 'arrays' }, 17// { preserving: { 18// them: 'for you' 19// }} 20// ] 21// }
When enabled, arrays will not be created automatically when calling unflatten, like so:
1unflatten({ 2 'hello.you.0': 'ipsum', 3 'hello.you.1': 'lorem', 4 'hello.other.world': 'foo' 5}, { object: true }) 6 7// hello: { 8// you: { 9// 0: 'ipsum', 10// 1: 'lorem', 11// }, 12// other: { world: 'foo' } 13// }
When enabled, existing keys in the unflattened object may be overwritten if they cannot hold a newly encountered nested value:
1unflatten({ 2 'TRAVIS': 'true', 3 'TRAVIS.DIR': '/home/travis/build/kvz/environmental' 4}, { overwrite: true }) 5 6// TRAVIS: { 7// DIR: '/home/travis/build/kvz/environmental' 8// }
Without overwrite
set to true
, the TRAVIS
key would already have been set to a string, thus could not accept the nested DIR
element.
This only makes sense on ordered arrays, and since we're overwriting data, should be used with care.
Maximum number of nested objects to flatten.
1import { flatten } from 'flat' 2 3flatten({ 4 key1: { 5 keyA: 'valueI' 6 }, 7 key2: { 8 keyB: 'valueII' 9 }, 10 key3: { a: { b: { c: 2 } } } 11}, { maxDepth: 2 }) 12 13// { 14// 'key1.keyA': 'valueI', 15// 'key2.keyB': 'valueII', 16// 'key3.a': { b: { c: 2 } } 17// }
Transform each part of a flat key before and after flattening.
1import { flatten, unflatten } from 'flat' 2 3flatten({ 4 key1: { 5 keyA: 'valueI' 6 }, 7 key2: { 8 keyB: 'valueII' 9 }, 10 key3: { a: { b: { c: 2 } } } 11}, { 12 transformKey: function(key){ 13 return '__' + key + '__'; 14 } 15}) 16 17// { 18// '__key1__.__keyA__': 'valueI', 19// '__key2__.__keyB__': 'valueII', 20// '__key3__.__a__.__b__.__c__': 2 21// } 22 23unflatten({ 24 '__key1__.__keyA__': 'valueI', 25 '__key2__.__keyB__': 'valueII', 26 '__key3__.__a__.__b__.__c__': 2 27}, { 28 transformKey: function(key){ 29 return key.substring(2, key.length - 2) 30 } 31}) 32 33// { 34// key1: { 35// keyA: 'valueI' 36// }, 37// key2: { 38// keyB: 'valueII' 39// }, 40// key3: { a: { b: { c: 2 } } } 41// }
flat
is also available as a command line tool. You can run it with npx
:
1npx flat foo.json
Or install the flat
command globally:
1npm i -g flat && flat foo.json
Accepts a filename as an argument:
1flat foo.json
Also accepts JSON on stdin:
1cat foo.json | flat
The latest stable version of the package.
Stable Version
1
9.8/10
Summary
flat vulnerable to Prototype Pollution
Affected Versions
< 5.0.1
Patched Versions
5.0.1
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
1 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
2 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 2
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
Found 1/21 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
security policy file not detected
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More