Collection

A class which encapsulates a keyed, filterable, sortable collection of objects. Entries may not be atomic data types such as string or number.

The entries are keyed by their id which is determined by interrogating the idProperty.

To filter a Collection, add a CollectionFilter using the addFilter method. A Filter config object may be specified here which will be promoted to a CollectionFilter instance.

To sort a Collection, add a CollectionSorter using the addSorter method. A Sorter config object may be specified here which will be promoted to a CollectionSorter instance.

Configs

4
autoFilter: Boolean= true

Automatically apply filters on item add.

extraKeys: String[] | Object[]

Specify the names or index configs of properties which are to be indexed for fast lookup.

Index configs use the format { property : string, unique : boolean }. Unique indices stores one index per entry, non-unique stores a Set. If left out, unique defaults to true

ParameterTypeDescription
propertyString

Property to index by

uniqueBoolean

true for unique keys (~primary keys), false for non-unique keys (~foreign keys)

Specify the name of the property of added objects which provides the lookup key

A Sorter, or Sorter config object, or an array of these, to use to sort this Collection.

Properties

12
allValues: Object[]readonly

The set of all values of this Collection regardless of filters applied.

count: Numberreadonly

The number of items in this collection. Note that this honours filtering. See totalCount;

filterFunction: functionreadonly

A filter function which encapsulates the Filters for this Collection.

ParameterTypeDescription
value*

Value to compare

Returns: Boolean -

Returns true for value valid for including

The Collection of Filters for this Collection.

generation: Numberreadonly

A counter which is incremented whenever the Collection is mutated in a meaningful way.

If a splice call results in no net replacement, removal or addition, then the generation will not be incremented.

The property name used to extract item ids from added objects.

isFiltered: Booleanreadonly

A flag which is true if this Collection has active filters.

isSorted: Booleanreadonly

A flag which is true if this Collection has active sorters.

The Collection of Sorters for this Collection.

sortFunction: functionreadonly

A sorter function which encapsulates the Sorters for this Collection.

ParameterTypeDescription
first*

First value to compare

second*

Second value to compare

Returns: Number -

Returns 1 if first value is a greater unit than second value, -1 if the opposite is true or 0 if they're equal

totalCount: Numberreadonly

The number of items in this collection regardless of filtering.

values: Object[]

The set of values of this Collection. If this Collection isFiltered, this yields the filtered data set.

Setting this property replaces the data set.

Functions

20

Adds items to this Collection. Multiple new items may be passed.

By default, new items are appended to the existing values.

Any sorters sorters present are re-run.

Any filters filters present are re-run.

Note that if application functionality requires add and remove, the splice operation is preferred as it performs both operations in an atomic manner

ParameterTypeDescription
itemsObject

The item(s) to add.

Adds a Filter to the Collection of Filters which are operating on this Collection.

A Filter may be an specified as an instantiated CollectionFilter, or a config object for a CollectionFilter of the form

{
    property : 'age',
    operator : '>=',
    value    : 21
}

Note that by default, a Filter replaces a Filter with the same property to make it easy to change existing Filters. A Filter's id is its property by default. You can avoid this and add multiple Filters for one property by configuring Filters with ids.

A Filter may also be specified as a function which filters candidate objects eg:

candidate => candidate.customerDetails.age >= 21
ParameterTypeDescription
filterCollectionFilterConfig | CollectionFilter

A Filter or Filter configuration object to add to the Collection of Filters operating on this Collection.

Returns: CollectionFilter -

The resulting Filter to make it easy to remove Filters.

Adds a Sorter to the Collection of Sorters which are operating on this Collection.

A Sorter may be specified as an instantiated CollectionSorter, or a config object for a CollectionSorter of the form

{
    property  : 'age',
    direction : 'desc'
}

Note that by default, a Sorter replaces a Sorter with the same property to make it easy to change existing Sorters. A Sorter's id is its property by default. You can avoid this and add multiple Sorters for one property by configuring Sorters with ids.

A Sorter may also be specified as a function which compares two objects eg:

(lhs, rhs) => lhs.customerDetails.age - rhs.customerDetails.age
ParameterTypeDescription
sorterCollectionSorterConfig

A Sorter configuration object to add to the Collection of Sorters operating on this Collection.

Returns: CollectionSorter -

