arrayex
v1.0.7
Published
A functional extension to Native Array Object
Downloads
325
Maintainers
Readme
Arrayex - enhance the native array operation
The native methods for array is really powerful in the new js standard, but still, some frequently used operations is missing. Arrayex is a extremely lightweight library that seperated from several projects, provides some frequently used array functions.
Install
The recommended way to install and maintain arrayex in your project is through the Node.js Pacakge Manager (NPM), simply type the npm command in your project folder:
npm install arrayex
Usage
The library doesn't extend the Array prototype, so you have call Arrayex.SomeFunc(array, ...params) to use it. The name of function and parameters is Straightforward so you really do not need a document to use it.
import { Arrayex } from 'arrayex'
let arr1 = [1,2,3,4,3]
Arrayex.Delete(arr1, 3) // arr1 = [1,2,4,3]
let arr2 = [3,1,2,3,4,3]
Arrayex.Delete(arr2, v => v >= 3, true) // arr2 = [1,2]
let arr3 = [1,2,3,4,3]
Arrayex.BatchDelete(arr3, [3, 4], true) // arr3 = [1,2]
let arr4 = [1,3,1,3,3]
Arrayex.Replace(arr3, 3, 1, true) // arr4 = [1,1,1,1,1]
let n1 = Arrayex.Find([1,2,3,4,5], val => val > 4) // 5
let p1 = Arrayex.IncludeSome([1,2,3,4,5], [7, 9, 5]) // true
let arr5 = [1, 3, 7, 9, 10]
let index1 = Arrayex.OrderedInsert(arr5, 5, (a, b) => a - b) // arr5 = [1, 3, 5, 7, 9, 10], index1 = 2
let index2 = Arrayex.OrderedInsert(arr5, 5, (a, b) => a - b, true) // arr5 = [1, 3, 5, 7, 9, 10], index1 = 2, with unique = true, no duplicate item will be inserted and the existing item index will returned.
let arr6 = [1,2,4]
Arrayex.InsertBefore(arr6, 4, 3) // arr6 = [1,2,3,4]
Arrayex.InsertAfter(arr6, 2, 9) // arr6 = [1,2,9,3,4]
let arr7 = [1.0, 2.000001, 3.0, 4.000000032, 4.5]
let n2 = Arrayex.Approximate(arr7, 4, 0.001) // 4.000000032, find approximate float value
let index3 = Arrayex.ApproximateIndex(arr7, 4, 2, 0.0001) // 1
let arr8 = Arrayex.Empty(3) // [null, null, null]
let arr9 = Arrayex.Create(4, (index, total) => index) // [0, 1, 2, 3]
let arr10 = Arrayex.Repeat(2/* total */, 1, 2, 3) // [1, 1, 2, 2, 3, 3], Repeat each element n times
let arr11 = Arrayex.RepeatSequence(3/* total */, 1, 2) // [1, 2, 1, 2, 1, 2], Repeat the initial sequence in order n times
let peroid1 = Arrayex.AnalyzePeriod([1,2,3,1,2,3,1,2,3,1,2,3]) // [1,2,3], since the array is repeat this sequence
let peroid2 = Arrayex.AnalyzePeriod([1,2,1,2,1]) // [1,2,1,2,1], the array do not has peroidic property
let flat1 = Arrayex.Flat([1, [2,3], [4]]) // [1,2,3,4]
let div1 = Arrayex.Divide([1,2,3,4,5,6,7], 3) // [[1,2,3], [4,5,6], [7]]
let group1 = Arrayex.Group([1, -1, 2, -2, 4, -4], (value, index, array) => value > 0 ? 'positive' : 'negative') // { positive: [1,2,4], negative: [-1,-2,-4] }
let sample = Arrayex.Sample([1,2,3,4,5,6,7,8], 3) // [1, 4, 7], equal-spaced sample 3 values
let sample = Arrayex.Sample([1,2,3,4,5,6,7,8], 3, true) // [2, 8, 3], randomly sample 3 values
let u1 = Arrayex.Union([[1,2,3], [3,4,5]]) // [1,2,3,3,4,5]
let u2 = Arrayex.Union([[1,2,3], [3,4,5]]) // [1,2,3,4,5], remove duplicates
let intersect = Arrayex.Intersect([[1,2,3], [2,3,4]]) // [2,3], the order of output follows the first array
let subtract = Arrayex.Subtract([1,2,3,4,5,6], [[6,5,9], [7, 8]]) // [1,2,3,4]
let nn = Arrayex.NonNull([1, null, 2, null, null, 3, 4, null]) //nn = [1,2,3,4]
License
Distributed under the MIT license. See LICENSE for detail.