0.7.0 • Published 9 months ago

pxth v0.7.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

pxth

Tiny utility library for object property path handling

npm version npm downloads vulnerabilities

Install

npm install pxth

Or

yarn add pxth

Api reference

Here are all functions described.

Pxth\

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.

deepGet

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'

deepSet

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' } }

Contributing

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.

License

MIT © Artiom Tretjakovas

Created with aqu 🌊

0.7.0

9 months ago

0.6.0

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.0

3 years ago