|
1 | | -import { Prop, Paths } from '../typings/types'; |
| 1 | +import type { Prop, Paths } from "../typings/types"; |
| 2 | + |
| 3 | +type If<B, F, S> = B extends true ? F : S; |
| 4 | +type Or<B1, B2> = B1 extends true ? true : B2; |
| 5 | +type Not<B> = B extends true ? false : true; |
| 6 | +type Has<U extends any, U1 extends any> = [U1] extends [U] ? true : false; |
| 7 | + |
| 8 | +type IsExactKey<T> = string extends T |
| 9 | + ? false |
| 10 | + : number extends T |
| 11 | + ? false |
| 12 | + : symbol extends T |
| 13 | + ? false |
| 14 | + : true; |
| 15 | + |
| 16 | +type ValueByPath<P, O, U = false> = P extends readonly [infer F, ...(infer R)] |
| 17 | + ? /** |
| 18 | + * In case we accessed optional property of O on the previous step |
| 19 | + * O can be `undefined`. keyof operator won't work on a union |
| 20 | + */ |
| 21 | + Or<Has<O, null>, Has<O, undefined>> extends infer HasNullOrUndefined |
| 22 | + ? Exclude<O, undefined | null> extends infer O2 |
| 23 | + ? O2 extends object |
| 24 | + ? F extends keyof O2 |
| 25 | + ? R extends [] |
| 26 | + ? If<Or<U, HasNullOrUndefined>, O2[F] | undefined, O2[F]> |
| 27 | + : ValueByPath< |
| 28 | + R, |
| 29 | + O2[F], |
| 30 | + Or< |
| 31 | + Or<U, HasNullOrUndefined>, |
| 32 | + /** |
| 33 | + * In case we run into some kind of dynamic dictionary |
| 34 | + * something like Record<string, ..> or Record<number, ..> |
| 35 | + * We want to make sure that we get T | undefined instead of T as a result |
| 36 | + */ |
| 37 | + Not<IsExactKey<keyof O2>> |
| 38 | + > |
| 39 | + > |
| 40 | + : undefined |
| 41 | + : never |
| 42 | + : never |
| 43 | + : never |
| 44 | + : never; |
2 | 45 |
|
3 | 46 | export interface Path { |
4 | | - <K extends Prop, O extends Record<K, any>>(path: [K], obj: O): O[K]; |
5 | | - <K extends Prop>(path: [K]): <O extends Record<K, any>>(obj: O) => O[K]; |
6 | | - <T>(path: Paths, obj): T | undefined; |
7 | | - <T>(path: Paths): (obj) => T | undefined; |
8 | | -} |
9 | | - |
10 | | -import curryN from '../function/curryN'; |
11 | | - |
12 | | -/** |
13 | | - * Retrieve the value at a given path. |
14 | | - * |
15 | | - * @param {[String]} paths The path to use. |
16 | | - * @param {Object} obj The object to retrieve the nested property from. |
17 | | - * @return {*} The data at `path`. |
18 | | - * @example |
19 | | - * |
20 | | - * path(['a', 'b'], {a: {b: 2}}); //=> 2 |
21 | | - * path(['a', 'b'], {c: {b: 2}}); //=> undefined |
22 | | - */ |
23 | | -export default curryN(2, <K extends Prop, O extends Record<K, any>>(paths: Paths = [], obj: O = {} as any) => { |
24 | | - let val = obj; |
25 | | - |
26 | | - for (let i = 0; i < paths.length; i++) { |
27 | | - if (val == null) { |
28 | | - return; |
29 | | - } |
30 | | - |
31 | | - val = val[paths[i]]; |
| 47 | + /** |
| 48 | + * Retrieve the value at a given path. |
| 49 | + * **Note:** Use `as const` cast on the `paths` for type inference. |
| 50 | + * |
| 51 | + * @param {[String]} paths The path to use. |
| 52 | + * @param {Object} obj The object to retrieve the nested property from. |
| 53 | + * @return {*} The data at `path`. |
| 54 | + * @example |
| 55 | + * |
| 56 | + * path(['a', 'b'], {a: {b: 2}}); //=> 2 |
| 57 | + * path(['a', 'b'], {c: {b: 2}}); //=> undefined |
| 58 | + */ |
| 59 | + (pathToProp: Prop[], obj: object): unknown; |
| 60 | + /** |
| 61 | + * Retrieve the value at a given path. |
| 62 | + * **Note:** Use `as const` cast on the `paths` for type inference. |
| 63 | + * |
| 64 | + * @param {[String]} paths The path to use. |
| 65 | + * @return {*} function to get data at `path` for a given object |
| 66 | + * @example |
| 67 | + * |
| 68 | + * path(['a', 'b'])({a: {b: 2}}); //=> 2 |
| 69 | + * path(['a', 'b'])({c: {b: 2}}); //=> undefined |
| 70 | + */ |
| 71 | + (pathToProp: Prop[]): (obj: object) => unknown; |
| 72 | + /** |
| 73 | + * @deprecated |
| 74 | + * Please use `path` without type parameters instead. |
| 75 | + * Make sure to use `as const` cast for props array for type inference |
| 76 | + * @example |
| 77 | + * |
| 78 | + * path(['a', 'b'] as const, { a: { b: 2 } }); |
| 79 | + */ |
| 80 | + <T>(pathToProp: Prop[], obj: object): T | undefined; |
| 81 | + /** |
| 82 | + * @deprecated |
| 83 | + * Please use `path` without type parameters instead. |
| 84 | + * Make sure to use `as const` cast for props array for type inference |
| 85 | + * @example |
| 86 | + * |
| 87 | + * path(['a', 'b'] as const)({ a: { b: 2 } }); |
| 88 | + */ |
| 89 | + <T>(pathToProp: Prop[]): (obj: object) => T | undefined; |
| 90 | + /** |
| 91 | + * Retrieve the value at a given path. |
| 92 | + * |
| 93 | + * @param {[String]} paths The path to use. |
| 94 | + * @param {Object} obj The object to retrieve the nested property from. |
| 95 | + * @return {*} The data at `path`. |
| 96 | + * @example |
| 97 | + * |
| 98 | + * const johnDoe = { |
| 99 | + * fio: { |
| 100 | + * firstName: 'John', |
| 101 | + * lastName: 'Doe', |
| 102 | + * }, |
| 103 | + * }; |
| 104 | + * const firstName = path(['fio', 'firstName'] as const, johnDoe); // => 'John' |
| 105 | + */ |
| 106 | + <P extends Paths, O>(pathToProp: P, obj: O): ValueByPath<P, O>; |
| 107 | + /** |
| 108 | + * Retrieve the value at a given path. |
| 109 | + * |
| 110 | + * @param {[String]} paths The path to use. |
| 111 | + * @return {*} function to get data at `path` for a given object |
| 112 | + * @example |
| 113 | + * |
| 114 | + * const johnDoe = { |
| 115 | + * fio: { |
| 116 | + * firstName: 'John', |
| 117 | + * lastName: 'Doe', |
| 118 | + * }, |
| 119 | + * }; |
| 120 | + * const getLastName = path(['fio', 'lastName'] as const); |
| 121 | + * const lastName = getLastName(johnDoe); // => 'Doe' |
| 122 | + */ |
| 123 | + <P extends Paths>(pathToProp: P): <O>(obj: O) => ValueByPath<P, O>; |
| 124 | +}; |
| 125 | + |
| 126 | +import curryN from "../function/curryN"; |
| 127 | + |
| 128 | +const _path = <K extends Prop, O extends Record<K, any>>( |
| 129 | + paths: Paths = [], |
| 130 | + obj: O = {} as any |
| 131 | +) => { |
| 132 | + let val = obj; |
| 133 | + |
| 134 | + for (let i = 0; i < paths.length; i++) { |
| 135 | + if (val == null) { |
| 136 | + return undefined; |
32 | 137 | } |
33 | 138 |
|
34 | | - return val; |
35 | | -}) as Path; |
| 139 | + val = val[paths[i]]; |
| 140 | + } |
| 141 | + |
| 142 | + return val; |
| 143 | +}; |
| 144 | + |
| 145 | +export default curryN(2, _path) as Path; |
0 commit comments