JSPM

extra-array

2.2.10
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1577
  • Score
    100M100P100Q102801F
  • License MIT

An array is a collection of values, stored contiguously.

Package Exports

  • extra-array

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (extra-array) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

An array is a collection of values, stored contiguously. This was my attempt at a simpler array manipulation API. Most are immutable methods, and do not modify the arguments. Any that do for performance reasons end in $. Many methods, like union() also accept a compare function. More performant versions accept a map function instead to avoid O(n²) calls, and end with On. I find this map-approach beautiful, which i learned from Haskell's sortOn().

You can notice that i have followed Javascript naming scheme as far as possible. Some names are borrowed from Haskell, Python, Java, Processing. They are grouped together by function, in reference below. Did i miss some really useful method?

Each method is also available as separate package for use by bundling tools, like browserify, rollup, uglify-js.

Stability: Stable.

const array = require('extra-array');

array.last([1, 2, 3]);
// 3

var a = [1, 2, 3, 4];
array.swap(a, 0, 1);
// [2, 1, 3, 4]

array.linspace(0, -1, 5);
// [0, -0.25, -0.5, -0.75, -1]

var a = [1, 2, 3, 4];
array.rotate(a, 1);
// [4, 1, 2, 3]

array.bsearchl([1, 3, 5, 7], 5);
// 2                  ^ found

[...array.permutations([1, 2, 3])];
// [
//   [ 1, 2, 3 ],
//   [ 2, 1, 3 ],
//   [ 1, 3, 2 ],
//   [ 3, 1, 2 ],
//   [ 2, 3, 1 ],
//   [ 3, 2, 1 ]
// ]

reference

Method Action
is Checks if value is array.
head Gets first value.
tail Gets values except first.
init Gets values except last.
last Gets last value.
get Gets value at index (+ve, -ve).
getAll Gets value at indices (+ve, -ve).
getLerp Gets value at fractional index.
set Sets value at index (+ve, -ve).
set$ Searches a value from right.
swap Exchanges two values.
swap$ Exchanges two values.
range Lists all possible prefixes.
linspace Returns evenly spaced values within given interval.
push Removes last value.
pop Removes or replaces existing values.
shift Sets value at index (+ve, -ve).
unshift Adds values to the start.
copyWithin Copies part of array within.
copy Copies part of array to another.
copy$ Copies part of array to another.
fill Fills with given value.
slice$ Rearranges values in arbitrary order.
concat$ Appends arrays to the end.
splice Keeps only the selected region.
flatten Flattens nested array to given depth.
chunk Breaks array into chunks of given size.
repeat Adds values to the end.
reverse Repeats an array given times.
rotate Rotates values in array.
rotate$ Reverses the values.
shuffle Rearranges values in arbitrary order.
shuffle$ Removes first value.
zip Combines values from n arrays, with a function.
map$ Returns evenly spaced values within given interval.
filter$ Keeps the values which pass the test.
count Counts occurrences of a value.
countOn Counts occurrences of a value.
countAllOn Counts occurrences of values.
partition Updates values based on map function.
partitionOn Breaks array into values, by test.
group Keeps similar values together and in order.
groupOn Keeps similar values together and in order.
cut Breaks array at given indices.
cutRight Breaks array after given indices.
split Breaks array considering filter as separator.
unique Removes duplicate elements.
uniqueOn Removes duplicate elements.
union Gives union of first array with another.
union$ Gives union of first array with another.
unionOn Gives union of first array with another.
unionOn$ Gives union of first array with another.
intersection Gives values of an array present in another.
intersectionOn Gives values of an array present in another.
difference Gives values of an array not present in another.
differenceOn Gives values of an array not present in another.
isDisjoint Checks if arrays have no value in common.
isDisjointOn Checks if arrays have no value in common.
prefixes Lists all possible arrangements.
suffixes Lists all possible suffixes.
subsequences Lists all possible partial sequences.
permutations Breaks array into values, by map.
infixes Lists all possible infixes.
isPrefix Checks if array starts with a prefix.
isPrefixOn Checks if array starts with a prefix.
isInfix Checks if array contains an infix.
isInfixOn Checks if array contains an infix.
isSuffix Checks if array ends with a suffix.
isSuffixOn Checks if array ends with a suffix.
isSubsequence Checks if array has a subsequence.
isSubsequenceOn Checks if array has a subsequence.
isPermutation Checks if array has a permutation.
isPermutationOn Checks if array has a permutation.
isEqual Checks if two arrays are equal.
compare Compares two arrays.
search Rotates values in array.
searchAll Searches a value from left.
searchRight Searches a value throughout.
bsearch Binary searches leftmost value in sorted array.
bsearchAny Binary searches value in sorted array.
bsearchClosest Binary searches closest value in sorted array.
bsearchRight Binary searches rightmost value in sorted array.
findIndices Finds indices of values passing the test.
findRight Finds index of rightmost value passing the test.
sort Arranges values in an order.
sortOn Arranges values in an order.
sortOn$ Arranges values in an order.

nodef

Browserified, minified version of this package is extra-array.min.