File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- # obj-path
1+ # obj-path
2+
3+ <p align =" center " width =" 100% " >
4+ <a href =" https://github.com/pyramation/obj-path/actions/workflows/run-tests.yaml " >
5+ <img height="20" src="https://github.com/pyramation/obj-path/actions/workflows/run-tests.yaml/badge.svg" />
6+ </a >
7+ <a href =" https://github.com/pyramation/obj-path/blob/main/LICENSE-MIT " ><img height =" 20 " src =" https://img.shields.io/badge/license-MIT-blue.svg " ></a >
8+ <a href =" https://github.com/pyramation/obj-path/blob/main/LICENSE-Apache " ><img height =" 20 " src =" https://img.shields.io/badge/license-Apache-blue.svg " ></a >
9+ </p >
10+
11+ ` obj-path ` is a simple and lightweight JavaScript utility library for safely accessing and modifying nested properties in objects using string paths.
12+
13+ ## Features
14+
15+ - ** Get** : Safely access nested properties in an object.
16+ - ** Set** : Set a value at a specific path in an object. Does not overwrite if the value is undefined.
17+ - ** Has** : Check if a specific path exists within an object.
18+
19+ ## Installation
20+
21+ ``` bash
22+ npm install obj-path
23+ ```
24+
25+ ## Usage
26+
27+ ### Get
28+
29+ Retrieve a nested property value from an object.
30+
31+ ``` ts
32+ import objectPath from ' obj-path' ;
33+
34+ const obj = {
35+ user: {
36+ name: ' John Doe' ,
37+ address: {
38+ street: ' 123 Main St' ,
39+ city: ' Anytown'
40+ }
41+ }
42+ };
43+
44+ const userName = objectPath .get (obj , ' user.name' );
45+ console .log (userName ); // 'John Doe'
46+ ```
47+
48+ ### Set
49+
50+ Set a value at a specific path in an object. If any part of the path does not exist, it will be created.
51+
52+ ``` ts
53+ objectPath .set (obj , ' user.address.zip' , ' 12345' );
54+ console .log (obj .user .address .zip ); // '12345'
55+ ```
56+
57+ ### Has
58+
59+ Check if a path exists within an object.
60+
61+ ``` ts
62+ const hasCity = objectPath .has (obj , ' user.address.city' );
63+ console .log (hasCity ); // true
64+ ```
65+
66+ ## Running Tests
67+
68+ To run tests, execute the following command:
69+
70+ ``` sh
71+ npm test
72+ ```
73+
74+ or for continuous
75+
76+ ``` sh
77+ npm test:watch
78+ ```
You can’t perform that action at this time.
0 commit comments