-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove lodash dependencies
- Loading branch information
1 parent
122d422
commit 36b6c75
Showing
11 changed files
with
59 additions
and
54 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,34 @@ | ||
export const get: (obj: unknown, path: (string | string[]), defValue?: unknown) => unknown = (obj: unknown, path: string | string[], defValue: unknown) => { | ||
// If path is not defined or it has false value | ||
if (!path) { | ||
return undefined; | ||
} | ||
// Check if path is string or array. Regex : ensure that we do not have '.' and brackets. | ||
// Regex explained: https://regexr.com/58j0k | ||
const pathArray: string[] = Array.isArray(path) ? path : path.match(/([^[.\]])+/g); | ||
// Find value | ||
const result: unknown = pathArray.reduce( | ||
(prevObj: unknown, key: string) => prevObj && prevObj[key], | ||
obj | ||
); | ||
// If found value is undefined return default value; otherwise return the value | ||
return result === undefined ? defValue : result; | ||
}; | ||
|
||
export const set: (obj: unknown, path: (string | string[]), value: unknown) => void = (obj: unknown, path: string | string[], value: unknown) => { | ||
// Regex explained: https://regexr.com/58j0k | ||
const pathArray: (string | string[]) | RegExpMatchArray = Array.isArray(path) ? path : path.match(/([^[.\]])+/g); | ||
|
||
pathArray.reduce((acc: unknown, key: string, i: number) => { | ||
if (acc[key] === undefined) { | ||
acc[key] = {}; | ||
} | ||
if (i === pathArray.length - 1) { | ||
acc[key] = value; | ||
} | ||
return acc[key]; | ||
}, obj); | ||
}; | ||
|
||
export const intersection: (arr: unknown[], ...args: unknown[][]) => unknown[] = (arr: unknown[], ...args: unknown[][]) => | ||
arr.filter((item: unknown) => args.every((arr: unknown[]) => arr.includes(item))); |
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
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