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.last([1, 2, 3]);
// 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. |
push | Adds values to the end. |
pop | Removes last value. |
shift | Removes first value. |
unshift | Adds values to the start. |
copyWithin | Copies part of array within. |
copy | Copies part of array to another. |
fill | Fills with given value. |
slice$ | Keeps only the selected region. |
concat$ | Appends arrays to the end. |
splice | Removes or replaces existing values. |
flat | Flattens nested array to given depth. |
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. |
zip | Combines values from n arrays, with a function. |
map$ | Updates values based on map function. |
filter$ | Keeps the values which pass the test. |
min | Finds smallest value. |
max | Finds largest value. |
range | Finds smallest and largest values. |
count | Counts occurrences of value(s). |
partition | Breaks array into values, by test. |
group | Keeps similar values together and in order. |
cut | Breaks array at/after given indices. |
split | Breaks array considering test as separator. |
unique | Removes duplicate elements. |
union | Gives union of first array with another. |
intersection | Gives values of an array present in another. |
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 | Gives an arbitrary prefix. |
infix | Gives an arbitrary infix. |
suffix | Gives an arbitrary suffix. |
subsequence | Gives an arbitrary subsequence. |
permutation | Rearranges values in arbitrary order. |
prefixes | Lists all possible prefixes. |
infixes | Lists all possible infixes. |
suffixes | Lists all possible suffixes. |
subsequences | Lists all possible partial sequences. |
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. |
findRight | Finds index of rightmost value passing the test. |
findIndices | Finds indices of values passing the test. |
sort | Arranges values in an order. |
Browserified, minified version of this package is extra-array.min.