2.11.84 • Published 2 months ago

@taktikorg/quas-praesentium v2.11.84

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

2 months ago

2.11.83

2 months ago

2.11.82

2 months ago

2.11.81

3 months ago

2.11.80

3 months ago

2.11.79

3 months ago

2.11.78

3 months ago

2.11.77

3 months ago

2.11.76

3 months ago

2.11.75

3 months ago

2.10.75

3 months ago

2.10.74

3 months ago

2.9.74

3 months ago

2.9.73

3 months ago

2.8.73

3 months ago

2.8.72

3 months ago

2.8.71

3 months ago

2.8.70

3 months ago

2.7.70

3 months ago

2.7.69

3 months ago

2.7.68

3 months ago

2.7.67

3 months ago

2.6.67

3 months ago

1.6.67

3 months ago

1.6.66

3 months ago

1.6.65

3 months ago

1.6.64

3 months ago

1.6.63

3 months ago

1.6.62

3 months ago

1.6.61

3 months ago

1.6.60

3 months ago

1.6.59

3 months ago

1.5.59

3 months ago

1.5.58

4 months ago

1.5.57

4 months ago

1.5.56

4 months ago

1.5.55

4 months ago

1.5.54

4 months ago

1.5.53

4 months ago

1.5.52

4 months ago

1.5.51

4 months ago

1.5.50

4 months ago

1.5.49

4 months ago

1.5.48

4 months ago

1.5.47

4 months ago

1.5.46

4 months ago

1.5.45

4 months ago

1.5.44

4 months ago

1.5.43

4 months ago

1.5.42

4 months ago

1.5.41

4 months ago

1.5.40

4 months ago

1.5.39

4 months ago

1.5.38

4 months ago

1.5.37

4 months ago

1.5.36

4 months ago

1.5.35

4 months ago

1.5.34

4 months ago

1.5.33

4 months ago

1.5.32

4 months ago

1.5.31

4 months ago

1.5.30

4 months ago

1.5.29

4 months ago

1.5.28

5 months ago

1.4.28

5 months ago

1.4.27

5 months ago

1.3.27

5 months ago

1.3.26

5 months ago

1.3.25

5 months ago

1.3.24

5 months ago

1.3.23

5 months ago

1.3.22

5 months ago

1.3.21

5 months ago

1.3.20

5 months ago

1.3.19

5 months ago

1.3.18

5 months ago

1.3.17

5 months ago

1.3.16

5 months ago

1.3.15

5 months ago

1.2.15

5 months ago

1.2.14

5 months ago

1.2.13

5 months ago

1.2.12

5 months ago

1.1.12

5 months ago

1.1.11

5 months ago

1.1.10

5 months ago

1.1.9

5 months ago

1.1.8

5 months ago

1.1.7

5 months ago

1.1.6

5 months ago

1.1.5

5 months ago

1.1.4

5 months ago

1.1.3

5 months ago

1.1.2

6 months ago

1.1.1

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago