@ezy/object-description
TypeScript icon, indicating that this package has built-in type declarations

7.1.2 • Public • Published

object description

a more robust representation for flatten objects/arrays.

License CircleCI codecov Docs: typedoc GitHub issues Maintainability Dependencies status Dev Dependencies status Made with: typescript Code style: prettier Runkit: try now minified size minzipped size

Why

flatten/unflatten libraries relies on string based notation for paths leading to mismatches in conversions.

Install

npm i @ezy/object-description

Usage

iterate over values

import { to as toDescription } from '@ezy/object-description';

const desc = toDescription({
  value: true,
  lvl1: {
    lvl2: [[undefined, { 50: false }]]
  }
});

for (const { path, value } of desc.values) {
  console.log(path);
  console.log(value);
}

// ['value']
// true
// ['lvl1', 'lvl2', 0, 1, '50']
// false

change all values

import {
  from as fromDescription,
  to as toDescription
} from '@ezy/object-description';

const desc = toDescription({
  value: true,
  lvl1: {
    lvl2: [[undefined, { 50: false }]]
  }
});

const stringified = fromDescription({
  Ctor: desc.Ctor,
  values: desc.values.map(({ path, value }) => {
    return { path, value: value.toString() };
  })
});

console.log(stringified);

// {
//     value: "true",
//     lvl1: {
//         lvl2: [
//             [ undefined, { 50: "false" } ]
//         ]
//     }
// }

circular references support

deep clone made easy

import {
  from as fromDescription,
  to as toDescription
} from '@ezy/object-description';

const original: any = {
  someprop: 'something'
};
original.imcircular = original;

const desc = toDescription(original);
console.log(desc);
// => {
//   Ctor: Object,
//   values: [{ path: ['someprop'], value: 'something' }],
//   references: [{ path: ['imcircular'], target: [] }]
// }

const clone = fromDescription(desc);
console.log(clone);
// => {
//   someprop: 'something',
//   imcircular: [Circular]
// }

Links

See also

Package Sidebar

Install

npm i @ezy/object-description

Weekly Downloads

1

Version

7.1.2

License

MIT

Unpacked Size

52.3 kB

Total Files

21

Last publish

Collaborators

  • didierdemoniere