Skip to content

Instantly share code, notes, and snippets.

@jeffskelton3
Created December 16, 2024 15:29
Show Gist options
  • Select an option

  • Save jeffskelton3/83cce93f10680201dfa453220a051b4b to your computer and use it in GitHub Desktop.

Select an option

Save jeffskelton3/83cce93f10680201dfa453220a051b4b to your computer and use it in GitHub Desktop.
export class ArrayHelpers {
/**
* Sorts an array of objects by a specified key in ascending or descending order.
*
* @template T - The type of objects in the array.
* @param {T[]} array - The array to be sorted.
* @param {keyof T} key - The key to sort the objects by.
* @param {'asc' | 'desc'} [order='asc'] - The order to sort the objects. Defaults to 'asc'.
* @returns {T[]} - The sorted array.
*/
static orderBy = <T>(
array: T[],
key: keyof T,
order: 'asc' | 'desc' = 'asc'
): T[] => {
return array.sort((a, b) => {
if (a[key] < b[key]) return order === 'asc' ? -1 : 1
if (a[key] > b[key]) return order === 'asc' ? 1 : -1
return 0
})
}
/**
* Groups an array of objects by a specified key.
*
* This function takes an array of objects and a key, and returns an object where
* each key is the value from the specified key in the objects, and the corresponding
* value is the object itself.
*
* @param {T[]} array - The array of objects to be grouped.
* @param {keyof T} key - The key by which to group the objects.
* @returns {Record<string, T>} An object where the keys are the values of the specified key in each object,
* and the values are the corresponding objects.
*/
static groupBy = <T>(array: T[], key: keyof T): Record<string, T> => {
const map = new Map()
array.forEach((a) => {
map.set(a[key], a)
})
return Object.fromEntries(map)
}
/**
* Executes a callback function a specified number of times and returns an array of the results.
*
* @param {function} cb - The callback function to execute. It takes one argument of unknown type and returns a value of type T.
* @param {number} n - The number of times to execute the callback function.
* @returns {T[]} An array containing the results of executing the callback function 'n' times.
*
* @template T
*/
static times = <T>(cb: (arg: unknown, i: number) => T, n: number): T[] => {
return Array.from({ length: n }).map(cb)
}
/**
* Removes duplicate values from an array.
*
* This function takes an array of any data type and returns a new array with all duplicate
* values removed. The order of the elements in the resulting array will be the same as their
* first occurrence in the input array.
*
* @param {unknown[]} array - The array from which to remove duplicate values.
* @returns {unknown[]} A new array containing only unique elements from the input array.
*/
static dedupe = (array: unknown[]): unknown[] => Array.from(new Set(array))
/**
* Filters out null, undefined, and false values from an array.
*
* @param {unknown[]} array - The array to filter.
* @returns {unknown[]} A new array containing only truthy values from the original array.
*/
static filterNotNull = (array: unknown[]): unknown[] => array.filter(Boolean)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment