Note
This repository was moved into reactive-forms monorepo
Tiny utility library for object property path handling
npm install pxth
Or
yarn add pxth
Here are all functions described.
Holds path to field of type T
in the origin object.
const a: Pxth<string>; // path to string field.
const b: Pxth<number>; // path to number field.
If T
is object, it is possible to get paths to its fields:
const objectPath: Pxth<{ inner: string }>; // path to parent object.
const innerPath: Pxth<string> = objectPath.inner; // path to object's field.
const lengthPath: Pxth<number> = innerPath.length; // you can do it also for primitive type fields.
const deepLengthPath: Pxth<number> = objectPath.inner.length; // any amount of levels, just like normal object.
Deeply get value from object.
Usage:
import { deepGet } from 'pxth';
const somePath: Pxth<string> = /* 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'
Deeply set value in object. Mutates the object and returns it. If value already exists, overwrites it.
Usage:
import { deepSet } from 'pxth';
const somePath: Pxth<string> = /* from somewhere */;
// Type safe - type of third parameter is inferred from Pxth.
deepSet({ a: { hello: 'asdf' } }, somePath, 'New value'); // -> { a: { hello: 'New value' } }
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
MIT © Artiom Tretjakovas