-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from newclarityex/main
Added dot notation along with appropriate types
- Loading branch information
Showing
4 changed files
with
108 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
type PathImpl<T, K extends keyof T> = | ||
K extends string | ||
? T[K] extends Record<string, any> | ||
? T[K] extends ArrayLike<any> | ||
? K | `${K}.${PathImpl<T[K], Exclude<keyof T[K], keyof any[]>>}` | ||
: K | `${K}.${PathImpl<T[K], keyof T[K]>}` | ||
: K | ||
: never; | ||
|
||
export type Path<T> = PathImpl<T, keyof T> | keyof T; | ||
|
||
export type PathValue<T, P extends Path<T>> = | ||
P extends `${infer K}.${infer Rest}` | ||
? K extends keyof T | ||
? Rest extends Path<T[K]> | ||
? PathValue<T[K], Rest> | ||
: never | ||
: never | ||
: P extends keyof T | ||
? T[P] | ||
: never; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { Path, PathValue } from '../types/dot-notation'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function getDotNotation<SettingsSchema, K extends Path<SettingsSchema>>(obj: SettingsSchema, path: K): PathValue<SettingsSchema, K> | undefined { | ||
if (typeof path !== 'string') throw 'Error: path must be a string'; | ||
|
||
const keys = path.split('.'); | ||
|
||
let current: any = obj; | ||
for (let i = 0; i < keys.length; i++) { | ||
const key = keys[i]; | ||
|
||
if (current[key] === undefined) { | ||
return undefined; | ||
} else { | ||
current = current[key]; | ||
} | ||
} | ||
|
||
return current; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export function setDotNotation<SettingsSchema, K extends Path<SettingsSchema>>(obj: SettingsSchema, path: K, value: PathValue<SettingsSchema, K>): PathValue<SettingsSchema, K> | undefined { | ||
if (typeof path !== 'string') throw 'Error: path must be a string'; | ||
|
||
const keys = path.split('.'); | ||
|
||
let current = obj; | ||
for (let i = 0; i < keys.length - 1; i++) { | ||
const key = keys[i]; | ||
|
||
if (current[key] === undefined) { | ||
return undefined; | ||
} | ||
|
||
current = current[key]; | ||
} | ||
|
||
if (current[keys[keys.length - 1]] === undefined) return undefined; | ||
|
||
current[keys[keys.length - 1]] = value; | ||
return value; | ||
} |