Tiny utility library for object property path handling
npm install pxthbash
npm install pxth
`
Or
`bash
yarn add pxth
`
Api reference
Here are all functions described.
$3
Holds path to field of type T in the origin object.
`ts
const a: Pxth; // path to string field.
const b: Pxth; // path to number field.
`
If T is object, it is possible to get paths to its fields:
`ts
const objectPath: Pxth<{ inner: string }>; // path to parent object.
const innerPath: Pxth = objectPath.inner; // path to object's field.
const lengthPath: Pxth = innerPath.length; // you can do it also for primitive type fields.
const deepLengthPath: Pxth = objectPath.inner.length; // any amount of levels, just like normal object.
`
$3
Deeply get value from object.
Usage:
`ts
import { deepGet } from 'pxth';
const somePath: Pxth = / from somewhere /;
// Function is type safe - type of value will be automatically inferred from Pxth. In this case - string.
const value = deepGet({ a: { b: { c: 'Hello world!' } } }, somePath);
console.log(value); // -> 'Hello world'
`
$3
Deeply set value in object. Mutates the object and returns it. If value already exists, overwrites it.
Usage:
`ts
import { deepSet } from 'pxth';
const somePath: Pxth = / from somewhere /;
// Type safe - type of third parameter is inferred from Pxth.
deepSet({ a: { hello: 'asdf' } }, somePath, 'New value'); // -> { a: { hello: 'New value' } }
``