Collection of elements of the same type, has everything that we are so lacking in JS Array
s and a little more.
MobX
. See this blockExports two main classes:
Collection
- Just a wrapper around the Array, adds methods that make life easier (docs).IdCollection
- Identified collection. This collection is inherited from the previous one. Designed to work with data that has an id: number | string
(docs).Collection
classimport { Collection } from 'collossus'
const names = new Collection( [ 'Max' ] ) // [ ' Max' ]
names.push( [ 'Yan', 'Li' ] ) // [ ' Max', 'Yan', 'Li' ]
names.last() // -> 'Li'
names.swap( 1, 2 ) // [ ' Max', 'Li', 'Yan' ]
names.pushUniq( 'Max' ) // [ ' Max', 'Li', 'Yan' ]
names.rfindIndexBy( n => n === 'Yan' ) // -> 2
names.clear() // []
IdCollection
classimport { IdCollection } from 'collossus'
type User = { id: number, name: string }
const users = new IdCollection<User>( [ { id: 33, name: 'Max' } ] ) // [{ id: 33, name: 'Max' }]
users.push( [ { id: 55, name: 'Yan' } ] ) // [{ id: 33, name: 'Max' },{ id: 55, name: 'Yan' }]
users.findById( 33 ) // -> { id: 33, name: 'Max' }
users.pushUniqById( { id: 55, name: 'Li' } ) // [{ id: 33, name: 'Max' },{ id: 55, name: 'Yan' }]
users.removeById( 55 ) // [ { id: 33, name: 'Max' } ]
users.first() // { id: 33, name: 'Max' }
users.clear() // []
Collection
methods( chunkSize: number ) => Array<Array<T>>
() => void
( predicate: CallbackFuncType<T, boolean> ) => Array<T>
( predicate: CallbackFuncType<T, boolean> ) => Collection<T>
( predicate: CallbackFuncType<T, boolean>, startIndex?: number ) => null | T
( predicate: CallbackFuncType<T, boolean>, startIndex?: number ) => number
() => null | T
( closure: CallbackFuncType<T, void> ) => void
( index: number ) => null | T
() => Array<T>
( it: T ) => boolean
() => boolean
() => null | T
() => number
<R>( closure: CallbackFuncType<T, R> ) => Collection<R>
<R>( closure: CallbackFuncType<T, R> ) => Array<R>
() => null | T
( it: T | Array<T> ) => void
( it: T | Array<T> ) => void
( it: T | Array<T>, compare: CompareFuncType<T> ) => void
<A>( callback: ( acc: A, it: T ) => A, initValue: A ) => A
( index: number ) => null | T
( predicate: CallbackFuncType<T, boolean> ) => null | T
( num: number ) => Collection<T>
( data: null | Array<T> ) => void
( predicate: CallbackFuncType<T, boolean>, startIndex?: number ) => Array<T>
( predicate: CallbackFuncType<T, boolean>, startIndex?: number ) => null | T
( predicate: CallbackFuncType<T, boolean>, startIndex?: number ) => number
( index: number, it: T ) => boolean
() => null | T
() => void
( indexA: number, indexB: number ) => boolean
() => Array<T>
() => null | string
( len: number ) => boolean
IdCollection
methods( id: IdOf<T> ) => null | T
( id: IdOf<T> ) => number
( id: IdOf<T> ) => boolean
( it: Array<T> | T ) => void
( id: IdOf<T> ) => null | T
Mobx
Library exports two classes that prepared to be observable - ObservableCollection
and ObservableIdCollection
.
But since it does not have Mobx
in its dependencies you should patch it with your version of Mobx
.
You need to do it just once in your project before creating instances of observable collection.
import * as mobx from 'mobx'
import { patchObservableCollections, ObservableCollection } from 'collossus'
// call this somewhere on init
patchObservableCollections( mobx )
// now ObservableCollection is **really** observable
const oc = new ObservableCollection( [ 1, 2, 3 ] )
// this is `computed`
oc.length
// this is `action`
oc.push( 4 )
// and this too
oc.remove( 0 )
Everything else is identical to Collection
and IdCollection
This module is MIT licensed.
Generated using TypeDoc