@@ -22,157 +22,57 @@ yarn add pxth
2222
2323Here are all functions described.
2424
25- ### toPxth
26-
27- Convert string / number / symbol to Pxth.
28-
29- Usage:
25+ ### Pxth\< T>
3026
27+ Holds path to field of type ` T ` in the origin object.
3128``` ts
32- import { toPxth , ROOT_PATH } from ' pxth' ;
33-
34- toPxth (' a.b.c' ); // -> ['a', 'b', 'c']
29+ const a: Pxth <string >; // path to string field.
3530
36- // any symbol
37- toPxth (Symbol ()); // -> [Symbol()]
38- // ROOT_PATH symbol
39- toPxth (ROOT_PATH ); // -> []
40-
41- toPxth (0 ); // -> [0]
31+ const b: Pxth <number >; // path to number field.
4232```
4333
44- ### pxthToString
45-
46- Convert Pxth to string. Throws error if pxth cannot be stringified. [ Here is function for check] ( #canBeStringified )
47-
48- Usage:
49-
34+ If ` T ` is object, it is possible to get paths to its fields:
5035``` ts
51- import { pxthToString } from ' pxth' ;
52-
53- pxthToString ([' lol' , ' b' , 0 , ' .as0 ' ]); // -> lol.b.0.[" .as0 "]
54-
55- pxthToString ([Symbol (), ' asdf' ]); // -> throws error
56- ```
36+ const objectPath: Pxth <{ inner: string }>; // path to parent object.
5737
58- ### stringToPxth
38+ const innerPath : Pxth < string > = objectPath . inner ; // path to object's field.
5939
60- Convert string to pxth. Function [ toPxth ] ( #toPxth ) calls it, if first argument is string .
40+ const lengthPath : Pxth < number > = innerPath . length ; // you can do it also for primitive type fields .
6141
62- ``` ts
63- import { stringToPxth } from ' pxth' ;
64-
65- stringToPxth (' hello.a.b' ); // -> ['hello', 'a', 'b']
42+ const deepLengthPath: Pxth <number > = objectPath .inner .length ; // any amount of levels, just like normal object.
6643```
6744
68- ### get
45+ ### deepGet
6946
7047Deeply get value from object.
7148
7249Usage:
7350
7451``` ts
75- import { get } from ' pxth' ;
76-
77- get ({ a: { b: { c: ' Hello world!' } } }, toPxth (' a.b.c' )); // -> 'Hello world'
78-
79- // third argument is default value
80- get ({ a: ' hello' }, toPxth (' b.c.d' ), ' Default value' ); // -> 'Default value'
81- ```
82-
83- ### set
84-
85- Deeply set value in object. Mutates the object and returns it. If value already exists, overwrites it.
86-
87- Usage:
88-
89- ``` ts
90- import { set } from ' pxth' ;
91-
92- set ({ a: { hello: ' asdf' } }, toPxth (' a.hello' ), ' New value' ); // -> { a: { hello: 'New value' } }
93-
94- set ({ a: ' hello' }, toPxth (' a.b' ), ' New value' ); // -> { a: { b: 'New value' } }
95- ```
96-
97- ### isNestedPath
98-
99- Determines if one path is child path of another.
100-
101- Usage:
102-
103- ``` ts
104- import { isNestedPath } from ' pxth' ;
105-
106- isNestedPath ([' hello' , ' bye' , ' yes' ], [' hello' ]); // -> true
107-
108- isNestedPath ([' hello' , ' bye' , ' yes' ], [' hello' , ' bye' , ' no' ]); // -> false
109- ```
110-
111- ### longestCommonPath
52+ import { deepGet } from ' pxth' ;
11253
113- Returns longest common path in array
114-
115- Usage:
54+ const somePath: Pxth <string > = /* from somewhere */ ;
11655
117- ``` ts
118- import { longestCommonPath } from ' pxth ' ;
56+ // Function is type safe - type of value will be automatically inferred from Pxth. In this case - string.
57+ const value = deepGet ({ a: { b: { c: ' Hello world! ' } } }, somePath ) ;
11958
120- longestCommonPath ([
121- [' hello' , ' a' ],
122- [' hello' , ' b' ],
123- [' hello' , ' c' ],
124- ]); // -> ['hello']
59+ console .log (value ); // -> 'Hello world'
12560
126- longestCommonPath ([[' a' ], [' b' ], [' c' ]]); // -> []
12761```
12862
129- ### canBeStringified
130-
131- Function, detecting if ` Pxth ` instance could be stringified or not.
132-
133- Usage:
134-
135- ``` ts
136- import { canBeStringified } from ' pxth' ;
137-
138- canBeStringified ([' hello' , ' world' ]); // -> true
63+ ### deepSet
13964
140- canBeStringified ([Symbol (), ' asdf' ]); // -> false
141- ```
142-
143- ### relativePath
144-
145- Make one path relative to another
146-
147- Usage:
148-
149- ``` ts
150- import { relativePath } from ' pxth' ;
151-
152- relativePath ([' hello' , ' world' ], [' hello' , ' world' , ' asdf' ]); // -> ['asdf']
153- relativePath ([' a' , ' b' , ' c' ], [' a' , ' b' , ' c' , ' d' , ' e' ]); // -> ['d', 'e']
154- relativePath ([' a' ], [' b' ]); // -> Error
155- ```
156-
157- ### toObjectKey
158-
159- Convert pxth to object key
65+ Deeply set value in object. Mutates the object and returns it. If value already exists, overwrites it.
16066
16167Usage:
16268
16369``` ts
164- import { toObjectKey } from ' pxth' ;
70+ import { deepSet } from ' pxth' ;
16571
166- toObjectKey ([' a' , ' b' , ' c' ]); // -> 'a.b.c'
167- toObjectKey ([]); // -> ROOT_PATH
168- ```
72+ const somePath: Pxth <string > = /* from somewhere */ ;
16973
170- ### ROOT_PATH
171-
172- Constant, used in [ toObjectKey] ( #toObjectKey ) function, to convert empty Pxth to object key.
173-
174- ``` ts
175- import { ROOT_PATH } from ' pxth' ;
74+ // Type safe - type of third parameter is inferred from Pxth.
75+ deepSet ({ a: { hello: ' asdf' } }, somePath , ' New value' ); // -> { a: { hello: 'New value' } }
17676```
17777
17878## Contributing
0 commit comments