Module: bff/list

Maintains a list of items. The idea is to stay as true as possible to the native Array interface, but to augment its usefulness by triggering events whenever the list is updated, as well as adding convenience functions for mutating the List in place.

The exported List constructor can be used as-is, in which case a generic List will be created. This is a quite common use case. Alternatively, a List "subclass" constructor can be created by calling List.withProperties(...). This second approach provides the possibility to add custom calculated properties to the list. See module:bff/list#withProperties for more details.


new (require("bff/list"))( [items])

Creates a new List, with an optional list or array of initial items.

Parameters:
Name Type Argument Description
items Array | List <optional>

Items that will be added to the List on creation.

Mixes In:
Source:

Methods


<static> withProperties(schema)

Creates a new List constructor function, that will create List instances with the property schema
provided to this function

Parameters:
Name Type Description
schema Object

An object describing the properties that will be part of all new instances created by the returned constructor function. The property descriptions should be on the same format at the schema specified for module:bff/record.withProperties, with the only exception that setters are not allowed.

Source:
Returns:

New constructor function based on the provided schema.

Type
function

addEventListener(eventName)

Augmented version of bff/event-emitter#addEventListener that provides the option to listen to events emitted from any item in the List. To add a listener to an item event, prepend the event name with 'item:'. For instance, to listen for the 'change' event on all the list items (even ones added after the listener was created), add a listener for the 'item:change' event.

Parameters:
Name Type Description
eventName string

Identifier string for the event.

Source:

clear()

Mutates the List by removing all items.

Source:
Fires:
Returns:

The List, now empty.

Type
List

concat(value)

Returns a new List comprised of the List on which it is called joined with the List(s), Array(s) and/or value(s) provided as arguments. Mirrors the behavior of Array.concat.

Parameters:
Name Type Argument Description
value any <repeatable>

A List, Array or value that will be concatenated with the original List. Lists and Arrays will be deconstructed and each item added to the new List.

Source:
Returns:

The new List.

Type
List

every(predicate [, thisArg])

Mirrors Array.every behavior.

Parameters:
Name Type Argument Description
predicate module:bff/list~predicateCallback

Executed once per List item.

thisArg any <optional>

Value to use as "this" when executing callback.

Source:
Returns:

true if all items passes the predicate test, false otherwise.

Type
boolean

filter(predicate)

Creates a new List with all items that pass the test implemented by the predicate function test. The original list is unchanged. Mirrors the behavior of Array.filter.

Parameters:
Name Type Description
predicate module:bff/list~predicateCallback

Executed once per List item.

Source:
Returns:

The new List.

Type
List

filterMut(predicate [, thisArg])

Mutates the List by removing all items that does not pass the predicate function test.

Parameters:
Name Type Argument Description
predicate module:bff/list~predicateCallback

Called once per List item to determine whether the item should be removed.

thisArg any <optional>

Value to use as "this" when executing predicate callback.

Source:
Fires:
Returns:

The filtered List.

Type
List

find(predicate)

Returns the first item in the List that passes the predicate function test, or undefined if no item passes.

Parameters:
Name Type Description
predicate module:bff/list~predicateCallback

Called once per List item to in order to find a matching item.

Source:
Returns:

The matching item, if any.

Type
any | undefined

findIndex(predicate)

Returns the index of the first item in the List that passes the predicate function test, or -1 if no item passes.

Parameters:
Name Type Description
predicate module:bff/list~predicateCallback

Called once per List item to in order to find a matching item.

Source:
Returns:

The position of the matching item, or -1 if none matches.

Type
number

forEach(callback [, thisArg])

Executes the given function once per List item. Mirrors Array.forEach behavior.

Parameters:
Name Type Argument Description
callback module:bff/list~forEachCallback

The function that will be called once per List item.

thisArg any <optional>

Value to use as "this" when executing callback.

Source:

includes(item [, fromIndex])

Returns whether the provided item is part of the List.

Parameters:
Name Type Argument Description
item any
fromIndex number <optional>

The index to start the search at. If the index is greater than or equal to the List's length, -1 is returned, which means the List will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the List.

Source:
Returns:

True if the item is part of the List, false otherwise.

Type
boolean

indexOf(searchItem [, fromIndex])

Mirrors Array.indexOf behavior.

Parameters:
Name Type Argument Description
searchItem any

The item to locate within the List.

fromIndex number <optional>

The index to start the search at. If the index is greater than or equal to the List's length, -1 is returned, which means the List will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the List.

Source:
Returns:

