@letumfalx/array-collection
v0.1.0
Published
A wrapper for your native array.
Readme
Array Collection
A utility wrapper for the array. Inspired by Laravel's Collection and its JS port collect.js.
This package has its own implementation of each method.
Installation
For NPM:
npm install @letumfalx/array-collectionFor Yarn:
yarn add @letumfalx/array-collectionNotes
- This will just wrap the array to the collection instance maintaining its reference, meaning mutating the array outside the collection instance will also mutate the internal array reference.
- The methods with overloads uses the number and type of arguments passed to determine what kind of behavior it will do. For example,
containshas 4 overloads:- passing
1 argumentthat is not a function will check if that value is in the collection - passing
1 argumentthat is a function will use that function to check if there are entries that passes that function - passing
2 argumentswill use the first argument as the key of the object to search and the second argument as the comparing value - passing
3 argumentswill use the first argument as the key of the object to search, the second argument as the operator and the third as the value to compare
- passing
- The
defaultValues are always defaults toNaNif not given. - This will just copy the reference of the entry from the internal array if it is deriving a new collection.
API
Here are the list of the methods for the collection. Check the Definition File for more information.
constructor (data: T[] = [])clone (): Collection<T>contains (value: T): Booleancontains (search: (value: T, index: Number) => Boolean): Booleancontains<V> (key: String, value: V): Booleancontains<V> (key: String, operator: Operator, value: V): Booleancount (): Numbereach (iterator: (value: T, index: Number) => void): thisempty (): thisevery (key: String): Booleanevery (testFunc: (value: T, index: Number) => Boolean): Booleanevery<V> (key: String, value: V): Booleanevery<V> (key: String, testFunction: (value: V, index: Number) => Boolean): Booleanevery<V> (key: String, operator: Operator, value: V): Booleanfilter (): Collection<T>filter (testFunction: (value: T, index: Number) => Boolean): Collection<T>filter (key: String): Collection<T>filter<V> (key: String, value: V): Collection<T>filter<V> (key: String, testFunction: (value: V, index: Number) => Boolean): Collection<T>filter<V> (key: String, operator: Operator, value: V): Collection<T>first (): Tfirst (searchFunc: (entry: T) => Boolean): Tfirst<V> (defaultValue: V): Tfirst<V> (searchFunc: (entry: T) => Boolean, defaultValue: V): Tforget (value: T): thisforget<V> (key: String, value: V): thisforget<V> (key: String, testFunction: (value: V, index: Number) => Boolean): thisforget<V> (key: String, operator: Operator, value: V): thisforgetIndex (index: Number): thisforgetLast (value: T): thisforgetLast<V> (key: String, value: V): thisforgetLast<V> (key: String, testFunction: (value: V, index: Number) => Boolean): thisforgetLast<V> (key: String, operator: Operator, value: V): thisget<V> (index: Number, defaultValue: V): TgetOriginal (): T[]has (index: Number): Booleanindices(): Collection<Number>isEmpty (): BooleanisNotEmpty (): Booleanlast (): Tlast (searchFunc: (entry: T) => Boolean): Tlast<V> (defaultValue: V): Tlast<V> (searchFunc: (entry: T) => Boolean, defaultValue: V): TlastIndex (): Numbermap<V> (transformer: (value: T, index: Number) => V): Collection<V>pop<V> (defaultValue: V): Tprepend (value: T): thisprepend (values: T[]): thispull<V> (index: Number, defaultValue: V): Tpush (value: T): thispush (values: T[]): thisrandom<V> (defaultValue: V): TrandomPull<V> (defaultValue: V): Treduce<I, A, R> (reducer: (accumulator: A, current: T, index: Number) => R, initialValue: I): Rreject (): Collection<T>reject (testFunction: (value: T, index: Number) => Boolean): Collection<T>reject (key: String): Collection<T>reject<V> (key: String, value: V): Collection<T>reject<V> (key: String, testFunction: (value: V, index: Number) => Boolean): Collection<T>reject<V> (key: String, operator: Operator, value: V): Collection<T>reverse (): Collection<T>search (value: T): Numbersearch<V> (key: String, value: V): Numbersearch<V> (key: String, testFunction: (value: V, index: Number) => Boolean): Numbersearch<V> (key: String, operator: Operator, value: V): NumbersearchLast (value: T): NumbersearchLast<V> (key: String, value: V): NumbersearchLast<V> (key: String, testFunction: (value: V, index: Number) => Boolean): NumbersearchLast<V> (key: String, operator: Operator, value: V): Numbershift<V> (defaultValue: V): Tslice (startIndex: Number, endIndex: Number): Collection<T>sort (): Collection<T>sort (sortFunc: (a: T, b: T) => Number): Collection<T>sort (key: String): Collection<T>sort<V> (key: String, sortFunc: (a: V, b: V) => Number): Collection<T>sortDesc (): Collection<T>sortDesc (sortFunc: (a: T, b: T) => Number): Collection<T>sortDesc (key: String): Collection<T>sortDesc<V> (key: String, sortFunc: (a: V, b: V) => Number): Collection<T>splice (startIndex: Number, endIndex: Number, value: T): Collection<T>splice (startIndex: Number, endIndex: Number, value: T[]): Collection<T>splice (startIndex: Number, endIndex: Number, value: Collection<T>): Collection<T>toJson (): Stringwhen (condition: Boolean, ifHandler: (collection: this) => void, elseHandler: (collection: this) => void): this
License
MIT
