Collection component
$ component install apily/collection
Create a collection.
var users = new Collection();
Add model
to this collection,
emit add
event.
Add all the models
to this collection,
emit add_all
event.
Remove model
from the collection,
if the model exists, emit 'remove' event.
Remove all the models
from this collection,
emit remove_all
event.
Get the i
-th model.
Iterate each value and invoke fn(val, i)
.
users.each(function (user, i) {
console.log(user, i);
});
Map each return value from fn(val, i)
.
var names = users.map(function (user) {
return user.name.first;
});
Select all values that return a truthy value of fn(val, i)
.
var adults = users.select(function (user) {
return user.age > 18;
});
Select all unique values.
Reject all values that return a truthy value of fn(val, i)
.
var teens = users.reject(function (user) {
return user.age <= 18;
});
Return the first value when fn(val, i)
is truthy,
otherwise return undefined
.
var admin = users.find(function (user) {
return user.role == 'admin';
});
Return the last value when fn(val, i)
is truthy,
otherwise return undefined
.
var admin = users.findLast(function (user) {
return user.role == 'admin';
});
Assert that all invocations of fn(val, i)
are truthy.
var test = pets.all(function (pet) {
return pet.species == 'ferret'
});
Assert that none of the invocations of fn(val, i)
are truthy.
var test = pets.none(function (pet) {
return pet.admin;
});
Assert that at least one invocation of fn(val, i)
is truthy.
var test = pets.any(function(pet){
return pet.species == 'ferret';
});
Count the number of times fn(val, i)
returns true
.
Get the index of model
.
Check if this collection contains model
.
Extract a list of property values.
(The MIT License)
Copyright (c) 2013 Enrico Marino and Federico Spini
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.