- Notifications
You must be signed in to change notification settings - Fork6
Safely get and set deep nested properties using dot notation.
License
NotificationsYou must be signed in to change notification settings
g-makarov/dot-path-value
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Safely get and set deep nested properties using dot notation.
- TypeScript first 🤙
- Support arrays
- Tiny
- No dependencies
- Utility types
PathandPathValue
If you find this library useful, why not
# using npmnpm install dot-path-value# using pnpmpnpm install dot-path-value# using yarnyarn add dot-path-value
import{getByPath,setByPath}from'dot-path-value';constobj={a:{b:'hello',d:[{e:'world',}],},};// access through objectgetByPath(obj,'a.b');// outputs 'hello' with type `string`// access through arraygetByPath(obj,'a.d.0.e');// outputs 'world' with type `string`getByPath(obj,'a.d.0');// outputs '{ e: 'world' }' with type `{ e: string }`// also you can pass array as first argumentgetByPath([{a:1}],'0.a');// outputs '1' with type `number`// typescript errorsgetByPath(obj,'a.b.c');// `c` property does not exist// set a property through an objectsetByPath(obj,'a.b','hello there');
dot-path-value exports a few types to ensure the type safety:
| Type | Description |
|---|---|
Path<T> | converts nested structureT into a string representation of the paths to its properties |
PathValue<T, TPath> | returns the type of the value at the specified path |
import{Path,PathValue}from'dot-path-value';constobj={a:{b:'hello',d:[{e:'world',}],},};typeFoo=Path<typeofobj>;// 'a.d' | 'a' | 'a.b' | `a.d.${number}` | `a.d.${number}.e`typeBar=PathValue<typeofobj,'a.b'>;// 'string'
About
Safely get and set deep nested properties using dot notation.
Topics
Resources
License
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.
Contributors4
Uh oh!
There was an error while loading.Please reload this page.
