Skip to content

Commit

Permalink
Add some doc to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ya-erm committed Nov 18, 2023
1 parent 89eef04 commit 10244d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/lib/utils/arrayToDict.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
export function arrayToDict<T, S>(array: T[], map: (item: T) => S, key: (item: S) => string | undefined) {
/**
* Converts an array to a dictionary using the provided mapping functions.
*
* @template T The type of the items in the array.
* @template S The type of the items in the resulting dictionary.
* @param {T[]} array The array to convert to a dictionary.
* @param {(item: T) => S} map The mapping function to transform each item in the array.
* @param {(item: S) => string | undefined} key The function to extract the key from each mapped item.
* @returns {Map<string, S>} The resulting dictionary.
*/
export function arrayToDict<T, S>(
array: T[],
map: (item: T) => S,
key: (item: S) => string | undefined,
): Map<string, S> {
const items = new Map<string, S>();
array.forEach((item) => {
const mapped = map(item);
Expand Down
6 changes: 6 additions & 0 deletions src/lib/utils/filterNotEmpty.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Filters out `null` and `undefined` values from an array.
*
* @param array The array to filter.
* @returns A new array with non-empty values.
*/
export function filterNotEmpty<T>(array?: T[]): NonNullable<T>[] {
return (array?.filter((item) => item !== null && item !== undefined) as NonNullable<T>[]) ?? [];
}

0 comments on commit 10244d5

Please sign in to comment.