flat

repository·master·Indexed 23 days ago

https://github.com/hughsk/flat

A JavaScript utility for flattening nested objects into a single-level object with delimited keys, or unflattening delimited keys back into a nested structure. Version 6.0.1 provides the flatten() and unflatten() methods, along with a CLI tool for processing JSON files or stdin. Features include customizable delimiters, depth limiting via maxDepth, key transformation via transformKey, and options to preserve arrays or prevent automatic array creation during unflattening.

Tokens
2.6K
Snippets
10
Records
17
Agent score
80%

What's inside flat

  1. Configure flatten and unflatten with options

    master

    Both flatten and unflatten accept an options object to customize behavior. Available options include:

    • delimiter: Use a custom string instead of . to separate keys.
    • safe: When true, preserves arrays and their contents instead of flattening them. Disabled by default.
    • object: When true (used with unflatten), arrays will not be created automatically; instead, numeric keys will be treated as object properties.
    • overwrite: When true (used with unflatten), existing keys may be overwritten if they cannot hold a newly encountered nested value.
    • maxDepth: The maximum number of nested levels to flatten.
    • transformKey: A function applied to each part of a key before and after the (un)flattening process.
  2. Limit flattening depth with maxDepth

    master

    The maxDepth option limits how many levels of nesting the flatten method will process.

    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 } }
    // }
  3. Transform keys with transformKey

    master

    The transformKey option accepts a function that is called on every key segment. This is useful for adding prefixes/suffixes during flattening and removing them during unflattening.

    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 } } }
    // }
  4. Use the safe option to preserve arrays

    master

    By default, flat flattens arrays. Setting { safe: true } ensures arrays and their contents are preserved in the resulting object.

    import { flatten } from 'flat'
    
    flatten({
        this: [
            { contains: 'arrays' },
            { preserving: { 
                  them: 'for you' 
            }}
        ]
    }, {
        safe: true
    })
    
    // {
    //     'this': [
    //         { contains: 'arrays' },
    //         { preserving: {
    //             them: 'for you'
    //         }}
    //     ]
    // }
  5. Use the object option in unflatten to prevent automatic array creation

    master

    When calling unflatten, setting { object: true } prevents the creation of arrays for numeric keys, treating them as standard object properties instead.

    unflatten({
        'hello.you.0': 'ipsum',
        'hello.you.1': 'lorem',
        'hello.other.world': 'foo'
    }, { object: true })
    
    // hello: {
    //     you: {
    //         0: 'ipsum',
    //         1: 'lorem',
    //     },
    //     other: { world: 'foo' }
    // }
  6. Use the overwrite option in unflatten to resolve key conflicts

    master

    If an existing key in the unflattened object is a primitive (like a string) but a new nested value needs to be placed at that path, setting { overwrite: true } allows the primitive to be replaced by the new nested object.

    unflatten({
        'TRAVIS': 'true',
        'TRAVIS.DIR': '/home/travis/build/kvz/environmental'
    }, { overwrite: true })
    
    // TRAVIS: {
    //     DIR: '/home/travis/build/kvz/environmental'
    // }
  7. Flatten a nested object with flatten()

    master

    The flatten(original, options) method takes a nested Javascript object and returns a new object that is one level deep. Nested keys are joined using a delimiter (defaulting to .).

    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
    // }
  8. Unflatten an object with unflatten()

    master

    The unflatten(original, options) method is the reverse of flatten. It takes an object with delimited keys and reconstructs the original nested structure.

    import { unflatten } from 'flat'
    
    unflatten({
        'three.levels.deep': 42,
        'three.levels': {
            nested: true
        }
    })
    
    // {
    //     three: {
    //         levels: {
    //             deep: 42,
    //             nested: true
    //         }
    //     }
    // }
  9. Use flat via the CLI

    master

    The flat package can be used as a command-line tool to process JSON files or stdin.

    Using npx:

    npx flat foo.json

    Global installation:

    npm i -g flat && flat foo.json

    Using stdin:

    cat foo.json | flat
  10. Flatten an object with flatten()

    master
    Use flatten(target, options) to convert a nested object into a flat object where keys represent the path to the original values. The target is the object to be flattened. You can provide optional FlattenOptions to customize the behavior.
  11. Unflatten a flat object with unflatten()

    master

    The unflatten(target, opts) function takes a flat object with delimited keys and reconstructs the original nested object structure.

    If the input target is not a plain object (e.g., it is a Buffer or a primitive), the function returns the target as-is. It also includes safety checks to prevent prototype pollution by ignoring __proto__ keys.