-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove
lodash
as a dependency (#2378)
This will reduce the bundle size by ~23%(117kb). Only 4 methods were used `flatten`, `flatMap`, `omitBy`, and `groupBy`. `omitBy and `groupBy` were recreated while the es2019 implementations of `flatten` and `flatMap` are used. `lodash` is still used in the tests which is fine because it makes the tests cleaner. Closes #2118
- Loading branch information
Showing
13 changed files
with
229 additions
and
163 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
type ValueOf<T> = T[keyof T] | ||
|
||
/** | ||
* Creates an object composed of keys generated from the results of running each element of collection thru iteratee. | ||
* The order of grouped values is determined by the order they occur in collection. | ||
* The corresponding value of each key is an array of elements responsible for generating the key. | ||
* | ||
* Similar to lodash's groupBy | ||
* | ||
* @param array - array to iterate over | ||
* @param iteratee - function that returns key of the group to place the item | ||
* | ||
* @returns a map of arrays | ||
*/ | ||
export function groupBy<T>( | ||
array: T[], | ||
iteratee: (value: T, index: number, array: T[]) => string, | ||
): { [p: string]: T[] } { | ||
// eslint-disable-next-line max-params -- need all the params for the fallback | ||
return array.reduce<{ [key: string]: T[] }>(function predicate( | ||
acc, | ||
value, | ||
index, | ||
arrayReference, | ||
) { | ||
;(acc[iteratee(value, index, arrayReference)] ||= []).push(value) | ||
return acc | ||
}, | ||
{}) | ||
} | ||
|
||
/** | ||
* Creates an object composed of the own and inherited enumerable string keyed properties of object that | ||
* predicate doesn't return truthy for. | ||
* | ||
* @param obj - Object to have properties removed. | ||
* @param predicate - function that returns whether the property should be removed from the obj. | ||
* | ||
* @returns object | ||
*/ | ||
export function omitBy<T extends object>( | ||
obj: T, | ||
predicate: (objElement: ValueOf<T>, k: string | number | symbol) => boolean, | ||
): Partial<T> { | ||
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- We know the keys are properties of T | ||
const keys: Array<keyof T> = Object.keys(obj) as Array<keyof T> | ||
const keysToKeep = keys.filter((kb) => !predicate(obj[kb], kb)) | ||
return keysToKeep.reduce((acc: Partial<T>, key: keyof T) => { | ||
acc[key] = obj[key] | ||
return acc | ||
}, {}) | ||
} |
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