The resulting Sorter to make it easy to remove Sorters.

Change the id of an existing member by mutating its idProperty.

ParameterTypeDescription
itemString | Number | Object

The item or id of the item to change.

newIdString | Number

The id to set in the existing member.

Clears this collection.

Compares the content of this Collection with the content of the passed Collection or with the passed array. Order insensitive. This returns true if the two objects passed contain the same set of items.

ParameterTypeDescription
otherCollection | Array

The Collection or array to compare with.

mapfunction

Optionally a function to convert the items into a comparable object to compare. For example item => item.id could be used to compare the ids of the constituent items.

Returns: Boolean -

true if the two objects passed have the same content.

Returns the first item in this Collection which elicits a truthy return value from the passed function.

ParameterTypeDescription
fnfunction

A function, which, when passed an item, returns true to select it as the item to return.

ignoreFiltersBoolean

Pass true to include filtered out items.

Returns: Object -

The matched item, or undefined.

Return the index of the item with the specified key having the specified value.

By default, filtering is taken into account and this returns the index in the filtered dataset if present. To bypass this, pass the third parameter as true.

Only useful for indices configured with unique: true.

ParameterTypeDescription
propertyNameString

The name of the property to test.

value*

The value to test for.

ignoreFiltersBoolean

Pass true to return the index in the original data set if the item is filtered out.

Returns: Number -

The index of the item or -1 if not found for unique indices

Return the item with the specified key having the specified value.

By default, filtering is taken into account. To bypass this, pass the third parameter as true.

For indices configured with unique: false, a Set of items will be returned.

ParameterTypeDescription
propertyNameString

The name of the property to test.

value*

The value to test for.

ignoreFiltersBoolean

Pass true to return the index in the original data set if the item is filtered out.

Returns: Object | Set -

The found item or Set of items or null

Executes the passed function for each item in this Collection, passing in the item, ths index, and the full item array.

ParameterTypeDescription
fnfunction

The function to execute.

ignoreFiltersBoolean

Pass true to include all items, bypassing filters.

Returns the item with the passed id. By default, filtered are honoured, and if the item with the requested id is filtered out, nothing will be returned.

To return the item even if it has been filtered out, pass the second parameter as true.

ParameterTypeDescription
id*

The id to find.

ignoreFiltersBoolean

Pass true to return an item even if it is filtered out.

Returns: Object -

The found item, or undefined.

Returns the item with passed property name equal to the passed value. By default, filtered are honoured, and if the item with the requested id is filtered out, nothing will be returned.

To return the item even if it has been filtered out, pass the third parameter as true.

ParameterTypeDescription
propertyNameString

The property to test.

value*

The value to find.

ignoreFiltersBoolean

Pass true to return an item even if it is filtered out.

Returns: Object -

The found item, or undefined.

Returns true if this Collection includes an item with the same id as the passed item.

By default, filtering is honoured, so if the item in question has been added, but is currently filtered out of visibility, false will be returned.

To query inclusion in the master, unfiltered dataset, pass the second parameter as true;

ParameterTypeDescription
itemObject | String | Number

The item to find, or an id to find.

ignoreFiltersBoolean

Pass true to find the index in the master, unfiltered data set.

Returns: Boolean -

True if the passed item is found.

Returns the index of the item with the same id as the passed item.

By default, filtering is honoured, so if the item in question has been added, but is currently filtered out of visibility, -1 will be returned.

To find the index in the master, unfiltered dataset, pass the second parameter as true;

ParameterTypeDescription
itemObject | String | Number

The item to find, or an id to find.

ignoreFiltersBoolean

Pass true to find the index in the master, unfiltered data set.

Returns: Number -

The index of the item, or -1 if not found.

Extracts ths content of this Collection into an array based upon the passed value extraction function.

ParameterTypeDescription
fnfunction

A function, which, when passed an item, returns a value to place into the resulting array.

ignoreFiltersBoolean

Pass true to process an item even if it is filtered out.

Returns: Object[] -

An array of values extracted from this Collection.

This method ensures that every item in this Collection is replaced by the matched by id item in the other Collection.

By default, any items in this Collection which are not in the other Collection are removed.

If the second parameter is passed as false, then items which are not in the other Collection are not removed.

