util-array
v0.4.5
Published
Small js lib
Downloads
46
Readme
#ArrayUtil
This small library adds a small object that is a wrapper for the standard class array in JavaScript. You just need to write something like ArrayUtil(array, [fn])
, where the array
is an array that you had before, and fn
, which is an optional argument, is a function of the comparison of elements in the array.
After that you will be available as methods of the class Array
, and many other methods described below.
For example: ArrayUtil([1, 2, 3]).isOrdered()
return true.
And i'm sorry for my English :)
###Library methods
add(value, index) - add value to the array. If index not passed is work like
push()`, but return current object.
allIndexOf(a[, b[, c[...]]])
- returns an instance of ArrayUtil
containing all indexes of passed elements. If obtained one argument and it is an array, it will look for items that are in it.
For example:
AUtil( [1, 1, 2, 3] ).allIndexOf(1, 2, 4)
return [[0, 1], [2], []]
AUtil( [1, 1, 2, 3] ).allIndexOf(1)
return [0, 1]
AUtil( [1, 1, 2, 3] ).allIndexOf([1])
return [0, 1]
binarySearch(item)
- return index of item
in object or -1 if there is no such element. Important! It will work correctly only for sorted objects.
clear()
- remove all undefined
from object and return it.
clone()
- return copy of current object. It's not deep copying, so reffered types will not be copied.
deleteIf(fun)
- removes all values from the object for which fun
returns true and return it.
drop(index)
- remove element at current index. If index not passed is work like pop()
, but return current object.
empty()
- remove all elements from object and return it.
first()
- return first element of object or undefined if there is no elements.
greaterOrEqual(than)
- returns an instance of ArrayUtil
with values greater or equal than the passed.
greaterThan(than)
- returns an instance of ArrayUtil
with values greater than the passed.
has(a[, b[, c[...]]])
- take one or more arguments. Return true
if object has all of this arguments. If obtained one argument and it is an array, it will look for items that are in it.
hasAny(a[, b[, c[...]]])
- take one or more arguments. Return true
if object has at least one of this arguments. If obtained one argument and it is an array, it will look for items that are in it.
isEmpty()
- return true if object not contain any element.
isOrdered()
- return true
if object ordered by ascending.
isOrderedByDesc()
- return true
if object ordered by descending.
inRange(from, to)
- return instance of ArrayUtil
with values lying in a given range, including boundary. Important! Both parameters are required!
last()
- return last element of array or undefined if there is no elements in array.
lastIndex()
- return number is equal to length of object minus one.
lessOrEqual(than)
- returns instance of ArrayUtil
with values less or equal than the passed.
lessThan(than)
- returns an instance of ArrayUtil
with values less than the passed.
max()
- return the greatest element in array.
min()
- return the least element in array.
minMax()
- return instance of ArrayUtil
with two values, comprising the least and the greatest elements.
orderStatistic(index)
- returns a value in a sorted array would stand at the given position.
For example:
ArrayUtil( [4, 3, 2, 1] ).orderStatistic(0)
return 1, because after sorting object will be equal to [1, 2, 3 ,4]
.
remove(a[, b[, c[...]]])
- - take one or more arguments. Removes all occurrences of the arguments in the object. If obtained one argument and it is an array, it will look for items that are in it. Return current object.
shuffle()
- shuffles elements in object and return it.
sortByDesc()
- sort object by descending and return it.
isIndexCorrect(index)
- return true
if index greater than or equal 0 and less than length of object minus one.
swap(firstIndex, secondIndex)
- swap two elements with given indexes and return object.
toArray()
- return array, comprising values of current object.
unique()
- reserves in the object only unique values and return it.