Utility functions to boost your productivity
sort
arraySort an array of objects by a property
Signature
sort<T extends Record<string, any>> (options: SortOptions<T>): T[]
Parameters
options
The sort options
options
T[]
.arr - The array to sort
options
keyof T
.prop - The property to sort by
options
boolean
.ascending=true] - Sort in ascending order
Returns
T[]
Returns the sorted array
Example
sort({ arr: [{name: 'Bob'}, {name: 'Alice'}], prop: 'name' });
// [{name: 'Alice'}, {name: 'Bob'}]
getUnique
arrayGet unique values from an array
Signature
getUnique<T> (data: T[], field?: string): T[]
Parameters
data
T[]
The array to filter
field
optionalstring
Optional field to use for uniqueness in objects
Returns
T[]
Returns array of unique values
Example
getUnique([1, 2, 2, 3]); // [1, 2, 3]
getUnique([{id: 1}, {id: 2}, {id: 1}], 'id'); // [{id: 1}, {id: 2}]
getLastElement
arrayGet the last element(s) from an array or object
Signature
getLastElement<T> (data: T[] | Record<string, any>): T[] | Record<string, any>
Returns
void
Example
getLastElement([1, 2, 3]); // [3]
getLastElement({a: 1, b: 2}); // {b: 2}
randomString
arrayGet a random string from an array
Signature
randomString (arr: string[]): string
Returns
void
Example
randomString(['apple', 'banana', 'cherry']); // 'banana' (random)
checkLength
arrayCheck if array length is less than size, return array or fallback
Signature
checkLength<T> (first: T[], second: string, size: number): T[] | string
Returns
void
Example
checkLength([1, 2], 'fallback', 5); // [1, 2]
checkLength([1, 2, 3, 4, 5, 6], 'fallback', 5); // 'fallback'
chunk
arrayCreates an array of elements split into groups the length of size
Signature
chunk<T> (array: T[], size: number = 1): T[][]
Parameters
array
T[]
The array to process
size
number
=1] - The length of each chunk
Returns
T[][]
Returns the new array of chunks
Example
chunk([1, 2, 3, 4], 2); // [[1, 2], [3, 4]]
chunk([1, 2, 3, 4, 5], 3); // [[1, 2, 3], [4, 5]]
compact
arrayCreates an array with all falsy values removed
Signature
compact<T> (array: (T | null | undefined | false | 0 | "")[]): T[]
Parameters
array
(T | null | undefined | false | 0 | "")[]
The array to compact
Returns
T[]
Returns the new array of filtered values
Example
compact([0, 1, false, 2, '', 3]); // [1, 2, 3]
concat
arrayCreates a new array concatenating array with any additional arrays and/or values
Signature
concat<T> (array: T[], ...values: (T | T[])[]): T[]
Parameters
array
T[]
The array to concatenate
values
...(T | T[])
The values to concatenate
Returns
T[]
Returns the new concatenated array
Example
concat([1], 2, [3], [[4]]); // [1, 2, 3, [4]]
difference
arrayCreates an array of array values not included in the other given arrays
Signature
difference<T> (array: T[], ...values: T[][]): T[]
Parameters
array
T[]
The array to inspect
values
...T[][]
The values to exclude
Returns
T[]
Returns the new array of filtered values
Example
difference([2, 1], [2, 3]); // [1]
differenceBy
arrayLike difference except that it accepts iteratee which is invoked for each element
Signature
differenceBy<T> (array: T[], values: T[], iteratee: (value: T) => any): T[]
Parameters
array
T[]
The array to inspect
values
T[]
The values to exclude
iteratee
(value: T) => any
The iteratee invoked per element
Returns
T[]
Returns the new array of filtered values
Example
differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [1.2]
differenceWith
arrayLike difference except that it accepts comparator which is invoked to compare elements
Signature
differenceWith<T> (array: T[], values: T[], comparator: (a: T, b: T) => boolean): T[]
Parameters
array
T[]
The array to inspect
values
T[]
The values to exclude
comparator
(a: T, b: T) => boolean
The comparator invoked per element
Returns
T[]
Returns the new array of filtered values
Example
differenceWith([{ 'x': 1 }, { 'x': 2 }], [{ 'x': 1 }], (a, b) => a.x === b.x); // [{ 'x': 2 }]
drop
arrayCreates a slice of array with n elements dropped from the beginning
Signature
drop<T> (array: T[], n: number = 1): T[]
Parameters
array
T[]
The array to query
n
number
=1] - The number of elements to drop
Returns
T[]
Returns the slice of array
Example
drop([1, 2, 3]); // [2, 3]
drop([1, 2, 3], 2); // [3]
dropRight
arrayCreates a slice of array with n elements dropped from the end
Signature
dropRight<T> (array: T[], n: number = 1): T[]
Parameters
array
T[]
The array to query
n
number
=1] - The number of elements to drop
Returns
T[]
Returns the slice of array
Example
dropRight([1, 2, 3]); // [1, 2]
dropRight([1, 2, 3], 2); // [1]
dropWhile
arrayCreates a slice of array excluding elements dropped from the beginning
Signature
dropWhile<T> (array: T[], predicate: (value: T) => boolean): T[]
Parameters
array
T[]
The array to query
predicate
(value: T) => boolean
The function invoked per iteration
Returns
T[]
Returns the slice of array
Example
dropWhile([1, 2, 3, 4], n => n < 3); // [3, 4]
dropRightWhile
arrayCreates a slice of array excluding elements dropped from the end
Signature
dropRightWhile<T> (array: T[], predicate: (value: T) => boolean): T[]
Parameters
array
T[]
The array to query
predicate
(value: T) => boolean
The function invoked per iteration
Returns
T[]
Returns the slice of array
Example
dropRightWhile([1, 2, 3, 4], n => n > 2); // [1, 2]
fill
arrayFills elements of array with value from start up to, but not including, end
Signature
fill<T> (array: T[], value: T, start: number = 0, end: number = array.length): T[]
Parameters
array
T[]
The array to fill
value
The value to fill array with
start
number
=0] - The start position
end
number
=array.length] - The end position
Returns
T[]
Returns the filled array
Example
fill([1, 2, 3], 'a'); // ['a', 'a', 'a']
fill([4, 6, 8, 10], '*', 1, 3); // [4, '*', '*', 10]
findIndex
arrayReturns the first index at which a given element can be found
Signature
findIndex<T> (array: T[], predicate: (value: T, index: number, array: T[]) => boolean, fromIndex: number = 0): number
Parameters
array
T[]
The array to inspect
predicate
(value: T, index: number, array: T[]) => boolean
The function invoked per iteration
fromIndex
number
=0] - The index to search from
Returns
number
Returns the index of the found element, else -1
Example
findIndex([1, 2, 3, 4], n => n % 2 === 0); // 1
findLastIndex
arrayLike findIndex except that it iterates over elements from right to left
Signature
findLastIndex<T> (array: T[], predicate: (value: T, index: number, array: T[]) => boolean, fromIndex: number = array.length - 1): number
Parameters
array
T[]
The array to inspect
predicate
(value: T, index: number, array: T[]) => boolean
The function invoked per iteration
fromIndex
number
=array.length-1] - The index to search from
Returns
number
Returns the index of the found element, else -1
Example
findLastIndex([1, 2, 3, 4], n => n % 2 === 1); // 2
flatten
arrayFlattens array a single level deep
Signature
flatten<T> (array: (T | T[])[]): T[]
Parameters
array
(T | T[])[]
The array to flatten
Returns
T[]
Returns the new flattened array
Example
flatten([1, [2, [3, [4]], 5]]); // [1, 2, [3, [4]], 5]
flattenDeep
arrayRecursively flattens array
Signature
flattenDeep<T> (array: any[]): T[]
Parameters
array
any[]
The array to flatten
Returns
T[]
Returns the new flattened array
Example
flattenDeep([1, [2, [3, [4]], 5]]); // [1, 2, 3, 4, 5]