This can be used for example when updating a selected record Collection when a new Store or new store dataset arrives. The selected Collection must reference the latest versions of the selected record ids

ParameterTypeDescription
otherCollection

The Collection whose items to match.

Moves an individual item, or a block of items to another location.

ParameterTypeDescription
itemsObject | Object[]

The item/items to move.

beforeItemObject

the item to insert the first item before. If omitted, the item is moved to the end of the Collection.

Returns: Number -

The new index of the item.

Returns an accumulated value based on the passed function similar to Array.reduce.

ParameterTypeDescription
fnfunction

A function, which, when passed an accumulator value and an item, returns the new accumulator value.

ignoreFiltersBoolean

Pass true to process an item even if it is filtered out.

accumulator*

The initial value of the accumulator.

Returns: * -

the final accumulator value.

Removes items from this Collection. Multiple items may be passed.

Any sorters sorters present are re-run.

Any filters filters present are re-run.

Note that if application functionality requires add and remove, the splice operation is preferred as it performs both operations in an atomic manner

ParameterTypeDescription
itemsObject

The item(s) to remove.

The core data set mutation method. Removes and adds at the same time. Analogous to the Array splice method.

Note that if items that are specified for removal are also in the toAdd array, then those items are not removed then appended. They remain in the same position relative to all remaining items.

ParameterTypeDescription
indexNumber

Index at which to remove a block of items. Only valid if the second, toRemove argument is a number.

toRemoveObject[] | Number

Either the number of items to remove starting at the passed index, or an array of items to remove (If an array is passed, the index is ignored).

toAddObject[] | Object

An item, or an array of items to add.

Events

2

Fired when items are added, replace or removed

// Adding a listener using the "on" method
collection.on('change', ({ action, source, removed, added, replaced, oldCount }) => {

});
ParameterTypeDescription
actionsplice | clear | replaceValues | move | sort | filter

The underlying operation which caused data change. May be 'splice' (meaning an atomic add/remove operation, 'sort' or 'filter'), 'clear', 'replaceValues', 'move', 'sort' or 'filter'.

sourceCollection

This Collection.

removedObject[]

An array of removed items. If the action is 'filter', the removed property represents the records which were filtered out by the action.

addedObject[]

An array of added items. If the action is 'filter', the added property represents the records which were filtered in by the action.

replacedObject[]

An array of replacements, each entry of which contains [oldValue, newValue].

oldCountNumber

The number of items in the full, unfiltered collection prior to the splice operation.

Fired when a splice operation is requested but the operation is a no-op and has caused no change to this Collection's dataset. The splice method's parameters are passed for reference.

// Adding a listener using the "on" method
collection.on('noChange', ({ index, toRemove, toAdd }) => {

});
ParameterTypeDescription
indexNumber

Index at which to remove a block of items.

toRemoveObject[] | Number

Either the number of items to remove starting at the passed index, or an array of items to remove (If an array is passed, the index is ignored).

toAddObject[] | Object

An item, or an array of items to add.

Event handlers

2

Called when items are added, replace or removed

new Collection({
    onChange({ action, source, removed, added, replaced, oldCount }) {

    }
});
ParameterTypeDescription
actionsplice | clear | replaceValues | move | sort | filter

The underlying operation which caused data change. May be 'splice' (meaning an atomic add/remove operation, 'sort' or 'filter'), 'clear', 'replaceValues', 'move', 'sort' or 'filter'.

sourceCollection

This Collection.

removedObject[]

An array of removed items. If the action is 'filter', the removed property represents the records which were filtered out by the action.

addedObject[]

An array of added items. If the action is 'filter', the added property represents the records which were filtered in by the action.

replacedObject[]

An array of replacements, each entry of which contains [oldValue, newValue].

oldCountNumber

The number of items in the full, unfiltered collection prior to the splice operation.

Called when a splice operation is requested but the operation is a no-op and has caused no change to this Collection's dataset. The splice method's parameters are passed for reference.

new Collection({
    onNoChange({ index, toRemove, toAdd }) {

    }
});
ParameterTypeDescription
indexNumber

Index at which to remove a block of items.

toRemoveObject[] | Number

Either the number of items to remove starting at the passed index, or an array of items to remove (If an array is passed, the index is ignored).

toAddObject[] | Object

An item, or an array of items to add.