The first index at which a given item can be found in the List, or -1 if it is not present.

Type
number

join( [separator])

Parameters:
Name Type Argument Description
separator string <optional>

Specifies a string to separate each item of the List. If omitted, the List items are separated with a comma. Mirrors Array.join behavior.

Source:
Returns:

The stringified List items, joined by the 'separator' string argument.

Type
string

lastIndexOf(searchItem [, fromIndex])

Parameters:
Name Type Argument Description
searchItem any

The item to locate within the List.

fromIndex number <optional>

The index at which to start searching backwards. Defaults to the List's length minus one, i.e. the whole List will be searched. If the index is greater than or equal to the length of the List, the whole List will be searched. If negative, it is taken as the offset from the end of the List. Mirrors Array.lastIndexOf behavior.

Source:
Returns:

The last index at which a given item can be found in the List, or -1 if it is not present.

Type
number

map(callback [, thisArg])

Creates a new List with the results of calling a provided callback function on every element in the List. Mirrors the behavior of Array.map.

Parameters:
Name Type Argument Description
callback module:bff/module~mapCallback

Executed once per List item.

thisArg any <optional>

Value to use as "this" when executing callback.

Source:
Returns:

The new List.

Type
List

mapMut(callback [, thisArg])

Replaces all items in the List with new items, generated by the callback function.

Parameters:
Name Type Argument Description
callback module:bff/module~mapCallback

Executed once per List item to produce new items.

thisArg any <optional>

Value to use as "this" when executing callback.

Source:
Fires:
Returns:

The List, with all items updated.

Type
List

pop()

Remove and return one item from the end of the List. Mirrors Array.pop behavior.

Source:
Fires:
Returns:

Removed item

Type
any

propertiesToJSON()

Returns a newly created Object containing the List's deep copied properties.

Source:
Returns:
Type
Object

push(item)

Add one or more items to the end of the List. Mirrors Array.push behavior.

Parameters:
Name Type Argument Description
item any <repeatable>

Each item argument will be pushed onto the List.

Source:
Fires:
Returns:

Updated List length

Type
number

pushAll(items)

Mutates the List by adding the items comprising the provided List or Array to the end of the List.

Parameters:
Name Type Description
items module:bff/list | Array

The items that will be appended to the List.

Source:
Fires:
Returns:

The List, with new items appended.

Type
List

reduce(callback, initialValue)

Applies a function against an accumulator and each value of the List (from left-to-right) to reduce it to a single value. Mirrors Array.reduce behavior.

Parameters:
Name Type Description
callback module:bff/list~reduceCallback

The function that will be called once per List item.

initialValue any

Value to use as the first argument to the first call of the callback.

Source:
Returns:

Aggregated value

Type
any

reduceRight(callback, initialValue)

Applies a function against an accumulator and each value of the List (from right-to-left) to reduce it to a single value. Mirrors Array.reduceRight behavior.

Parameters:
Name Type Description
callback module:bff/list~reduceCallback

The function that will be called once per List item.

initialValue any

Value to use as the first argument to the first call of the callback.

Source:
Returns:

Aggregated value

Type
any

remove(item)

Mutates the List by removing all occurances of the provided item.

Parameters:
Name Type Description
item any

The item to remove from the list.

Source:
Fires:
Returns:

The List, without any occurances of 'item'.

Type
List

removeEventListener(eventName)

Augmented version of bff/event-emitter#removeEventListener that provides functionality for removing "item" event listeners, e.g. for stop listening to events like "items:change".

Parameters:
Name Type Description
eventName string

Identifier string for the event.

Source:

reverse()

Reverses the List in place. Mirros the behavior of Array.reverse. No events are emitted by this operation.

Source:

shift()

Remove and return one item from the beginning of the List. Mirrors Array.shift behavior.

Source:
Fires:
Returns:

Removed item

Type
any

slice( [begin] [, end])

Creates a new List from the range of current items specified by the begin and end arguments. Mirrors the behavior of Array.slice.

Parameters:
Name Type Argument Description
begin number <optional>

Index that specifies the beginning of the range. Inclusive. A negative index will
be relative to the end of the List instead of the beginning.

end number <optional>

Index that specifies the end of the range. Exclusive. A negative index will
be relative to the end of the List instead of the beginning.

Source:
Returns:

The new List.

Type
List

sliceMut( [begin] [, end])

Mutates the List by removing all elements outside of range specified by the begin and end arguments.

Parameters:
Name Type Argument Description
begin number <optional>

