INormalizedCollection
Interface | Source Code
A collection structure that combines the benefits of an object (O(1) lookup, stable identifiers) and an array (preservation of order and sorting). Also serializes to JSON nicely provided the data type used for the elements is JSON-serializable.
Properties
Section titled “Properties”isEmpty: boolean
Section titled “isEmpty: boolean”Whether the collection is currently empty.
length: number
Section titled “length: number”The number of items in the collection.
Methods
Section titled “Methods”add(item: T, overwriteExisting?: boolean): void
Section titled “add(item: T, overwriteExisting?: boolean): void”Adds the provided item. If the ID of the item matches an item
already in the collection, you can customize behaviour of the method
with the overwriteExisting param.
clear(): void
Section titled “clear(): void”Removes all items from the collection.
filter(predicate: Function): INormalizedCollection
Section titled “filter(predicate: Function): INormalizedCollection”Returns
Section titled “Returns”A new collection that contains only the items from this collection that passed
the predicate.
find(predicate: Function): undefined | T
Section titled “find(predicate: Function): undefined | T”Returns
Section titled “Returns”The first occurrence of an item returning true when given to the
provided predicate, or undefined if no item passed the predicate.
forEach(iteratee: Function): void
Section titled “forEach(iteratee: Function): void”fromJSON(json: string): undefined | INormalizedCollection
Section titled “fromJSON(json: string): undefined | INormalizedCollection”Returns
Section titled “Returns”A new collection instance that represents the serialized JSON, or undefined
if parsing wasn’t possible.
get(id: string): undefined | T
Section titled “get(id: string): undefined | T”Returns
Section titled “Returns”The collection item specified by id, or undefined if there was
no item with the provided ID.
includesId(id: string): boolean
Section titled “includesId(id: string): boolean”Returns
Section titled “Returns”Whether the collection includes the item with the specified ID.
includesItem(item: T, comparator?: Function): boolean
Section titled “includesItem(item: T, comparator?: Function): boolean”Determines whether an item exists in the collection. By default, items are compared
by identity (item === otherItem). If that’s insufficient for your use case, use the
optional comparator param of the method to specify how equality is determined.
Note that because this method requires traversing the list to find a match, it’s
preferable to use includesId when possible.
Returns
Section titled “Returns”Whether the collection includes the specified item.
map(iteratee: Function): U[]
Section titled “map(iteratee: Function): U[]”Returns
Section titled “Returns”The result of invoking iteratee on every item in the collection.
merge(…others: INormalizedCollection[] ): INormalizedCollection
Section titled “merge(…others: INormalizedCollection[]): INormalizedCollection”Example
Section titled “Example”// Returns a new collection where// the contents of `collection`, `otherCollection`, and `anotherCollection` are all present.// Assuming all collections had an item with ID '123', item '123' in the new collection// will be the '123' from `anotherCollection` because it was specified last in the merge// chain.collection.merge(otherCollection, anotherCollection);remove(id: string): void
Section titled “remove(id: string): void”Removes the item specified by the id.
toArray(): T[]
Section titled “toArray(): T[]”Returns
Section titled “Returns”The collection as an array.
toJSON(): string
Section titled “toJSON(): string”Returns
Section titled “Returns”The collection serialized as a JSON string.
toString(): string
Section titled “toString(): string”Returns
Section titled “Returns”The collection as a string. If trying to obtain the JSON-serialized version of
the collection, use toJSON.