Skip to content
This repository was archived by the owner on Oct 28, 2024. It is now read-only.

Commit f13babc

Browse files
authored
Updated documentation, fixed minor bugs (#24)
1 parent 4093ade commit f13babc

File tree

7 files changed

+40
-147
lines changed

7 files changed

+40
-147
lines changed

README.md

Lines changed: 21 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -22,157 +22,57 @@ yarn add pxth
2222

2323
Here 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

7047
Deeply get value from object.
7148

7249
Usage:
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

16167
Usage:
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

src/Pxth.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
1-
type OmitMethods<V extends object> = Pick<
1+
type OmitMethods<V> = Pick<
22
V,
33
{
44
[K in keyof V]: V[K] extends Function ? never : K;
55
}[keyof V]
66
>;
77

8-
type RecordPxth<V extends object> = {
8+
type RecordPxth<V> = {
99
[K in keyof OmitMethods<V>]: Pxth<V[K]>;
1010
};
1111

12-
type ArrayPxth<V extends unknown[]> = {
13-
[K in number]: Pxth<V[number]>;
14-
} &
15-
RecordPxth<Omit<V, number>>;
12+
type PreparePxth<V> = V extends number
13+
? Number
14+
: V extends boolean
15+
? Boolean
16+
: V extends string
17+
? String
18+
: V extends symbol
19+
? Symbol
20+
: V;
1621

17-
type ObjectPxth<V extends object> = V extends unknown[]
18-
? ArrayPxth<V>
19-
: RecordPxth<V>;
22+
type SpecificPxth<V> = V extends unknown[]
23+
? RecordPxth<Omit<Array<V>, number>>
24+
: {};
2025

2126
declare const BrandKey: unique symbol;
22-
declare const PrimitiveKey: unique symbol;
2327

2428
export type Pxth<V> = {
2529
/**
@@ -30,15 +34,5 @@ export type Pxth<V> = {
3034
* assign value, that cannot be accessed (because BrandKey is not exported)
3135
*/
3236
[BrandKey]: V;
33-
} & (V extends object
34-
? ObjectPxth<V>
35-
: {
36-
/**
37-
* Another trick - if we leave this object empty, typescript will
38-
* automatically unwrap all non-object types (Pxth<number> becomes
39-
* { [BrandKey]: number }). Because of that, we cannot infer type
40-
* when using in generic functions. To prevent this behavior, we
41-
* add another inaccessible property.
42-
*/
43-
[PrimitiveKey]: true;
44-
});
37+
} & RecordPxth<PreparePxth<V>> &
38+
SpecificPxth<V>;
File renamed without changes.

src/createPxthProxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SegmentsToken } from './getPxthSegments';
22
import { Pxth } from './Pxth';
3-
import { PxthSegments } from './PxthSource';
3+
import { PxthSegments } from './PxthSegments';
44

55
const handlers: ProxyHandler<
66
Record<string, Pxth<unknown>> & {

src/getPxthSegments.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PxthSegments } from './PxthSource';
1+
import { PxthSegments } from './PxthSegments';
22
import { Pxth } from '.';
33

44
export const SegmentsToken = Symbol();

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export * from './deepGet';
22
export * from './deepSet';
33
export { getPxthSegments } from './getPxthSegments';
44
export * from './Pxth';
5-
export * from './PxthSource';
5+
export * from './PxthSegments';
66
export * from './RootPath';
77
export * from './createPxth';
88
export * from './pxthToString';

src/longestCommonPxth.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { createPxth } from './createPxth';
22
import { getPxthSegments } from './getPxthSegments';
33
import { Pxth } from './Pxth';
4-
import { PxthSegments } from './PxthSource';
54

65
/**
76
* Finds longest common path.

0 commit comments

Comments
 (0)