11// forked from https://github.com/g-makarov/dot-path-value
2- export type Primitive = null | undefined | string | number | boolean | symbol | bigint
2+ export type Primitive = null | undefined | string | number | boolean | symbol | bigint ;
33
4- type ArrayKey = number
4+ type ArrayKey = number ;
55
6- type IsTuple < T extends readonly any [ ] > = number extends T [ 'length' ] ? false : true
6+ type IsTuple < T extends readonly any [ ] > = number extends T [ 'length' ] ? false : true ;
77
8- type TupleKeys < T extends readonly any [ ] > = Exclude < keyof T , keyof any [ ] >
8+ type TupleKeys < T extends readonly any [ ] > = Exclude < keyof T , keyof any [ ] > ;
99
1010export type PathConcat < TKey extends string | number , TValue > = TValue extends Primitive
1111 ? `${TKey } `
12- : `${TKey } ` | `${TKey } .${Path < TValue > } `
12+ : `${TKey } ` | `${TKey } .${Path < TValue > } `;
1313
1414export type Path < T > = T extends readonly ( infer V ) [ ]
1515 ? IsTuple < T > extends true
@@ -19,15 +19,15 @@ export type Path<T> = T extends readonly (infer V)[]
1919 : PathConcat < ArrayKey , V >
2020 : {
2121 [ K in keyof T ] -?: PathConcat < K & string , T [ K ] > ;
22- } [ keyof T ]
22+ } [ keyof T ] ;
2323
2424type ArrayPathConcat < TKey extends string | number , TValue > = TValue extends Primitive
2525 ? never
2626 : TValue extends readonly ( infer U ) [ ]
27- ? U extends Primitive
28- ? never
29- : `${TKey } ` | `${TKey } .${ArrayPath < TValue > } `
30- : `${TKey } .${ArrayPath < TValue > } `
27+ ? U extends Primitive
28+ ? never
29+ : `${TKey } ` | `${TKey } .${ArrayPath < TValue > } `
30+ : `${TKey } .${ArrayPath < TValue > } `;
3131
3232export type ArrayPath < T > = T extends readonly ( infer V ) [ ]
3333 ? IsTuple < T > extends true
@@ -37,7 +37,7 @@ export type ArrayPath<T> = T extends readonly (infer V)[]
3737 : ArrayPathConcat < ArrayKey , V >
3838 : {
3939 [ K in keyof T ] -?: ArrayPathConcat < K & string , T [ K ] > ;
40- } [ keyof T ]
40+ } [ keyof T ] ;
4141
4242export type PathValue < T , TPath extends Path < T > | ArrayPath < T > > = T extends any
4343 ? TPath extends `${infer K } .${infer R } `
@@ -48,66 +48,47 @@ export type PathValue<T, TPath extends Path<T> | ArrayPath<T>> = T extends any
4848 : PathValue < T [ K ] , R >
4949 : never
5050 : K extends `${ArrayKey } `
51- ? T extends readonly ( infer V ) [ ]
52- ? PathValue < V , R & Path < V > >
53- : never
51+ ? T extends readonly ( infer V ) [ ]
52+ ? PathValue < V , R & Path < V > >
5453 : never
54+ : never
5555 : TPath extends keyof T
56- ? T [ TPath ]
57- : TPath extends `${ArrayKey } `
58- ? T extends readonly ( infer V ) [ ]
59- ? V
60- : never
61- : never
62- : never
63- /**
64- * It takes an object and a path, and returns the value at that path, type safe.
65- * @param {T } obj - The object to get the value from.
66- * @param {TPath } path - The path to the value you want to get.
67- * @returns The value of the path in the object.
68- * @forked from https://github.com/g-makarov/dot-path-value
69- * @example
70- * ```ts
71- * const obj = {
72- * a: {
73- * b: {
74- * c: [
75- * { d: 1 }
76- * ]
77- * },
78- * }
79- * getByPath(obj, 'a.b.c[0].d') // 1
80- * getByPath(obj, 'a.b.c.0') // { d: 1 }
81- * ```
82- * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/doPathValue.ts
83- */
56+ ? T [ TPath ]
57+ : TPath extends `${ArrayKey } `
58+ ? T extends readonly ( infer V ) [ ]
59+ ? V
60+ : never
61+ : never
62+ : never ;
63+
8464export function getByPath < T extends Record < string , any > , TPath extends Path < T > > (
8565 obj : T ,
8666 path : TPath ,
8767) : PathValue < T , TPath > {
88- return path . split ( '.' ) . reduce ( ( acc , key ) => acc ?. [ key ] , obj ) as PathValue < T , TPath >
68+ return path . split ( '.' ) . reduce ( ( acc , key ) => acc ?. [ key ] , obj ) as PathValue < T , TPath > ;
8969}
9070
9171export function setByPath < T extends Record < string , any > , TPath extends Path < T > > (
9272 obj : T ,
9373 path : TPath ,
9474 value : PathValue < T , TPath > ,
95- ) {
96- const segments = path . split ( '.' ) as TPath [ ]
97- const lastKey = segments . pop ( )
75+ ) : T {
76+ const segments = path . split ( '.' ) as TPath [ ] ;
77+ const lastKey = segments . pop ( ) ;
9878
99- let target : T = obj
79+ let target : T = obj ;
10080
10181 for ( let i = 0 ; i < segments . length ; i ++ ) {
102- const key = segments [ i ] as TPath
103- if ( ! ( key in target ) )
104- target [ key ] = { } as PathValue < T , TPath >
105-
106- target = target [ key ]
82+ const key = segments [ i ] as TPath ;
83+ if ( ! ( key in target ) ) {
84+ target [ key ] = { } as PathValue < T , TPath > ;
85+ }
86+ target = target [ key ] ;
10787 }
10888
109- if ( lastKey )
110- target [ lastKey ] = value
89+ if ( lastKey ) {
90+ target [ lastKey ] = value ;
91+ }
11192
112- return obj
113- }
93+ return obj ;
94+ }
0 commit comments