@ztimson/utils / array
array
Functions
addUnique()
addUnique<
T>(array,el):T[]
Only add element to array if it isn't already included
Type Parameters
• T
Parameters
• array: T[]
Target array element will be added to
• el: T
Unique element to add
Returns
T[]
Array with element if it was unique
Example
const arr = addUnique([1, 2, 3], 3);
console.log(arr); // Output: [1, 2, 3]
Deprecated
Use ASet to create unique arrays
Defined in
src/array.ts:17
arrayDiff()
arrayDiff(
a,b):any[]
Find all unique elements in arrays
Parameters
• a: any[]
First array to compare
• b: any[]
Second array to compare
Returns
any[]
Unique elements
Deprecated
Use ASet to perform Set operations on arrays
Defined in
src/array.ts:30
caseInsensitiveSort()
caseInsensitiveSort(
prop): (a,b) =>number
Provides a shorthand for sorting arrays of complex objects by a string property
Parameters
• prop: string
Name of property to use, supports dot notation
Returns
Function
- Function to handle sort (Meant to be passed to Array.prototype.sort or used in sortFn)
Parameters
• a: any
• b: any
Returns
number
Example
let arr = [{a: 'Apple', b: 123}, {a: 'Carrot', b: 789}, {a: 'banana', b: 456}];
arr.sort(caseInsensitiveSort('a'));
Defined in
src/array.ts:49
findByProp()
findByProp(
prop,value): (v) =>boolean
Shorthand to find objects with a property value
Parameters
• prop: string
Property to compare (Dot nation supported)
• value: any
Value property must have
Returns
Function
Function used by filter or find
Parameters
• v: any
Returns
boolean
Example
const found = [
{name: 'Batman'},
{name: 'Superman'},
].filter(findByProp('name', 'Batman'));
Defined in
src/array.ts:73
flattenArr()
flattenArr(
arr,result):any[]
Recursively flatten nested arrays
Parameters
• arr: any[]
n-dimensional array
• result: any[] = []
Internal use only -- Keeps track of recursion
Returns
any[]
- Flattened array
Example
const arr = [
{label: null, url: '/'},
{label: 'Model Admin', url: '/model-admin'},
[
{label: 'Elements', url: '/model-admin/elements'},
{label: 'Example', url: null}
]
];
console.log(flattenArr(arr));
// Output:
[
{label: null, url: '/'},
{label: 'Model Admin', url: '/model-admin'},
{label: 'Elements', url: '/model-admin/elements'},
{label: 'Example', url: null}
]
Defined in
src/array.ts:105
makeArray()
makeArray<
T>(value):T[]
Make sure value is an array, if it isn't wrap it in one
Type Parameters
• T
Parameters
• value: T | T[]
Value that should be an array
Returns
T[]
Value in an array
Defined in
src/array.ts:155
makeUnique()
makeUnique(
arr):any[]
Make sure every element in array is unique
Parameters
• arr: any[]
Array that will be filtered in place
Returns
any[]
Original array
Deprecated
Please use ASet to create a guaranteed unique array
Defined in
src/array.ts:142
sortByProp()
sortByProp(
prop,reverse): (a,b) =>number
Provides a shorthand for sorting arrays of complex objects
Parameters
• prop: string
Name of property to use, supports dot notation
• reverse: boolean = false
Reverse the order of the sort
Returns
Function
- Function to handle sort (Meant to be passed to Array.prototype.sort)
Parameters
• a: any
• b: any
Returns
number
Example
let arr = [{a: {b: 2}}, {a: {b: 3}}, {a: {b: 1}}];
arr.sort(sortByProp('a.b'));
Defined in
src/array.ts:123