new Rekord.Collection(values)
An extension of the Array class adding many useful functions and events. This is the base collection class in Rekord.
A collection of any type can be created via Rekord.collect
.
var nc = new Rekord.Collection([1, 2, 3, 4]);
Name | Type | Description |
---|---|---|
values |
Array |
optional
0 The initial set of values in this collection. |
- See:
Extends
- Rekord.Eventful
- Array
Members
-
staticRekord.Collection.Events
-
-
comparatorcomparisonCallback
-
A comparator to keep the collection sorted with.
Methods
-
add(value, delaySort){Rekord.Collection}
-
Adds an element to this collection - sorting the collection if a comparator is set on this collection and
delaySort
is not a specified or a true value.var a = Rekord.collect(1, 2, 3, 4); a.add( 5 ); // [1, 2, 3, 4, 5]
Name Type Default Description value
Any The value to add to this collection.
delaySort
Boolean false optional Whether automatic sorting should be delayed until the user manually calls
sort
.Fires:
Returns:
Type Description Rekord.Collection - The reference to this collection. -
addAll(values, delaySort){Rekord.Collection}
-
Adds all elements in the given array to this collection - sorting the collection if a comparator is set on this collection and
delaySort
is not specified or a true value.var a = Rekord.collect(1, 2, 3, 4); a.addAll( [5, 6] ); // [1, 2, 3, 4, 5, 6]
Name Type Default Description values
Array.<Any> The values to add to this collection.
delaySort
Boolean false optional Whether automatic sorting should be delayed until the user manually calls
sort
.Fires:
Returns:
Type Description Rekord.Collection - The reference to this collection. -
addComparator(comparator, nullsFirst){Rekord.Collection}
-
Adds a comparator to the existing comparator. This added comparator is ran after the current comparator when it finds two elements equal. If no comparator exists on this collection then it's set to the given comparator.
Name Type Default Description comparator
ComparatorInput The comparator input to convert to a comparison function.
nullsFirst
Boolean false optional When a comparison is done involving a null/undefined value this can determine which is ordered before the other.
Fires:
Returns:
Type Description Rekord.Collection -
aggregate(resolver, validator, process, getResult){Any}
-
Iterates over all elements in this collection and passes them through the
resolver
function. The returned value is passed through thevalidator
function and if that returns true the resolved value is passed through theprocess
function. After iteration, thegetResult
function is executed and the returned value is returned by this function.Name Type Description resolver
function The function which takes an element in this collection and returns a value based on that element.
validator
function The function which takes the resolved value and determines whether it passes some test.
process
function The function which is given the resolved value if it passes the test.
getResult
function The function which is executed at the end of iteration and the result is is returned by this function.
Returns:
Type Description Any - The value returned by getResult
. -
avg(numbers){Number}
-
Averages all numbers resolved from the given property expression and returns the result.
var c = Rekord.collect([2, 3, 4]); c.avg(); // 3 var d = Rekord.collect([{age: 5}, {age: 4}, {age: 2}]); d.avg('age'); // 3.66666
Name Type Description numbers
propertyResolverInput optional The expression which converts an element in this collection to a number.
- See:
-
- Rekord.createNumberResolver
Returns:
Type Description Number - The average of all valid numbers found in this collection. -
change(callback, context){function}
-
Adds a listener for change events on this collection.
Name Type Description callback
function A function to call every time a change occurs in this collection.
context
Object optional The desired context (this) for the given callback function.
Returns:
Type Description function - A function to call to stop listening for change events. -
chunk(chunkSize, out){Array}
-
Breaks up the collection into an array of arrays of a maximum size (chunks). A destination array can be used to avoid re-allocating arrays.
var c = Rekord.collect([1, 2, 3, 4, 5, 6, 7, 8, 9]); c.chunk(4); // [[1, 2, 3, 4], [5, 6, 7, 8], [9]]
Name Type Description chunkSize
Number The maximum number of elements that can exist in a chunk.
out
Array optional The destination array to place the chunks.
Returns:
Type Description Array - The array of chunks of elements taken from this collection. -
Clears all elements from this collection.
var a = Rekord.collect(1, 2, 3, 4); a.clear(); // []
Fires:
Returns:
Type Description Rekord.Collection - The reference to this collection. -
Returns a clone of this collection.
Returns:
Type Description Rekord.Collection - The reference to a clone collection. -
Returns a clone of this collection.
Returns:
Type Description Rekord.Collection - The reference to a clone collection. -
cloneEmpty(){Rekord.Collection}
-
Returns an empty clone of this collection.
Returns:
Type Description Rekord.Collection - The reference to a clone collection. -
cloneEmpty(){Rekord.Collection}
-
Returns an empty clone of this collection.
Returns:
Type Description Rekord.Collection - The reference to a clone collection. -
complement(collection, out, equals){Array}
-
Returns a collection of elements that exist in the given collection but not in this collection.
var a = Rekord.collect(1, 2, 3, 4); var b = Rekord.collect(1, 3, 5); var c = a.complement( b ); // [5]
Name Type Default Description collection
Array The array of elements that could exist in the resulting collection.
out
Array this.cloneEmpty() optional The array to place the elements that exist in given collection but not in this collection. If this is not given - a collection of this type will be created.
equals
equalityCallback Rekord.equalsStrict optional The function which determines whether one of the elements that exist in this collection are equivalent to an element that exists in the given collection.
Returns:
Type Description Array - The collection of elements that exist in the given collection and not this collection. -
contains(properties, value, equals){Boolean}
-
Determines whether at least one element in this collection matches the given criteria.
var c = Rekord.collect([{age: 2}, {age: 6}]); c.contains('age', 2); // true c.contains('age', 3); // false c.contains('age'); // true c.contains('name'); // false
Name Type Default Description properties
whereInput optional The expression used to create a function to test the elements in this collection.
value
Any optional When the first argument is a string this argument will be treated as a value to compare to the value of the named property on the object passed through the filter function.
equals
equalityCallback Rekord.equalsStrict optional An alternative function can be used to compare to values.
- See:
Returns:
Type Description Boolean - True if any of the elements passed the test function, otherwise false. -
count(properties){Number}
-
Counts the number of elements in this collection that has a value for the given property expression.
var c = Rekord.collect([{age: 2}, {age: 3}, {taco: 4}]); c.count('age'); // 2 c.count('taco'); // 1 c.count(); // 3
Name Type Description properties
propertyResolverInput optional The expression which converts one value into another.
Returns:
Type Description Number - The number of elements that had values for the property expression. -
countWhere(properties, value, equals){Number}
-
Counts the number of elements in this collection that past the test function generated by
Rekord.createWhere
.var c = Rekord.collect([{name: 't1', done: 1}, {name: 't2', done: 0}, {name: 't3', done: 1}, {name: 't4'}]); c.countWhere('done'); // 3 c.countWhere('done', 0); // 1 c.countWhere('done', 1); // 2
Name Type Default Description properties
whereInput optional The expression used to create a function to test the elements in this collection.
value
Any optional When the first argument is a string this argument will be treated as a value to compare to the value of the named property on the object passed through the filter function.
equals
equalityCallback Rekord.equalsStrict optional An alternative function can be used to compare to values.
- See:
Returns:
Type Description Number - The number of elements in the collection that passed the test. -
each(callback, context){Rekord.Collection}
-
Iterates over each element in this collection and passes the element and it's index to the given function. An optional function context can be given.
Name Type Description callback
function The function to invoke for each element of this collection passing the element and the index where it exists.
context
Object optional The context to the callback function.
Returns:
Type Description Rekord.Collection - The reference to this collection. -
eachWhere(callback, properties, value, equals){Rekord.Collection}
-
Iterates over each element in this collection that matches the where expression and passes the element and it's index to the given function.
Name Type Default Description callback
function The function to invoke for each element of this collection passing the element and the index where it exists.
properties
whereInput optional value
Any optional equals
equalityCallback Rekord.equalsStrict optional - See:
Returns:
Type Description Rekord.Collection - The reference to this collection. -
filtered(whereProperties, whereValue, whereEquals){Rekord.FilteredCollection}
-
Creates a sub view of this collection known as a filtered collection. The resulting collection changes when this collection changes. Any time an element is added or removed to this collection it may be added or removed from the filtered collection if it fits the filter function. The filter function is created by passing the arguments of this function to
Rekord.createWhere
.Name Type Description whereProperties
whereInput optional whereValue
Any optional whereEquals
equalityCallback optional - See:
Returns:
Type Description Rekord.FilteredCollection - The newly created live filtered view of this collection. -
first(properties, delim){Any}
-
Returns the first non-null value in this collection given a property expression. If no non-null values exist for the given property expression, then undefined will be returned.
var c = Rekord.collect([{x: 5}, {y: 6}, {y: 4}, {z: 7}]); c.first('y'); // 6 c.first(); // {x: 5}
Name Type Default Description properties
propertyResolverInput optional The expression which converts one value into another.
delim
String ',' optional A delimiter to use to join multiple properties into a string.
Returns:
Type Description Any - -
firstWhere(whereProperties, whereValue, whereEquals){Any}
-
Returns the first element where the given expression is true.
var c = Rekord.collect([{x: 5}, {y: 6}, {y: 6, age: 8}, {z: 7}]); c.firstWhere('y', 6); // {x: 6} c.firstWhere(); // {x: 5}
Name Type Default Description whereProperties
whereInput optional The expression used to create a function to test the elements in this collection.
whereValue
Any optional When the first argument is a string this argument will be treated as a value to compare to the value of the named property on the object passed through the filter function.
whereEquals
equalityCallback Rekord.equalsStrict optional An alternative function can be used to compare to values.
- See:
Returns:
Type Description Any - The first element in this collection that matches the given expression. -
group(grouping){Rekord.Collection}
-
Groups the elements into sub collections given some property expression to use as the value to group by.
var c = Rekord.collect([ { name: 'Tom', age: 6, group: 'X' }, { name: 'Jon', age: 7, group: 'X' }, { name: 'Rob', age: 8, group: 'X' }, { name: 'Bon', age: 9, group: 'Y' }, { name: 'Ran', age: 10, group: 'Y' }, { name: 'Man', age: 11, group: 'Y' }, { name: 'Tac', age: 12, group: 'Z' } ]); c.group({by: 'group'}); // [{group: 'X', $count: 3, $group: [...]}, // {group: 'Y', $count: 3, $group: [...]}, // {group: 'Z', $count: 1, $group: [.]}] c.group({by: 'group', select: {age: 'avg', name: 'first'}}); // [{group: 'X', age: 7, name: 'Tom', $count: 3, $group: [...]}, // {group: 'Y', age: 9, name: 'Bon', $count: 3, $group: [...]}, // {group: 'Z', age: 12, name: 'Tac', $count: 1, $group: [.]}] c.group({by: 'group', track: false, count: false}); // [{group: 'X'}, {group: 'Y'}, {group: 'Z'}] var havingMoreThanOne = function(grouping, groupElements) { return groupElements.length > 0; }; c.group({by: 'group', select: {age: 'avg'}, comparator: '-age', having: havingMoreThanOne, track: false, count: false}); // [{group: 'Y', age: 9}, // {group: 'X', age: 7}]
Name Type Description grouping
Object An object specifying how elements in this collection are to be grouped and what properties from the elements should be aggregated in the resulting groupings.
- `by`: A property expression that resolves how elements will be grouped. - `bySeparator`: When an array or object property expression is specified, this is the string that joins them. - `select`: An object which contains properties that should be aggregated where the value is the aggregate collection function to call (sum, avg, count, first, last, etc). - `having`: A having expression which takes a grouping and the grouped elements and determines whether the grouping should be in the final result. - `comparator`: A comparator for sorting the resulting collection of groupings. - `comparatorNullsFirst`: Whether nulls should be sorted to the top. - `track`: Whether all elements in the group should exist in a collection in the `$group` property of each grouping. - `count`: Whether the number of elements in the group should be placed in the `$count` property of each grouping.
Returns:
Type Description Rekord.Collection - A collection of groupings. -
indexOf(value, equals){Number}
-
Returns the index of the given element in this collection or returns -1 if the element doesn't exist in this collection.
var c = Rekord.collect(1, 2, 3, 4); c.indexOf( 1 ); // 0 c.indexOf( 2 ); // 1 c.indexOf( 5 ); // -1
Name Type Default Description value
Any The value to search for.
equals
equalityCallback Rekord.equalsStrict optional The function which determines whether one of the elements that exist in this collection are equivalent to the given value.
- See:
-
- Rekord.equals
- Rekord.equalsStrict
Returns:
Type Description Number - The index of the element in this collection or -1 if it was not found. -
insertAt(i, value, delaySort){Rekord.Collection}
-
Inserts an element into this collection at the given index - sorting the collection if a comparator is set on this collection and
delaySort
is not specified or a true value.var c = Rekord.collect(1, 2, 3, 4); c.insertAt( 0, 0 ); // [0, 1, 2, 3, 4] c.insertAt( 2, 1.5 ); // [0, 1, 1.5, 2, 3, 4]
Name Type Default Description i
Number The index to insert the element at.
value
Any The value to insert into the collection.
delaySort
Boolean false optional Whether automatic sorting should be delayed until the user manually calls
sort
.Fires:
Returns:
Type Description Rekord.Collection - The reference to this collection. -
intersect(collection, out, equals){Array}
-
Returns a collection of elements that are shared between this collection and the given collection.
var a = Rekord.collect(1, 2, 3, 4); var b = Rekord.collect(1, 3, 5); var c = a.intersect( b ); // [1, 3]
Name Type Default Description collection
Array The collection of elements to intersect with this collection.
out
Array this.cloneEmpty() optional The array to place the elements that exist in both this collection and the given collection. If this is not given - a collection of this type will be created.
equals
equalityCallback Rekord.equalsStrict optional The function which determines whether one of the elements that exist in this collection are equivalent to an element that exists in the given collection.
Returns:
Type Description Array - The collection of elements that exist in both collections. -
isSorted(comparator, nullsFirst){Boolean}
-
Determines if the collection is currently sorted based on the current comparator of the collection unless a comparator is given
Name Type Default Description comparator
ComparatorInput optional The comparator input to convert to a comparison function.
nullsFirst
Boolean false optional When a comparison is done involving a null/undefined value this can determine which is ordered before the other.
Returns:
Type Description Boolean -
last(properties, delim){Any}
-
Returns the last non-null value in this collection given a property expression. If no non-null values exist for the given property expression, then undefined will be returned.
var c = Rekord.collect([{x: 5}, {y: 6}, {y: 4}, {z: 7}]); c.last('y'); // 4 c.last(); // {z: 7}
Name Type Default Description properties
propertyResolverInput optional The expression which converts one value into another.
delim
String ',' optional A delimiter to use to join multiple properties into a string.
Returns:
Type Description Any - -
lastWhere(properties, value, equals){Any}
-
Returns the last element where the given expression is true.
var c = Rekord.collect([{x: 5}, {y: 6}, {y: 6, age: 8}, {z: 7}]); c.lastWhere('y', 6); // {x: 6, age: 8} c.lastWhere(); // {z: 7}
Name Type Default Description properties
whereInput optional The expression used to create a function to test the elements in this collection.
value
Any optional When the first argument is a string this argument will be treated as a value to compare to the value of the named property on the object passed through the filter function.
equals
equalityCallback Rekord.equalsStrict optional An alternative function can be used to compare to values.
- See:
Returns:
Type Description Any - The last element in this collection that matches the given expression. -
max(properties, delim, startingValue){Any}
-
Returns the maximum value for the given property expression out of all the elements this collection.
var c = Rekord.collect({age: 6}, {age: 5}, {notage: 5}); c.max('age'); // 6
Name Type Default Description properties
propertyResolverInput optional The expression which takes an element in this container and resolves a value that can be compared to the current maximum.
delim
String ',' optional A delimiter to use to join multiple properties into a string.
startingValue
Any optional The initial maximum value. If a value is specified, it's compared against all elements in this collection until the comparator function finds a more maximal value. If it doesn't - this is the value returned.
- See:
-
- Rekord.createPropertyResolver
- Rekord.compare
Returns:
Type Description Any - The maximum value found. -
maxModel(comparator, startingValue){Any}
-
Returns the element with the maximum value given a comparator.
var c = Rekord.collect({age: 4}, {age: 5}, {age: 6}, {age: 3}); c.maxModel('age'); // {age: 6} c.maxModel('-age'); // {age: 3}
Name Type Description comparator
comparatorInput The comparator which calculates the maximum model.
startingValue
Any optional The initial maximum value. If a value is specified, it's compared against all elements in this collection until the comparator function finds a more maximal value. If it doesn't - this is the value returned.
Returns:
Type Description Any - The maximum element in the collection given the comparator function. -
min(properties, delim, startingValue){Any}
-
Returns the minimum value for the given property expression out of all the elements this collection.
var c = Rekord.collect({age: 6}, {age: 5}, {notage: 5}); c.min('age'); // 5
Name Type Default Description properties
propertyResolverInput optional The expression which takes an element in this container and resolves a value that can be compared to the current minimum.
delim
String ',' optional A delimiter to use to join multiple properties into a string.
startingValue
Any optional The initial minimum value. If a value is specified, it's compared against all elements in this collection until the comparator function finds a more minimal value. If it doesn't - this is the value returned.
- See:
-
- Rekord.createPropertyResolver
- Rekord.compare
Returns:
Type Description Any - The minimum value found. -
minModel(comparator, startingValue){Any}
-
Returns the element with the minimum value given a comparator.
var c = Rekord.collect({age: 4}, {age: 5}, {age: 6}, {age: 3}); c.minModel('age'); // {age: 3} c.minModel('-age'); // {age: 6}
Name Type Description comparator
comparatorInput The comparator which calculates the minimum model.
startingValue
Any optional The initial minimum value. If a value is specified, it's compared against all elements in this collection until the comparator function finds a more minimal value. If it doesn't - this is the value returned.
Returns:
Type Description Any - The minimum element in the collection given the comparator function. -
inherited on(events, callback, context){function}
-
Listens for every occurrence of the given events and invokes the callback each time any of them are triggered.
Name Type Description events
String | Array The event or events to listen to.
callback
function The function to invoke when any of the events are invoked.
context
Object optional The value of
this
when the callback is invoked. If not specified, the reference of the object this function exists on will bethis
.Returns:
Type Description function - A function to invoke to stop listening to all of the events given. -
inherited once(events, callback, context){function}
-
Listens for the first of the given events to be triggered and invokes the callback once.
Name Type Description events
String | Array The event or events to listen to.
callback
function The function to invoke when any of the events are invoked.
context
Object optional The value of
this
when the callback is invoked. If not specified, the reference of the object this function exists on will bethis
.Returns:
Type Description function - A function to invoke to stop listening to all of the events given. -
page(pageSize, pageIndex){Rekord.Page}
-
Creates a limited view of this collection known as a page. The resulting page object changes when this collection changes. At the very least the page size is required, and a starting page index can be specified.
Name Type Default Description pageSize
Number The maximum number of elements allowed in the page at once.
pageIndex
Number 0 optional The starting page offset. This isn't an element offset, but the element offset can be calculated by multiplying the page index by the page size.
Returns:
Type Description Rekord.Page - The newly created Page. -
pluck(values, keys, valuesDelim, keysDelim){Array|Object}
-
Plucks values from elements in the collection. If only a
values
property expression is given the result will be an array of resolved values. If thekeys
property expression is given, the result will be an object where the property of the object is determined by the key expression.var c = Rekord.collect([{age: 2, nm: 'T'}, {age: 4, nm: 'R'}, {age: 5, nm: 'G'}]); c.pluck(); // c c.pluck('age'); // [2, 4, 5] c.pluck('age', 'nm'); // {T: e, R: 4, G: 5} c.pluck(null, 'nm'); // {T: {age: 2, nm: 'T'}, R: {age: 4, nm: 'R'}, G: {age: 5, nm: 'G'}} c.pluck('{age}-{nm}'); // ['2-T', '4-R', '5-G']
Name Type Default Description values
propertyResolverInput optional The expression which converts an element into a value to pluck.
keys
propertyResolverInput optional The expression which converts an element into an object property (key).
valuesDelim
String ',' optional A delimiter to use to join multiple value properties into a string.
keysDelim
String ',' optional A delimiter to use to join multiple key properties into a string.
Returns:
Type Description Array | Object - The plucked values. -
pop(delaySort){Any}
-
Removes the last element in this collection and returns it - sorting the collection if a comparator is set on this collection and
delaySort
is no specified or a true value.var c = Rekord.collect(1, 2, 3, 4); c.pop(); // 4
Name Type Default Description delaySort
Boolean false optional Whether automatic sorting should be delayed until the user manually calls
sort
.Fires:
Returns:
Type Description Any - The element removed from the end of the collection. -
push(value){Number}
-
Adds one or more elements to the end of this collection - sorting the collection if a comparator is set on this collection.
var a = Rekord.collect(1, 2, 3, 4); a.push( 5, 6, 7 ); // 7 a // [1, 2, 3, 4, 5, 6, 7]
Name Type Description value
Any repeatable The values to add to this collection.
Fires:
Returns:
Type Description Number - The new length of this collection. -
random(){Any}
-
Returns a random element in this collection.
Returns:
Type Description Any - The randomly chosen element from this collection. -
reduce(reducer, initialValue){Any}
-
Reduces all the elements of this collection to a single value. All elements are passed to a function which accepts the currently reduced value and the current element and returns the new reduced value.
var reduceIt = function(curr, elem) { return curr + ( elem[0] * elem[1] ); }; var c = Rekord.collect([[2, 1], [3, 2], [5, 6]]); c.reduce( reduceIt, 0 ); // 38
Name Type Description reducer
function A function which accepts the current reduced value and an element and returns the new reduced value.
initialValue
Any optional The first value to pass to the reducer function.
Returns:
Type Description Any - The reduced value. -
remove(value, delaySort, equals){Any}
-
Removes the given value from this collection if it exists - sorting the collection if a comparator is set on this collection and
delaySort
is not specified or a true value.var c = Rekord.collect(1, 2, 3, 4); c.remove( 1 ); // 1 c.remove( 5 ); // undefined c // [2, 3, 4]
Name Type Default Description value
Any The value to remove from this collection if it exists.
delaySort
Boolean false optional Whether automatic sorting should be delayed until the user manually calls
sort
.equals
equalityCallback Rekord.equalsStrict optional The function which determines whether one of the elements that exist in this collection are equivalent to the given value.
Fires:
Returns:
Type Description Any - The element removed from this collection. -
removeAll(values, delaySort, equals){Array.<Any>}
-
Removes the given values from this collection - sorting the collection if a comparator is set on this collection and
delaySort
is not specified or a true value.var c = Rekord.collect(1, 2, 3, 4); c.removeAll( [1, 5] ); // [1] c // [2, 3, 4]
Name Type Default Description values
Array.<Any> The values to remove from this collection if they exist.
delaySort
Boolean false optional Whether automatic sorting should be delayed until the user manually calls
sort
.equals
equalityCallback Rekord.equalsStrict optional The function which determines whether one of the elements that exist in this collection are equivalent to any of the given values.
Fires:
Returns:
Type Description Array.<Any> - The elements removed from this collection. -
removeAt(i, delaySort){Any}
-
Removes the element in this collection at the given index
i
- sorting the collection if a comparator is set on this collection anddelaySort
is not specified or a true value.var c = Rekord.collect(1, 2, 3, 4); c.removeAt( 1 ); // 2 c.removeAt( 5 ); // undefined c // [1, 3, 4]
Name Type Default Description i
Number The index of the element to remove.
delaySort
Boolean false optional Whether automatic sorting should be delayed until the user manually calls
sort
.Fires:
Returns:
Type Description Any - The element removed, or undefined if the index was invalid. -
removeWhere(whereProperties, whereValue, whereEquals, out, delaySort){Rekord.Collection}
-
Removes elements from this collection that meet the specified criteria. The given criteria are passed to
Rekord.createWhere
to create a filter function. All elements removed are returnedvar isEven = function(x) { return x % 2 === 0; }; var c = Rekord.collect(1, 2, 3, 4); c.removeWhere( isEven ); // [2, 4]; c // [1, 3]
Name Type Default Description whereProperties
whereInput optional whereValue
Any optional whereEquals
equalityCallback optional out
Array this.cloneEmpty() optional The array to place the elements that match.
delaySort
Boolean false optional Whether automatic sorting should be delayed until the user manually calls
sort
.- See:
Fires:
Returns:
Type Description Rekord.Collection - The reference to this collection. -
reset(values){Rekord.Collection}
-
Resets the values in this collection with a new collection of values.
Name Type Description values
Array.<Any> optional The new array of values in this collection.
Fires:
Returns:
Type Description Rekord.Collection - The reference to this collection. -
reverse(){Rekord.Collection}
-
Reverses the order of elements in this collection.
var c = Rekord.collect(1, 2, 3, 4); c.reverse(); // [4, 3, 2, 1]
Fires:
Returns:
Type Description Rekord.Collection - The reference to this collection. -
setComparator(comparator, nullsFirst){Rekord.Collection}
-
Sets the comparator for this collection and performs a sort.
Name Type Default Description comparator
ComparatorInput The comparator input to convert to a comparison function.
nullsFirst
Boolean false optional When a comparison is done involving a null/undefined value this can determine which is ordered before the other.
Fires:
Returns:
Type Description Rekord.Collection -
shift(delaySort){Any}
-
Removes the first element in this collection and returns it - sorting the collection if a comparator is set on this collection and
delaySort
is no specified or a true value.var c = Rekord.collect(1, 2, 3, 4); c.shift(); // 1
Name Type Default Description delaySort
Boolean false optional Whether automatic sorting should be delayed until the user manually calls
sort
.Fires:
Returns:
Type Description Any - The element removed from the beginning of the collection. -
sort(comparator, nullsFirst, ignorePrimitive){Rekord.Collection}
-
Sorts the elements in this collection based on the current comparator unless a comparator is given. If a comparator is given it will not override the current comparator, subsequent operations to the collection may trigger a sort if the collection has a comparator.
Name Type Default Description comparator
ComparatorInput optional The comparator input to convert to a comparison function.
nullsFirst
Boolean false optional When a comparison is done involving a null/undefined value this can determine which is ordered before the other.
ignorePrimitive
Boolean false optional Sorting is automatically done for non-primitive collections if a comparator exists. This flag ensures primitive collections aren't sorted after every operation.
Fires:
Returns:
Type Description Rekord.Collection - The reference to this collection. -
splice(start, deleteCount, values){Array.<Any>}
-
Splices elements out of and into this collection - sorting the collection if a comparator is set on this collection.
Name Type Description start
Number Index at which to start changing the array (with origin 0). 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 elements from the end.
deleteCount
Number An integer indicating the number of old array elements to remove. If deleteCount is 0, no elements are removed. In this case, you should specify at least one new element. If deleteCount is greater than the number of elements left in the array starting at start, then all of the elements through the end of the array will be deleted. If deleteCount is omitted, deleteCount will be equal to (arr.length - start).
values
Any repeatable The elements to add to the array, beginning at the start index. If you don't specify any elements, splice() will only remove elements from the array.
Fires:
Returns:
Type Description Array.<Any> - The array of deleted elements. -
subtract(collection, out, equals){Array}
-
Returns a collection with elements that exist in this collection but does not exist in the given collection.
var a = Rekord.collect(1, 2, 3, 4); var b = Rekord.collect(1, 3, 5); var c = a.subtract( b ); // [2, 4]
Name Type Default Description collection
Array The array of elements that shouldn't exist in the resulting collection.
out
Array this.cloneEmpty() optional The array to place the elements that exist in this collection but not in the given collection. If this is not given - a collection of this type will be created.
equals
equalityCallback Rekord.equalsStrict optional The function which determines whether one of the elements that exist in this collection are equivalent to an element that exists in the given collection.
Returns:
Type Description Array - The collection of elements that exist in this collection and not the given collection. -
sum(numbers){Number}
-
Sums all numbers resolved from the given property expression and returns the result.
var c = Rekord.collect([2, 3, 4]); c.sum(); // 9 var d = Rekord.collect([{age: 5}, {age: 4}, {age: 2}]); d.sum('age'); // 11
Name Type Description numbers
propertyResolverInput optional The expression which converts an element in this collection to a number.
- See:
-
- Rekord.createNumberResolver
Returns:
Type Description Number - The sum of all valid numbers found in this collection. -
toArray(){Array}
-
Returns a copy of this collection as a plain Array.
Returns:
Type Description Array - The copy of this collection as a plain array. -
unshift(value){Number}
-
Adds one or more elements to the beginning of this collection - sorting the collection if a comparator is set on this collection.
var a = Rekord.collect(1, 2, 3, 4); a.unshift( 5, 6, 7 ); // 7 a // [5, 6, 7, 1, 2, 3, 4]
Name Type Description value
Any repeatable The values to add to this collection.
Fires:
Returns:
Type Description Number - The new length of this collection. -
where(whereProperties, whereValue, whereEquals, out){Rekord.Collection}
-
Creates a copy of this collection with elements that match the supplied parameters. The parameters are passed to the
Rekord.createWhere
to generate a function which tests each element of this collection for inclusion in the newly created collection.var isEven = function() { return x % 2 == 0; }; var c = Rekord.collect(1, 2, 3, 4, 5); var w = c.where(isEven); // [2, 4]
Name Type Default Description whereProperties
whereInput optional whereValue
Any optional whereEquals
equalityCallback optional out
Array this.cloneEmpty() optional The array to place the elements that match.
- See:
Returns:
Type Description Rekord.Collection - The copy of this collection ran through a filtering function.
Events
-
An event triggered when a single value is added to a collection.
Name Type Description collection
Rekord.Collection The collection that triggered the event.
value
T The value added.
-
An event triggered when multiple values are added to a collection.
Name Type Description collection
Rekord.Collection The collection that triggered the event.
value
Array.<T> The values added.
-
changes
-
All events triggered by a collection when the contents of the collection changes.
Name Type Description collection
Rekord.Collection The collection that triggered the event.
-
cleared
-
An event triggered when a collection is cleared of all elements.
Name Type Description collection
Rekord.Collection The collection that triggered the event.
-
remove
-
An event triggered when a collection has an element removed at a given index.
Name Type Description collection
Rekord.Collection The collection that triggered the event.
removing
Any The element that was removed.
index
Number The index where the element was removed at.
-
removes
-
An event triggered when a collection has multiple elements removed.
Name Type Description collection
Rekord.Collection The collection that triggered the event.
removed
Array.<Any> The array of elements removed from the collection.
-
An event triggered when a collection's elements are entirely replaced by a new set of elements.
Name Type Description collection
Rekord.Collection The collection that triggered the event.
updated
Array The array of elements modified.
-
An event triggered when a collection is sorted. This may automatically be triggered by any method that modifies the collection.
Name Type Description collection
Rekord.Collection The collection that triggered the event.
-
updates
-
An event triggered when a collection has elements modified.
Name Type Description collection
Rekord.Collection The collection that triggered the event.
updated
Array The array of elements modified.