🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

ampersand-collection-underscore-mixin

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ampersand-collection-underscore-mixin - npm Package Compare versions

Comparing version
1.0.0
to
1.0.1
.jshintignore

Sorry, the diff of this file is not supported yet

+12
{
"asi": false,
"expr": true,
"loopfunc": true,
"curly": false,
"evil": true,
"white": true,
"undef": true,
"node": true,
"indent": 4,
"eqnull": true
}
var _ = require('underscore');
var test = require('tape');
var AmpersandState = require('ampersand-state');
var AmpersandCollection = require('ampersand-collection');
var AmpersandUnderscoreMixins = require('../ampersand-collection-underscore-mixin');
var collection;
var Model = AmpersandState.extend({
props: {
id: 'number',
foo: 'string',
bar: 'string'
}
});
var Collection = AmpersandCollection.extend(AmpersandUnderscoreMixins, {
model: Model
});
var methods = ['forEach', 'each', 'map', 'collect', 'reduce', 'foldl',
'inject', 'reduceRight', 'foldr', 'find', 'detect', 'filter', 'select',
'reject', 'every', 'all', 'some', 'any', 'include', 'contains', 'invoke',
'max', 'min', 'toArray', 'size', 'first', 'head', 'take', 'initial', 'rest',
'tail', 'drop', 'last', 'without', 'difference', 'indexOf', 'shuffle',
'lastIndexOf', 'isEmpty', 'chain', 'sample', 'partition',
'groupBy', 'countBy', 'sortBy', 'indexBy'
];
test('extended collection contains all necessary methods', function (t) {
_.each(methods, function (method) {
t.ok(Collection.prototype[method], 'extended collection contains ' + method + ' method');
});
t.end();
});
test('`where` and `findWhere` methods should filter a collection based on a given attributes', function (t) {
var collection = new Collection([
{ id: 1, foo: 'baz', bar: 'baz' },
{ id: 2, foo: 'baz', bar: 'baz' },
{ id: 3, foo: 'baz', bar: 'qux' },
{ id: 4, foo: 'qux', bar: 'qux' },
{ id: 5, foo: 'qux', bar: 'qux' },
{ id: 6, foo: 'qux', bar: 'baz' }
]);
var whereSingleAttr = collection.where({
foo: 'baz'
});
t.equal(whereSingleAttr.length, 3, 'find all matching models for a given attribute');
var whereMultipleAttr = collection.where({
foo: 'qux',
bar: 'qux'
});
t.equal(whereMultipleAttr.length, 2, 'find all matching models for a given attributes');
var findWhereSingleAttr = collection.findWhere({
foo: 'baz'
});
t.ok(findWhereSingleAttr, 'return first matching model for a given attribute');
t.equal(findWhereSingleAttr.get('id'), 1, 'verify that returned model is indeed first possible match');
var findWhereMultipleAttr = collection.findWhere({
foo: 'qux',
bar: 'qux'
});
t.ok(findWhereMultipleAttr, 'return first matching model for a given attributes');
t.equal(findWhereMultipleAttr.get('id'), 4, 'verify that returned model is indeed first possible match');
t.end();
});
test('`pluck` method should get attribute value from each model', function (t) {
var collection = new Collection([
{ id: 1, foo: 'baz', bar: 'qux' },
{ id: 2, foo: 'qux', bar: 'baz' }
]);
var foo = collection.pluck('foo');
t.deepEqual(foo, ['baz', 'qux']), 'verify that returned attribute values are correct';
var bar = collection.pluck('bar');
t.deepEqual(bar, ['qux', 'baz'], 'verify that returned attribute values are correct');
t.end();
});
{
"framework": "tap",
"src_files": [
"ampersand-collection-underscore-mixin.js",
"test/*.js"
],
"serve_files": [
"tests.bundle.js"
],
"before_tests": "browserify test/index.js -o tests.bundle.js",
"after_tests": "rm tests.bundle.js",
"launch_in_dev": ["PhantomJS", "Chrome"]
}
+9
-9

@@ -16,5 +16,5 @@ var _ = require('underscore');

// Mix in each Underscore method as a proxy to `Collection#models`.
_.each(methods, function(method) {
_.each(methods, function (method) {
if (!_[method]) return;
mixins[method] = function() {
mixins[method] = function () {
var args = slice.call(arguments);

@@ -30,6 +30,6 @@ args.unshift(this.models);

// Use attributes instead of properties.
_.each(attributeMethods, function(method) {
_.each(attributeMethods, function (method) {
if (!_[method]) return;
mixins[method] = function(value, context) {
var iterator = _.isFunction(value) ? value : function(model) {
mixins[method] = function (value, context) {
var iterator = _.isFunction(value) ? value : function (model) {
return model.get ? model.get(value) : model[value];

@@ -43,5 +43,5 @@ };

// `filter`.
mixins.where = function(attrs, first) {
mixins.where = function (attrs, first) {
if (_.isEmpty(attrs)) return first ? void 0 : [];
return this[first ? 'find' : 'filter'](function(model) {
return this[first ? 'find' : 'filter'](function (model) {
var value;

@@ -58,3 +58,3 @@ for (var key in attrs) {

// of `find`.
mixins.findWhere = function(attrs) {
mixins.findWhere = function (attrs) {
return this.where(attrs, true);

@@ -64,3 +64,3 @@ };

// Plucks an attribute from each model in the collection.
mixins.pluck = function(attr) {
mixins.pluck = function (attr) {
return _.invoke(this.models, 'get', attr);

@@ -67,0 +67,0 @@ };

{
"name": "ampersand-collection-underscore-mixin",
"description": "A mixin for extending ampersand-collection with underscore methods.",
"version": "1.0.0",
"version": "1.0.1",
"author": "Henrik Joreteg <henrik@andyet.net>",

@@ -13,3 +13,7 @@ "bugs": {

"devDependencies": {
"ampersand-collection": "^1.0.0"
"ampersand-collection": "^1.0.0",
"ampersand-state": "^4.3.2",
"precommit-hook": "~0.3.10",
"run-browser": "~1.2.0",
"tape": "~2.12.1"
},

@@ -27,3 +31,24 @@ "homepage": "https://github.com/ampersandjs/ampersand-collection-underscore-mixin",

"url": "git://github.com/ampersandjs/ampersand-collection-underscore-mixin"
},
"scripts": {
"test": "node test/index.js",
"validate": "jshint .",
"start": "run-browser test/index.js"
},
"testling": {
"files": "test/*.js",
"browsers": [
"ie/9..latest",
"firefox/17..latest",
"firefox/nightly",
"chrome/22..latest",
"chrome/canary",
"opera/12..latest",
"opera/next",
"safari/5.1..latest",
"ipad/6.0..latest",
"iphone/6.0..latest",
"android-browser/4.2..latest"
]
}
}

@@ -5,2 +5,6 @@ # ampersand-collection-underscore-mixin

If you're using an [ampersand-rest-collection](http://ampersandjs.com/docs/#ampersand-rest-collection) this is already mixed in for you.
Out of the box, ampersand-collections proxy the [ES5 iteration methods already](http://ampersandjs.com/docs/#ampersand-collection-proxied-es5-array-methods-9) so you don't _have_ to use this mixin, but if you want all the underscore methods, or better browser support, you can use this.
## install

@@ -7,0 +11,0 @@