a more robust representation for flatten objects/arrays.
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]
// }