1.0.4 • Published 5 years ago

array-util-js v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

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

rotate(array, direction, degrees)

Returns an array that has been rotated clockwise or counterclockwise by a multiple of 90 degrees

paramtypedescription
arrayArraya rectangular array
directionString'clockwise' or 'counterclockwise'
degreesIntegernumber 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

paramtypedescription
arrayArraya normal array of numbers
typeString'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

paramtypedescription
arrayArraya normal array of numbers
medianCountnumbernumber 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

paramtypedescription
arrayArraya normal array of numbers
medianCountnumbernumber 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

paramtypedescription
arrayArraya normal array
defaultTypestringtypeof() variable that is needed
Defaults
  • defaultType : typeof() first variable in array
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

paramtypedescription
arrayArraya 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

paramtypedescription
arrayArraya normal array of numbers
directionstring'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

paramtypedescription
arrayArraya normal array
valuevarvariable to be inserted into the array
indexnumberindex 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

paramtypedescription
arrayArraya normal array
directionstring'left' or 'right'
countstringnumber 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

paramtypedescription
arrayArraya 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 (===)

paramtypedescription
array1Arraya normal array
array2Arraya 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

paramtypedescription
arrayArraya normal array of numbers
countstringnumber 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

paramtypedescription
arrayArraya normal array of numbers
countstringnumber 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

paramtypedescription
arrayArraya 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

paramtypedescription
arrayArraya normal array of numbers
functionstring'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)

paramtypedescription
arrayArraya "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

paramtypedescription
arrayArrayan array filled with same dimensional points (numbers)
systemstringLp 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)

paramtypedescription
arrayArrayan array of numbers
ArrayUtil.magnitude([2, 4, 4]);
/*  returns:
    6
*/

normalize

normalize(array)

Returns a normalized vector (array)

paramtypedescription
arrayArrayan 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)

paramtypedescription
array1Arrayan array of numbers
array2Arrayan array of numbers
ArrayUtil.dot([1, 3, 5], [2, 4, 6]);
/*  returns:
    44
*/
1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago