safe-flat
    DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/safe-flat package

    2.0.2 • Public • Published

    safe-flat

    Safely flatten a nested JavaScript object.

    NPM

    Commitizen friendly js-standard-style Build Coverage Known Vulnerabilities

    Installation

    $ npm i safe-flat

    Methods

    flatten(obj, [delimiter])

    Flats an object to one level deep. Optionally takes a custom delimiter, otherwise uses . by default. Circular references within the object will be replaced with [Circular].

    const { flatten } = require('safe-flat')
    
    const original = {
        a: {
            b: {
                c: [{
                    val: 'one'
                }, {
                    val: 'two'
                }],
                d: 'three'
            },
            e: 'four',
        }
    }
    original.a.b.f = original.a.b
    original.a.b.c.push(original.a)
    
    const flat = flatten(original)
    /*
    {
      'a.b.c.0.val': 'one',
      'a.b.c.1.val': 'two',
      'a.b.c.2': '[Circular]',
      'a.b.d': 'three',
      'a.e': 'four',
      'a.b.f': '[Circular]'
    }
    */
    
    const underscoreFlat = flatten(original, '_')
    /*
    {
      'a_b_c_0_val': 'one',
      'a_b_c_1_val': 'two',
      'a_b_c_2': '[Circular]',
      'a_b_d': 'three',
      'a_e': 'four',
      'a_b_f': '[Circular]'
    }
    */

    unflatten(obj, [delimiter])

    Unflats an object back to its original nested form. Optionally takes a custom delimiter, otherwise uses . by default. Circular references denoted by [Circular] are treated as Strings.

    const { unflatten } = require('safe-flat')
    
    const original = {
        'a.b.c.0.val': 'one',
        'a.b.c.1.val': 'two',
        'a.b.c.2': '[Circular]',
        'a.b.d': 'three',
        'a.e': 'four',
        'a.b.f': '[Circular]'
    }
    
    
    const unflat = unflatten(original)
    
    /*{
      a:{
        b:{
          c:[
            {
              val:'one'
            },
            {
              val:'two'
            },
            '[Circular]'
          ],
          d:'three',
          f:'[Circular]'
        },
        e:'four'
      }
    }*/

    Install

    npm i safe-flat

    DownloadsWeekly Downloads

    93,874

    Version

    2.0.2

    License

    MIT

    Unpacked Size

    24.1 kB

    Total Files

    15

    Last publish

    Collaborators

    • jessie-codes