Index that specifies the beginning of the range. Inclusive. A negative index will be relative to the end of the List instead of the beginning.

end number <optional>

Index that specifies the end of the range. Exclusive. A negative index will be relative to the end of the List instead of the beginning.

Source:
Fires:
Returns:

The List, sans the items outside of the specified range.

Type
List

some(predicate [, thisArg])

Mirrors Array.some behavior.

Parameters:
Name Type Argument Description
predicate module:bff/list~predicateCallback

Executed once per List item.

thisArg any <optional>

Value to use as "this" when executing callback.

Source:
Returns:

true if at least one item passes the predicate test, false otherwise.

Type
boolean

sort( [comparator])

Sorts the items of the List in place. The sort is not necessarily stable. The default sort order is according to string Unicode code points, unless a custom comparator finction is provided. Mirrors the behavior of Array.sort. No events are emitted by this operation.

Parameters:
Name Type Argument Description
comparator module:bff/list~compareFunction <optional>

A function that specifies the ordering of two arbitrary List items. Called multiple times in order to produce a total ordering of the items.

Source:
Returns:

The sorted list.

Type
List

splice(start, nItemsToRemove [, itemToAdd])

Changes the content of the List by removing existing items and/or adding new items. Mirrors Array.splice behavior.

Parameters:
Name Type Argument Description
start number

Index at which to start changing the array. If greater than the length of the array, actual starting index will be set to the length of the array. If negative, will begin that many items from the end.

nItemsToRemove number

An integer indicating the number of old array items to remove. If nItemsToRemove is greater than the number of items left in the array starting at start, then all of the items through the end of the array will be deleted.

itemToAdd any <optional>
<repeatable>

Item that will be added to the array, starting at the index specified in the first argument.

Source:
Fires:
Returns:

Array of removed items

Type
Array.<any>

toArray()

Returns an newly created Array, containing all the items of the List. List properties are not copied to the Array object. The items are copied shallowly.

Source:
Returns:
Type
Array

toJSON()

Returns a newly created Array representation of the List, containing deep copies of all the List's items, but not its properties.

Source:
Returns:
Type
Array

toString()

Source:
Returns:

A human readable string representation of the List.

Type
string

unshift(item)

Add one or more items to the beginning of the List. Mirrors Array.unshift behavior.

Parameters:
Name Type Argument Description
item any <repeatable>

Each item argument will be pushed onto the List.

Source:
Fires:
Returns:

Updated List length

Type
Number

Type Definitions


compareFunction(itemA, itemB)

Compares itemA and itemB arguments according to some sorting criterion. Should return -1 if itemA comes before itemB, 0 if itemA is equal to itemB, 1 if itemB comes before itemA.

Parameters:
Name Type Description
itemA any

A list item.

itemB any

Another list item.

Source:
Returns:

.

Type
number

forEachCallback(item, index, list)

Parameters:
Name Type Description
item any

Current List item.

index number

Item position in List.

list module:bff/list

List being iterated over.

Source:

mapCallback(item, index, list)

Parameters:
Name Type Description
item any

Current List item.

index number

Item position in List.

list module:bff/list

List being iterated over.

Source:
Returns:

Transformed object.

Type
any

predicateCallback(item, index, list)

Parameters:
Name Type Description
item any

Current List item.

index number

Item position in List.

list module:bff/list

List being iterated over.

Source:
Returns:

true if the item passes the test, false otherwise

Type
boolean

reduceCallback(previousItem, item, index, list)

Parameters:
Name Type Description
previousItem any

The value previously returned in the last invocation of the callback, or initialValue, if supplied. Usually an aggregate of previous items.

item any

Current List item being processed.

index number

Item position in List.

list module:bff/list

List being iterated over.

Source:
Returns:

The aggregated value.

Type
any

Events


change:length

Parameters:
Name Type Description
newLength number

The current length of the List.

oldLength number

The previous length of the List.

list module:bff/list

The List whose length has changed.

Source:

item:added

Parameters:
Name Type Description
item any

The item that was added to the List.

index number

The position withing the List where the item was added.

list module:bff/list

The List to which the item was added.

Source:

item:removed

Parameters:
Name Type Description
item any

The item that was removed from the List.

index number

The position withing the List where the item was removed.

list module:bff/list

The List from which the item was removed.

Source:

item:replaced

Parameters:
Name Type Description
newItem any

The item that was added to the List.

oldItem any

The item that was removed from the List.

index number

The position withing the List where the item was replaced.

list module:bff/list

The List in which the item was replaced.

Source: