Fast, efficient, and easy-to-use array extensions for TypeScript.
npm install @ariesclark/extensions
# Or directly, if you only need the array extension.
npm install @ariesclark/array
Groups the items in the array by the given function.
group([1, 2, 3, 4, 5], item => (item % 2 === 0 ? 'even' : 'odd'));
// {
// odd: [1, 3, 5],
// even: [2, 4]
// }
Get a random index from an array.
Parameters:
- array (
{ length: 0; }
) - random (
RandomFunction
)
returns: null
const value = ['lorem', 'ipsum', 'dolor', 'sit', 'amet'];
randomIndex(value); // 3, eventually.
randomIndex(value); // 0, eventually.
randomIndex(value); // 2, eventually.
Parameters:
- array (
{ length: number; }
) - random (
RandomFunction
)
returns: null
Parameters:
- array (
{ length: number; }
) - random (
RandomFunction
)
returns: null
Get a random item from an array, with an optional random number generator.
Parameters:
- array (
{ length: 0; }
) - random (
RandomFunction
)
returns: null
const value = ['lorem', 'ipsum', 'dolor', 'sit', 'amet'];
randomItem(value); // "amet", eventually.
randomItem(value); // "dolor", eventually.
randomItem(value); // "lorem", eventually.
Parameters:
- array (
{ [index: number]: T; length: number; }
) - random (
RandomFunction
)
returns: null
Parameters:
- array (
{ [index: number]: T; length: number; }
) - random (
RandomFunction
)
returns: null
Returns a new array with unique values based on the by
function.
Parameters:
- array (
T[]
) - by (
(value: T) => unknown
)
returns: T[]