-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove lodash in favour of local functions
- Loading branch information
1 parent
31b0a1b
commit 2877428
Showing
8 changed files
with
78 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
type DebouncedFunction<T extends (...args: any[]) => any> = { | ||
(...args: Parameters<T>): void; | ||
cancel: () => void; | ||
}; | ||
|
||
export function debounce<T extends (...args: any[]) => any>( | ||
func: T, | ||
wait: number, | ||
immediate: boolean = false | ||
): DebouncedFunction<T> { | ||
let timeout: NodeJS.Timeout | null = null; | ||
|
||
const debounced = function (this: any, ...args: Parameters<T>) { | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
const context = this; | ||
|
||
if (timeout) clearTimeout(timeout); | ||
|
||
if (immediate && !timeout) { | ||
func.apply(context, args); | ||
} | ||
|
||
timeout = setTimeout(() => { | ||
timeout = null; | ||
if (!immediate) { | ||
func.apply(context, args); | ||
} | ||
}, wait); | ||
}; | ||
|
||
debounced.cancel = function () { | ||
if (timeout) { | ||
clearTimeout(timeout); | ||
timeout = null; | ||
} | ||
}; | ||
|
||
return debounced as DebouncedFunction<T>; | ||
} |
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,35 @@ | ||
type Path = string | number | Array<string | number>; | ||
|
||
/** | ||
* Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned. | ||
* @param obj The object to query | ||
* @param path The path of the property to get | ||
* @param defaultValue The value returned for undefined resolved values | ||
* @returns The resolved value | ||
*/ | ||
export function get(obj: any, path: Path, defaultValue?: any): any { | ||
// Handle empty/null object | ||
if (obj == null) return defaultValue; | ||
|
||
// Convert path to array if it's a string | ||
const segments = Array.isArray(path) | ||
? path | ||
: path | ||
.toString() | ||
.replace(/\[(\w+)\]/g, ".$1") | ||
.split("."); | ||
|
||
// Traverse the object | ||
let result = obj; | ||
for (const segment of segments) { | ||
// Skip empty segments | ||
if (!segment) continue; | ||
|
||
result = result[segment]; | ||
|
||
// Return default value if we hit undefined/null | ||
if (result == null) return defaultValue; | ||
} | ||
|
||
return result; | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.