6.0.1 • Published 1 year ago

flat v6.0.1

Weekly downloads
4,909,735
License
BSD-3-Clause
Repository
github
Last release
1 year ago

flat Build Status

Take a nested Javascript object and flatten it, or unflatten an object with delimited keys.

Installation

$ npm install flat

Methods

flatten(original, options)

Flattens the object - it'll return an object one level deep, regardless of how nested the original object was:

import { flatten } from 'flat'

flatten({
    key1: {
        keyA: 'valueI'
    },
    key2: {
        keyB: 'valueII'
    },
    key3: { a: { b: { c: 2 } } }
})

// {
//   'key1.keyA': 'valueI',
//   'key2.keyB': 'valueII',
//   'key3.a.b.c': 2
// }

unflatten(original, options)

Flattening is reversible too, you can call unflatten on an object:

import { unflatten } from 'flat'

unflatten({
    'three.levels.deep': 42,
    'three.levels': {
        nested: true
    }
})

// {
//     three: {
//         levels: {
//             deep: 42,
//             nested: true
//         }
//     }
// }

Options

delimiter

Use a custom delimiter for (un)flattening your objects, instead of ..

safe

When enabled, both flat and unflatten will preserve arrays and their contents. This is disabled by default.

import { flatten } from 'flat'

flatten({
    this: [
        { contains: 'arrays' },
        { preserving: {
              them: 'for you'
        }}
    ]
}, {
    safe: true
})

// {
//     'this': [
//         { contains: 'arrays' },
//         { preserving: {
//             them: 'for you'
//         }}
//     ]
// }

object

When enabled, arrays will not be created automatically when calling unflatten, like so:

unflatten({
    'hello.you.0': 'ipsum',
    'hello.you.1': 'lorem',
    'hello.other.world': 'foo'
}, { object: true })

// hello: {
//     you: {
//         0: 'ipsum',
//         1: 'lorem',
//     },
//     other: { world: 'foo' }
// }

overwrite

When enabled, existing keys in the unflattened object may be overwritten if they cannot hold a newly encountered nested value:

unflatten({
    'TRAVIS': 'true',
    'TRAVIS.DIR': '/home/travis/build/kvz/environmental'
}, { overwrite: true })

// TRAVIS: {
//     DIR: '/home/travis/build/kvz/environmental'
// }

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.

maxDepth

Maximum number of nested objects to flatten.

import { flatten } from 'flat'

flatten({
    key1: {
        keyA: 'valueI'
    },
    key2: {
        keyB: 'valueII'
    },
    key3: { a: { b: { c: 2 } } }
}, { maxDepth: 2 })

// {
//   'key1.keyA': 'valueI',
//   'key2.keyB': 'valueII',
//   'key3.a': { b: { c: 2 } }
// }

transformKey

Transform each part of a flat key before and after flattening.

import { flatten, unflatten } from 'flat'

flatten({
    key1: {
        keyA: 'valueI'
    },
    key2: {
        keyB: 'valueII'
    },
    key3: { a: { b: { c: 2 } } }
}, {
    transformKey: function(key){
      return '__' + key + '__';
    }
})

// {
//   '__key1__.__keyA__': 'valueI',
//   '__key2__.__keyB__': 'valueII',
//   '__key3__.__a__.__b__.__c__': 2
// }

unflatten({
      '__key1__.__keyA__': 'valueI',
      '__key2__.__keyB__': 'valueII',
      '__key3__.__a__.__b__.__c__': 2
}, {
    transformKey: function(key){
      return key.substring(2, key.length - 2)
    }
})

// {
//     key1: {
//         keyA: 'valueI'
//     },
//     key2: {
//         keyB: 'valueII'
//     },
//     key3: { a: { b: { c: 2 } } }
// }

Command Line Usage

flat is also available as a command line tool. You can run it with npx:

npx flat foo.json

Or install the flat command globally:

npm i -g flat && flat foo.json

Accepts a filename as an argument:

flat foo.json

Also accepts JSON on stdin:

cat foo.json | flat
webpack-mergeyargs-unparserfundwave-fund-management@fairfx/fuji@directus-asolole/api@atomist/sdm@atomist/automation-client@atomist/sdm-core@namcchan/themeadmin-bro-for-nextmis-potal-navigation@care-for/yargs-unparserreact-native-bluetooth2@admin-bro/coretest-sideex-webservicert3growerhub-avizio-lib@mailupinc/bee-plugin-bundleddependenciesbee-plugin-bundleddependenciescommie-service@splytech-io/castlat_trust_app_new_enrollmentelegant-redisadmin-bro-jztc-cli-pluslogdna-workersenotarylog-client@simon-id/temp-testpostcss-js-to-varsjoi-libairscanairscan-exampleamenov.req.onlyjs@lemoncompanies/angular-extractyarik-test@v9.group/toolkit-ts-commonvalideereact-native-esc-pos-sahaab@borisovart/atol-kkt-modulelaurel@axa/aletheiaperfecto-ai-reportskup.common.util@yaps/tts@containership/containership.cli@containership/containership.k8s.api-bridge@containership/containership.k8s.translator@containership/containership.schema-converterdeneme323112caec-admin-web@ntt_app/react-native-custom-notification@fuseday/torquebussr-admin-brobussr-admin-bro-mongoose@sonammalhotra/lotidemongo-crud-service@springworks/android-plankan@smishra17/lotide@cheapthrills/lotide@ngneat/optimizereact-native-covid-sdkember-i18n-export@nubohq/data@yebu/cli@iobroker-community-adapters/iobroker.device-watcher@heights/heights-uidirectus9-litefrey@elijahjpassmore/qwc2seamless-configsmallstack-cli@genesisx/create-genesis-workspace@gkampitakis/mongo-client@newhorizon-tech/dd-npm-package-templatesynvert-core-javascriptweb-data-managercorbeapplang-sync-clilevibestliblevibestlib2levilibtest19levilibtest24levilibtest25levilibtest26levilibtest27levilibtest28levilibtest29levinodelib@service-exchange/databaseporing@nicholasjj/lotidecorbetadimensioguardmancclibyarntest@knapsack/app-uiliquidjs-litemembership-system@everything-registry/sub-chunk-1679@erskingardner/inlang-plugin-json
6.0.1

1 year ago

6.0.0

1 year ago

3.0.1

4 years ago

1.6.2

4 years ago

2.0.2

4 years ago

4.1.1

4 years ago

5.0.2

4 years ago

5.0.1

4 years ago

5.0.0

5 years ago

4.1.0

6 years ago

4.0.0

7 years ago

3.0.0

7 years ago

2.0.1

8 years ago

2.0.0

9 years ago

1.6.1

9 years ago

1.6.0

9 years ago

1.5.1

10 years ago

1.5.0

10 years ago

1.4.0

10 years ago

1.3.0

10 years ago

1.2.1

11 years ago

1.2.0

11 years ago

1.1.0

11 years ago

1.0.0

12 years ago

0.2.0

12 years ago

0.1.0

12 years ago

0.0.2

12 years ago

0.0.1

12 years ago