Generic Functions Logo

Generic Functions

Utility functions to boost your productivity

sort

array

Sort an array of objects by a property

Since v0.8.0

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

Return ValueT[]

Returns the sorted array

Example

sort Example
sort({ arr: [{name: 'Bob'}, {name: 'Alice'}], prop: 'name' });
// [{name: 'Alice'}, {name: 'Bob'}]
Source: src/core/array.ts

getUnique

array

Get unique values from an array

Since v0.8.0

Signature

getUnique<T> (data: T[], field?: string): T[]

Parameters

data
T[]

The array to filter

fieldoptional
string

Optional field to use for uniqueness in objects

Returns

Return ValueT[]

Returns array of unique values

Example

getUnique Example
getUnique([1, 2, 2, 3]); // [1, 2, 3]
getUnique([{id: 1}, {id: 2}, {id: 1}], 'id'); // [{id: 1}, {id: 2}]
Source: src/core/array.ts

getLastElement

array

Get the last element(s) from an array or object

Since v0.8.0

Signature

getLastElement<T> (data: T[] | Record<string, any>): T[] | Record<string, any>

Returns

Return Valuevoid

Example

getLastElement Example
getLastElement([1, 2, 3]); // [3]
getLastElement({a: 1, b: 2}); // {b: 2}
Source: src/core/array.ts

randomString

array

Get a random string from an array

Since v0.8.0

Signature

randomString (arr: string[]): string

Returns

Return Valuevoid

Example

randomString Example
randomString(['apple', 'banana', 'cherry']); // 'banana' (random)
Source: src/core/array.ts

checkLength

array

Check if array length is less than size, return array or fallback

Since v0.8.0

Signature

checkLength<T> (first: T[], second: string, size: number): T[] | string

Returns

Return Valuevoid

Example

checkLength Example
checkLength([1, 2], 'fallback', 5); // [1, 2]
checkLength([1, 2, 3, 4, 5, 6], 'fallback', 5); // 'fallback'
Source: src/core/array.ts

chunk

array

Creates an array of elements split into groups the length of size

Since v0.9.0

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

Return ValueT[][]

Returns the new array of chunks

Example

chunk Example
chunk([1, 2, 3, 4], 2); // [[1, 2], [3, 4]]
chunk([1, 2, 3, 4, 5], 3); // [[1, 2, 3], [4, 5]]
Source: src/core/array.ts

compact

array

Creates an array with all falsy values removed

Since v0.9.0

Signature

compact<T> (array: (T | null | undefined | false | 0 | "")[]): T[]

Parameters

array
(T | null | undefined | false | 0 | "")[]

The array to compact

Returns

Return ValueT[]

Returns the new array of filtered values

Example

compact Example
compact([0, 1, false, 2, '', 3]); // [1, 2, 3]
Source: src/core/array.ts

concat

array

Creates a new array concatenating array with any additional arrays and/or values

Since v0.9.0

Signature

concat<T> (array: T[], ...values: (T | T[])[]): T[]

Parameters

array
T[]

The array to concatenate

values
...(T | T[])

The values to concatenate

Returns

Return ValueT[]

Returns the new concatenated array

Example

concat Example
concat([1], 2, [3], [[4]]); // [1, 2, 3, [4]]
Source: src/core/array.ts

difference

array

Creates an array of array values not included in the other given arrays

Since v0.9.0

Signature

difference<T> (array: T[], ...values: T[][]): T[]

Parameters

array
T[]

The array to inspect

values
...T[][]

The values to exclude

Returns

Return ValueT[]

Returns the new array of filtered values

Example

difference Example
difference([2, 1], [2, 3]); // [1]
Source: src/core/array.ts

differenceBy

array

Like difference except that it accepts iteratee which is invoked for each element

Since v0.9.0

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

Return ValueT[]

Returns the new array of filtered values

Example

differenceBy Example
differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor); // [1.2]
Source: src/core/array.ts

differenceWith

array

Like difference except that it accepts comparator which is invoked to compare elements

Since v0.9.0

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

Return ValueT[]

Returns the new array of filtered values

Example

differenceWith Example
differenceWith([{ 'x': 1 }, { 'x': 2 }], [{ 'x': 1 }], (a, b) => a.x === b.x); // [{ 'x': 2 }]
Source: src/core/array.ts

drop

array

Creates a slice of array with n elements dropped from the beginning

Since v0.9.0

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

Return ValueT[]

Returns the slice of array

Example

drop Example
drop([1, 2, 3]); // [2, 3]
drop([1, 2, 3], 2); // [3]
Source: src/core/array.ts

dropRight

array

Creates a slice of array with n elements dropped from the end

Since v0.9.0

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

Return ValueT[]

Returns the slice of array

Example

dropRight Example
dropRight([1, 2, 3]); // [1, 2]
dropRight([1, 2, 3], 2); // [1]
Source: src/core/array.ts

dropWhile

array

Creates a slice of array excluding elements dropped from the beginning

Since v0.9.0

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

Return ValueT[]

Returns the slice of array

Example

dropWhile Example
dropWhile([1, 2, 3, 4], n => n < 3); // [3, 4]
Source: src/core/array.ts

dropRightWhile

array

Creates a slice of array excluding elements dropped from the end

Since v0.9.0

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

Return ValueT[]

Returns the slice of array

Example

dropRightWhile Example
dropRightWhile([1, 2, 3, 4], n => n > 2); // [1, 2]
Source: src/core/array.ts

fill

array

Fills elements of array with value from start up to, but not including, end

Since v0.9.0

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

Return ValueT[]

Returns the filled array

Example

fill Example
fill([1, 2, 3], 'a'); // ['a', 'a', 'a']
fill([4, 6, 8, 10], '*', 1, 3); // [4, '*', '*', 10]
Source: src/core/array.ts

findIndex

array

Returns the first index at which a given element can be found

Since v0.9.0

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

Return Valuenumber

Returns the index of the found element, else -1

Example

findIndex Example
findIndex([1, 2, 3, 4], n => n % 2 === 0); // 1
Source: src/core/array.ts

findLastIndex

array

Like findIndex except that it iterates over elements from right to left

Since v0.9.0

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

Return Valuenumber

Returns the index of the found element, else -1

Example

findLastIndex Example
findLastIndex([1, 2, 3, 4], n => n % 2 === 1); // 2
Source: src/core/array.ts

flatten

array

Flattens array a single level deep

Since v0.9.0

Signature

flatten<T> (array: (T | T[])[]): T[]

Parameters

array
(T | T[])[]

The array to flatten

Returns

Return ValueT[]

Returns the new flattened array

Example

flatten Example
flatten([1, [2, [3, [4]], 5]]); // [1, 2, [3, [4]], 5]
Source: src/core/array.ts

flattenDeep

array

Recursively flattens array

Since v0.9.0

Signature

flattenDeep<T> (array: any[]): T[]

Parameters

array
any[]

The array to flatten

Returns

Return ValueT[]

Returns the new flattened array

Example

flattenDeep Example
flattenDeep([1, [2, [3, [4]], 5]]); // [1, 2, 3, 4, 5]
Source: src/core/array.ts