2.11.84 • Published 4 months ago

@taktikorg/quas-praesentium v2.11.84

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

@taktikorg/quas-praesentium

Safely flatten a nested JavaScript object.

NPM

Commitizen friendly js-standard-style Build Coverage Known Vulnerabilities

Installation

$ npm i @taktikorg/quas-praesentium

Methods

flatten(obj, delimiter)

Flattens 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('@taktikorg/quas-praesentium')

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)

Unflattens 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('@taktikorg/quas-praesentium')

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'
  }
}*/
2.11.84

4 months ago

2.11.83

4 months ago

2.11.82

4 months ago

2.11.81

5 months ago

2.11.80

5 months ago

2.11.79

5 months ago

2.11.78

5 months ago

2.11.77

5 months ago

2.11.76

5 months ago

2.11.75

5 months ago

2.10.75

5 months ago

2.10.74

5 months ago

2.9.74

5 months ago

2.9.73

5 months ago

2.8.73

5 months ago

2.8.72

5 months ago

2.8.71

5 months ago

2.8.70

5 months ago

2.7.70

5 months ago

2.7.69

5 months ago

2.7.68

5 months ago

2.7.67

5 months ago

2.6.67

5 months ago

1.6.67

5 months ago

1.6.66

5 months ago

1.6.65

5 months ago

1.6.64

5 months ago

1.6.63

5 months ago

1.6.62

5 months ago

1.6.61

5 months ago

1.6.60

5 months ago

1.6.59

5 months ago

1.5.59

5 months ago

1.5.58

6 months ago

1.5.57

6 months ago

1.5.56

6 months ago

1.5.55

6 months ago

1.5.54

6 months ago

1.5.53

6 months ago

1.5.52

6 months ago

1.5.51

6 months ago

1.5.50

6 months ago

1.5.49

6 months ago

1.5.48

6 months ago

1.5.47

6 months ago

1.5.46

6 months ago

1.5.45

6 months ago

1.5.44

6 months ago

1.5.43

6 months ago

1.5.42

6 months ago

1.5.41

6 months ago

1.5.40

6 months ago

1.5.39

6 months ago

1.5.38

6 months ago

1.5.37

6 months ago

1.5.36

6 months ago

1.5.35

6 months ago

1.5.34

6 months ago

1.5.33

6 months ago

1.5.32

6 months ago

1.5.31

6 months ago

1.5.30

6 months ago

1.5.29

6 months ago

1.5.28

7 months ago

1.4.28

7 months ago

1.4.27

7 months ago

1.3.27

7 months ago

1.3.26

7 months ago

1.3.25

7 months ago

1.3.24

7 months ago

1.3.23

7 months ago

1.3.22

7 months ago

1.3.21

7 months ago

1.3.20

7 months ago

1.3.19

7 months ago

1.3.18

7 months ago

1.3.17

7 months ago

1.3.16

7 months ago

1.3.15

7 months ago

1.2.15

7 months ago

1.2.14

7 months ago

1.2.13

7 months ago

1.2.12

7 months ago

1.1.12

7 months ago

1.1.11

7 months ago

1.1.10

7 months ago

1.1.9

7 months ago

1.1.8

7 months ago

1.1.7

7 months ago

1.1.6

7 months ago

1.1.5

7 months ago

1.1.4

7 months ago

1.1.3

7 months ago

1.1.2

8 months ago

1.1.1

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago