2.11.84 • Published 5 months ago

@taktikorg/quas-praesentium v2.11.84

Weekly downloads
-
License
MIT
Repository
github
Last release
5 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

5 months ago

2.11.83

6 months ago

2.11.82

6 months ago

2.11.81

6 months ago

2.11.80

6 months ago

2.11.79

6 months ago

2.11.78

6 months ago

2.11.77

6 months ago

2.11.76

6 months ago

2.11.75

6 months ago

2.10.75

6 months ago

2.10.74

6 months ago

2.9.74

6 months ago

2.9.73

6 months ago

2.8.73

6 months ago

2.8.72

6 months ago

2.8.71

6 months ago

2.8.70

6 months ago

2.7.70

6 months ago

2.7.69

6 months ago

2.7.68

6 months ago

2.7.67

6 months ago

2.6.67

6 months ago

1.6.67

6 months ago

1.6.66

6 months ago

1.6.65

6 months ago

1.6.64

6 months ago

1.6.63

6 months ago

1.6.62

6 months ago

1.6.61

6 months ago

1.6.60

6 months ago

1.6.59

7 months ago

1.5.59

7 months ago

1.5.58

7 months ago

1.5.57

7 months ago

1.5.56

7 months ago

1.5.55

7 months ago

1.5.54

7 months ago

1.5.53

7 months ago

1.5.52

7 months ago

1.5.51

7 months ago

1.5.50

7 months ago

1.5.49

7 months ago

1.5.48

7 months ago

1.5.47

7 months ago

1.5.46

7 months ago

1.5.45

7 months ago

1.5.44

7 months ago

1.5.43

7 months ago

1.5.42

7 months ago

1.5.41

7 months ago

1.5.40

7 months ago

1.5.39

7 months ago

1.5.38

7 months ago

1.5.37

7 months ago

1.5.36

7 months ago

1.5.35

7 months ago

1.5.34

7 months ago

1.5.33

7 months ago

1.5.32

7 months ago

1.5.31

7 months ago

1.5.30

8 months ago

1.5.29

8 months ago

1.5.28

8 months ago

1.4.28

8 months ago

1.4.27

8 months ago

1.3.27

8 months ago

1.3.26

8 months ago

1.3.25

8 months ago

1.3.24

8 months ago

1.3.23

8 months ago

1.3.22

8 months ago

1.3.21

8 months ago

1.3.20

8 months ago

1.3.19

8 months ago

1.3.18

8 months ago

1.3.17

8 months ago

1.3.16

8 months ago

1.3.15

8 months ago

1.2.15

8 months ago

1.2.14

8 months ago

1.2.13

8 months ago

1.2.12

8 months ago

1.1.12

8 months ago

1.1.11

8 months ago

1.1.10

8 months ago

1.1.9

8 months ago

1.1.8

8 months ago

1.1.7

8 months ago

1.1.6

8 months ago

1.1.5

8 months ago

1.1.4

9 months ago

1.1.3

9 months ago

1.1.2

9 months ago

1.1.1

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago