Array helper.
Installation & Usage
npm i @lbzg/arrays
Attach to prototype or import Arrays
or arr
.
fns: add, addUnique, remove, get, getAll, has, count, indices, list
import '@lbzg/arrays'
const x = []
x.arr().add({id: 1})
x.arr().add([{id: 2}, 3, 3])
x.arr().addUnique(1) # does not get added
x.arr().remove(1)
x.arr().getAll() # [{id: 2}, 3, 3]
selector
can be a value, an array of values or a function
# get(selector?, key? = 'id')
x.arr().get() # {id: 2}
x.arr().get(1) # undefined
x.arr().get(2) # {id: 2}
x.arr().get(3) # 3
x.arr().get([1, 2, 3]) # {id:2}
x.arr().get(x => x === 3) # 3
x.arr().getAll() # [{id: 2}, 3, 3]
x.arr().getAll(1) # []
x.arr().getAll(2) # [{id: 2}]
x.arr().getAll(3) # [3, 3]
x.arr().getAll([1, 2, 3]) # [{id: 2}, 3, 3]
x.arr().getAll(x => x === 3) # [3, 3]
x.arr().has(2) # true # has(selector?, key?)
x.arr().count() # 4 # count(selector?, key?)
x.arr().indices(1) # [0, 1] # indices(selector, key?)
x.arr().list() # [2] # list(key?)
Prototype
arr(input?)
Takes optional input and returns Arrays instance.
Array.prototype.arr() takes no input.
Arrays
constructor(input?)
add(
input
): array ~ mutates array
addUnique(input
, key?): array ~ mutates array
remove(selector
, amount?, key?): array ~ mutates array
get(
selector?
, key?): value | undefined
getAll(selector?
, key?): array
has(selector?
, key?): boolean ~ every for value(s); some for function
count(selector?
, key?): number
indices(selector
, key?): array ~ of matched indexes
list(key?): array ~ object key values
input
~ value or value[]
selector
~ value or value[] or function
key ~ string | number | symbol (object identifier)