Safely get/set deep nested properties with standard path, strong type support
npm install object-standard-path- Standard path, e.g. a.b.c[0].d.
- Provider types & utils: Path, PathValue, pathGet, pathSet, pathSetImmutable.
- Built with typescript, provide type protection, code autocompletion, make your app robust.
- No dependencies, less than 1kB page size.
``shell`
npm install object-standard-path
`typescript
import { Path, PathValue } from 'object-standard-path';
type Test = {
value: string;
array: {
value: string;
}[];
};
type TestPath = Path
// result: "value" | "array" | array[${number}] | array[${number}].value
type TestPathValue = PathValue
// result: { value: string }
`
`typescript
import { pathGet, pathSet, pathSetImmutable } from 'object-standard-path';
const object = {
array: [
{
value: 1,
},
],
};
const result = pathGet(object, 'array[0].value');
// result: 1
pathSet(object, 'array[0].value', 2);
// object: { array: [{ value: 2 }] }
const result = pathSetImmutable(object, 'array[0].value', 2);
// result: { array: [{ value: 2 }] }
`
Notes: please don't include the characters .[]` in the key of the object, as they may affect parsing.