JSPM

extra-array

2.4.37
  • 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: Experimental.

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

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

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

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

array.bsearch([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.
get Gets value at index (+ve, -ve).
set Sets value at index (+ve, -ve).
swap Exchanges two values.
index Gets zero-based index.
indexRange Gets index range of part of array.
length Gets length of part of array.
fill Fills with given value.
copy Copies part of array to another.
concat Appends arrays to the end.
slice Gets a part of array.
splice Removes or replaces existing values.
flat Flattens nested array to given depth.
cut Breaks array at/after given indices.
chunk Breaks array into chunks of given size.
cycle Gives values that cycle through an array.
repeat Repeats an array given times.
reverse Reverses the values.
rotate Rotates values in array.
interleave Places values of an array between another.
min Finds smallest value.
max Finds largest value.
range Finds smallest and largest values.
map Updates values based on map function.
filter Keeps the values which pass the test.
count Counts occurrences of similar value(s).
partition Segregates array keeping similar values together.
group Breaks array keeping similar values together.
split Breaks array considering test as separator.
zip Combines values from n arrays, with a function.
unique Removes duplicate elements.
union Gives values present in any array.
intersection Gives values present in both arrays.
difference Gives values of an array not present in another.
isUnique Checks if there are no duplicate values.
isDisjoint Checks if arrays have no value in common.
prefix Picks an arbitrary prefix.
infix Picks an arbitrary infix.
suffix Picks an arbitrary suffix.
subsequence Picks an arbitrary subsequence.
permutation Picks an arbitrary permutation.
prefixes Lists all possible prefixes.
infixes Lists all possible infixes.
suffixes Lists all possible suffixes.
subsequences Lists all possible subsequences.
permutations Lists all possible arrangements.
isPrefix Checks if array starts with a prefix.
isInfix Checks if array contains an infix.
isSuffix Checks if array ends with a suffix.
isSubsequence Checks if array has a subsequence.
isPermutation Checks if array has a permutation.
isEqual Checks if two arrays are equal.
compare Compares two arrays.
search Searches a value from left.
bsearch Binary searches value in sorted array.
find Finds index of left/rightmost value passing the test.
findIndex Finds index(es) of values passing the test.
sort Arranges values in an order.

nodef

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