3.0.6 • Published 10 months ago

primitivify v3.0.6

Weekly downloads
19
License
MIT
Repository
github
Last release
10 months ago

primitivify

deep copy data keeping only primitives in nested data structures. useful for serializing complex objects where you only care about the raw data contained within.

"primitive-ify"

TypeScript package semantic-release

install

npm install --save primitivify

usage

the best demonstration of the value of this module is exemplified from the tests!

import primitivify from "primitivify";
const dummyFn = () => {};
const complex = {
  dummyFn,
  a: {
    b: {
      c: setTimeout(() => {}, 0),
    },
    d: setInterval(dummyFn, 1e3),
  },
  b: [dummyFn, "wee", { e: "e" }],
};
t.deepEqual(
  {
    // observe how non-primitive datas are nulled away
    dummyFn: null,
    a: {
      b: {
        c: null,
      },
      d: null,
    },
    b: [null, "wee", { e: "e" }],
  },
  primitivify(complexObj),
  "complex"
);

primitivify is immutable--calls return deeply cloned values.

need a custom serializer? use the 2nd argument, onVisit, to transform the current value being inspected:

primitivify(
  { a: () => {} },
  v => typeof v === 'function' ? 'wee' : v)
)
// { a: 'wee' }

why

generally to decomplect objects and/or arrays. consinder a simple example:

JSON.stringify({ usefulData: "beep", a: setTimeout(() => {}, 0) });

/*
Thrown:
TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Timeout'
    |     property '_idlePrev' -> object with constructor 'TimersList'
    --- property '_idleNext' closes the circle
    at JSON.stringify (<anonymous>)
*/

JSON.stringify(primitivify({ a: setTimeout(() => {}, 0) }));
/*
'{"usefulData":"beep","a":null}'
*/
3.0.4

12 months ago

3.0.3

12 months ago

3.0.2

12 months ago

3.0.1

12 months ago

3.0.6

10 months ago

3.0.5

11 months ago

3.0.0

2 years ago

2.0.0

2 years ago

1.1.2

5 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago