array-util-js v1.0.4
array-util-js
array-util-js is a lightweight package that has basic array processing functions. Type safety is a feature of this package though it is not perfect. Please open issues as needed; this project is still a WIP.
NPM Link: https://www.npmjs.com/package/array-util-js
Quick start:
Import the library into your code and start coding!
const ArrayUtil = require('array-util-js');
console.log(ArrayUtil.median([2, 3, 4, 1, 6, 9]));
/* Should log:
{ indices: [ 1 ], medians: [ 3 ] }
*/
Documentation
Functions in this package:
- rotate
- mean
- median
- medianIndices
- sameType
- isArray
- sort
- insert
- cycle
- shuffle
- areEqual
- min
- max
- bounds
- reduce
- dimensions
- closestPair
- magnitude
- normalize
- dot
rotate
rotate(array, direction, degrees)
Returns an array that has been rotated clockwise or counterclockwise by a multiple of 90 degrees
param | type | description |
---|---|---|
array | Array | a rectangular array |
direction | String | 'clockwise' or 'counterclockwise' |
degrees | Integer | number that is multiple of 90 |
Defaults
- direction :
'clockwise'
- degrees :
90
ArrayUtil.rotate([[2, 3], [3, 2]], 'clockwise', 90);
/* returns:
[ [ 3, 2 ], [ 2, 3 ] ]
*/
mean
mean(array, type)
Returns the mean of an array
param | type | description |
---|---|---|
array | Array | a normal array of numbers |
type | String | 'arithmetic' , 'geometric' , or 'harmonic' |
Defaults
- type :
'arithmetic'
ArrayUtil.mean([1, 2, 3, 4, 5], 'arithmetic');
/* returns:
3
*/
median
median(array, medianCount)
Returns the median(s) and indice(s) of the median(s) of an array in an object
param | type | description |
---|---|---|
array | Array | a normal array of numbers |
medianCount | number | number of medians to be generated |
Defaults
- medianCount :
1
ArrayUtil.median([1, 2, 3, 4, 5], 1);
/* returns:
{ indices: [ 2 ], medians: [ 3 ] }
*/
ArrayUtil.median([1, 2, 3, 4, 5, 6, 7], 3);
/* returns:
{ indices: [ 1, 3, 4 ], medians: [ 2, 4, 5 ] }
*/
medianIndices
medianIndices(array, medianCount)
Returns the indices of the median(s) of an array
param | type | description |
---|---|---|
array | Array | a normal array of numbers |
medianCount | number | number of medians to be generated |
Defaults
- medianCount :
1
ArrayUtil.medianIndices([1, 2, 3, 4, 5], 1);
/* returns:
2
*/
ArrayUtil.medianIndices([1, 2, 3, 4, 5, 6, 7], 3);
/* returns:
[ 1, 3, 4 ]
*/
sameType
sameType(array, defaultType)
Checks if an arrays contains only one type of variable
param | type | description |
---|---|---|
array | Array | a normal array |
defaultType | string | typeof() variable that is needed |
Defaults
- defaultType :
typeof()
first variable inarray
ArrayUtil.sameType([1, 2, ':)', 4, 5], 'string');
/* returns:
false
*/
ArrayUtil.sameType([1, 2, 3, 4, 5, 6, 7]);
/* returns:
true
*/
isArray
isArray(array)
Checks if a variable is an array
param | type | description |
---|---|---|
array | Array | a normal array |
ArrayUtil.isArray(':)');
/* returns:
false
*/
ArrayUtil.isArray([1, 2, 3, 4, 5, 6, 7]);
/* returns:
true
*/
sort
sort(array, direction)
Returns an array sorted in ascending or descending order
param | type | description |
---|---|---|
array | Array | a normal array of numbers |
direction | string | 'ascending' or 'descending' |
Defaults
- direction :
'ascending'
ArrayUtil.sameType([1, 4, 3, 2, 6, 0], 'descending');
/* returns:
[ 6, 4, 3, 2, 1, 0 ]
*/
insert
insert(array, value, index)
Returns an array with a value inserted into it
param | type | description |
---|---|---|
array | Array | a normal array |
value | var | variable to be inserted into the array |
index | number | index to insert the variable at |
ArrayUtil.insert([0, 1, 2, 3, 5, 6], 4, 4));
/* returns:
[ 0, 1, 2, 3, 4, 5, 6 ]
*/
cycle
cycle(array, direction, count)
Returns an array shifted ("cycled") left or right n-times
param | type | description |
---|---|---|
array | Array | a normal array |
direction | string | 'left' or 'right' |
count | string | number of times to cycle the array |
Defaults
- direction :
'right'
- count :
1
ArrayUtil.cycle([0, 1, 2, 3, 4, 5, 6], 'right', 3);
/* returns:
[ 4, 5, 6, 0, 1, 2, 3 ]
*/
shuffle
shuffle(array)
Returns an array shuffled with the Fisher-Yates/Knuth Algorithm
param | type | description |
---|---|---|
array | Array | a normal array |
ArrayUtil.shuffle([0, 1, 2, 3, 4, 5, 6]);
/* returns:
[ 5, 0, 3, 6, 1, 4, 2 ]
// results will vary every time
*/
areEqual
areEqual(array1, array2)
Checks if two arrays are equal (===)
param | type | description |
---|---|---|
array1 | Array | a normal array |
array2 | Array | a normal array |
ArrayUtil.areEqual([0, 1, '3'], [0, 1, '3']);
/* returns:
true
*/
ArrayUtil.areEqual([0, 1, '3'], [0, '1', 2, '3']);
/* returns:
false
*/
min
min(array, count)
Returns object of the index(-ices) of the minimum value(s) and the minimum value(s) of an array
param | type | description |
---|---|---|
array | Array | a normal array of numbers |
count | string | number of minimum values |
Defaults
- count :
1
ArrayUtil.min([1, 2, 3, 4], 2);
/* returns:
{ values: [ 1, 2 ], indices: [ 0, 1 ] }
*/
ArrayUtil.min([5, 4, 3, 2], 1);
/* returns:
{ values: 2, indices: 3 }
*/
max
max(array, count)
Returns object of the index(-ices) of the maximum value(s) and the maximum value(s) of an array
param | type | description |
---|---|---|
array | Array | a normal array of numbers |
count | string | number of maximum values |
Defaults
- count :
1
ArrayUtil.max([1, 2, 3, 4], 2);
/* returns:
{ values: [ 4, 3 ], indices: [ 3, 2 ] }
*/
ArrayUtil.max([5, 4, 3, 2], 1);
/* returns:
{ values: 5, indices: 0 }
*/
bounds
bounds(array)
Returns object of the indices of the bounds and the bounds of the arrays
param | type | description |
---|---|---|
array | Array | a normal array of numbers |
ArrayUtil.bounds([2, 4, 3, 1, 5]);
/* returns:
{ values: [ 1, 5 ], indices: [ 3, 4 ] }
*/
reduce
reduce(array, function)
Returns a reduced array
param | type | description |
---|---|---|
array | Array | a normal array of numbers |
function | string | 'add' , 'subtract' , 'multiply' , or 'divide' |
Defaults
- function :
'add'
ArrayUtil.reduce([1, 2, 3, 4, 5, 6], 'multiply');
/* returns:
720
*/
dimensions
dimensions(array)
Returns the dimensions of a "box-y" array (ie. no arrays of different length within the same array)
param | type | description |
---|---|---|
array | Array | a "box-y" multidimensional array of numbers |
ArrayUtil.dimensions([[2, 3, 4], [6, 0, 7]]);
/* returns:
[ 2, 3 ]
*/
ArrayUtil.dimensions(Array(5).fill(Array(12).fill(Array(2).fill(Array(3).fill(0)))));
/* returns:
[ 5, 12, 2, 3 ]
*/
closestPair
closestPair(array, system)
Returns the pair of indices of the closest two values within an array
param | type | description |
---|---|---|
array | Array | an array filled with same dimensional points (numbers) |
system | string | Lp distance to use |
Defaults
- system :
2
ArrayUtil.closestPair([[0, 2], [3, 7], [12, 0]]);
/* returns:
[ 0, 1 ]
*/
ArrayUtil.closestPair([0, 4, 8, 9, 12, 16]);
/* returns:
[ 2, 3 ]
*/
magnitude
magnitude(array)
Returns the magnitude of a vector (array)
param | type | description |
---|---|---|
array | Array | an array of numbers |
ArrayUtil.magnitude([2, 4, 4]);
/* returns:
6
*/
normalize
normalize(array)
Returns a normalized vector (array)
param | type | description |
---|---|---|
array | Array | an array of numbers |
ArrayUtil.normalize([2, 4, 4]);
/* returns:
[ 0.3333333333333333, 0.6666666666666666, 0.6666666666666666 ]
*/
dot
dot(array)
Returns the dot product of two vectors (arrays)
param | type | description |
---|---|---|
array1 | Array | an array of numbers |
array2 | Array | an array of numbers |
ArrayUtil.dot([1, 3, 5], [2, 4, 6]);
/* returns:
44
*/