ampersand-collection-lodash-mixin
Advanced tools
Comparing version 1.0.2 to 2.0.0
@@ -1,2 +0,35 @@ | ||
var _ = require('lodash'); | ||
/*$AMPERSAND_VERSION*/ | ||
var isFunction = require('lodash.isfunction'); | ||
var _ = { | ||
countBy: require('lodash.countby'), | ||
difference: require('lodash.difference'), | ||
drop: require('lodash.drop'), | ||
each: require('lodash.foreach'), | ||
every: require('lodash.every'), | ||
filter: require('lodash.filter'), | ||
find: require('lodash.find'), | ||
forEach: require('lodash.foreach'), | ||
groupBy: require('lodash.groupby'), | ||
includes: require('lodash.includes'), | ||
indexBy: require('lodash.indexby'), | ||
indexOf: require('lodash.indexof'), | ||
initial: require('lodash.initial'), | ||
invoke: require('lodash.invoke'), | ||
isEmpty: require('lodash.isempty'), | ||
lastIndexOf: require('lodash.lastindexof'), | ||
map: require('lodash.map'), | ||
max: require('lodash.max'), | ||
min: require('lodash.min'), | ||
partition: require('lodash.partition'), | ||
reduce: require('lodash.reduce'), | ||
reduceRight: require('lodash.reduceright'), | ||
reject: require('lodash.reject'), | ||
rest: require('lodash.rest'), | ||
sample: require('lodash.sample'), | ||
shuffle: require('lodash.shuffle'), | ||
some: require('lodash.some'), | ||
sortBy: require('lodash.sortby'), | ||
take: require('lodash.take'), | ||
without: require('lodash.without') | ||
}; | ||
var slice = [].slice; | ||
@@ -6,12 +39,10 @@ var mixins = {}; | ||
// Lodash methods that we want to implement on the Collection. | ||
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' | ||
// lodash methods that we want to implement on the Collection. | ||
var methods = ['forEach', 'each', 'map', 'reduce', 'reduceRight', 'find', | ||
'filter', 'reject', 'every', 'some', 'includes', 'invoke', 'max', 'min', | ||
'take', 'initial', 'rest', 'drop', 'without', 'difference', 'indexOf', 'shuffle', | ||
'lastIndexOf', 'isEmpty', 'sample', 'partition' | ||
]; | ||
// Mix in each Lodash method as a proxy to `Collection#models`. | ||
// Mix in each lodash method as a proxy to `Collection#models`. | ||
_.each(methods, function (method) { | ||
@@ -26,3 +57,3 @@ if (!_[method]) return; | ||
// Lodash methods that take a property name as an argument. | ||
// lodash methods that take a property name as an argument. | ||
var attributeMethods = ['groupBy', 'countBy', 'sortBy', 'indexBy']; | ||
@@ -34,3 +65,3 @@ | ||
mixins[method] = function (value, context) { | ||
var iterator = _.isFunction(value) ? value : function (model) { | ||
var iterator = isFunction(value) ? value : function (model) { | ||
return model.get ? model.get(value) : model[value]; | ||
@@ -67,2 +98,19 @@ }; | ||
// We implement the following trivial methods ourselves. | ||
// Gets first model | ||
mixins.first = function () { | ||
return this.models[0]; | ||
}; | ||
// Gets last model | ||
mixins.last = function () { | ||
return this.models[this.models.length - 1]; | ||
}; | ||
// Gets size of collection | ||
mixins.size = function () { | ||
return this.models.length; | ||
}; | ||
module.exports = mixins; |
{ | ||
"name": "ampersand-collection-lodash-mixin", | ||
"description": "A mixin for extending ampersand-collection with lodash methods.", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
"contributors": [ | ||
"Samuel Reed <sam@bitmex.com>" | ||
], | ||
"browserify": { | ||
"transform": [ | ||
"ampersand-version" | ||
] | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/strml/ampersand-collection-lodash-mixin/issues" | ||
"url": "https://github.com/ampersandjs/ampersand-collection-lodash-mixin/issues" | ||
}, | ||
"dependencies": { | ||
"lodash": "^2.4.1" | ||
"ampersand-version": "^1.0.0", | ||
"lodash.countby": "^3.1.0", | ||
"lodash.difference": "^3.1.0", | ||
"lodash.drop": "^3.0.0", | ||
"lodash.every": "^3.2.0", | ||
"lodash.filter": "^3.1.0", | ||
"lodash.find": "^3.2.0", | ||
"lodash.foreach": "^3.0.2", | ||
"lodash.groupby": "^3.1.0", | ||
"lodash.includes": "^3.1.0", | ||
"lodash.indexby": "^3.1.0", | ||
"lodash.indexof": "^3.0.2", | ||
"lodash.initial": "^3.0.0", | ||
"lodash.invoke": "^3.1.0", | ||
"lodash.isempty": "^3.0.1", | ||
"lodash.isfunction": "^3.0.2", | ||
"lodash.lastindexof": "^3.0.2", | ||
"lodash.map": "^3.1.0", | ||
"lodash.max": "^3.2.0", | ||
"lodash.min": "^3.2.0", | ||
"lodash.partition": "^3.1.0", | ||
"lodash.reduce": "^3.1.0", | ||
"lodash.reduceright": "^3.1.0", | ||
"lodash.reject": "^3.1.0", | ||
"lodash.rest": "^3.0.0", | ||
"lodash.sample": "^3.0.0", | ||
"lodash.shuffle": "^3.0.0", | ||
"lodash.some": "^3.2.0", | ||
"lodash.sortby": "^3.1.0", | ||
"lodash.take": "^3.0.0", | ||
"lodash.without": "^3.1.0" | ||
}, | ||
@@ -18,10 +50,16 @@ "devDependencies": { | ||
"ampersand-state": "^4.3.2", | ||
"precommit-hook": "~0.3.10", | ||
"run-browser": "~1.2.0", | ||
"tape": "~2.12.1" | ||
"browserify": "^9.0.4", | ||
"jshint": "^2.5.5", | ||
"phantomjs": "^1.9.7-15", | ||
"precommit-hook": "^2.0.1", | ||
"run-browser": "^2.0.2", | ||
"tap-spec": "^2.2.2", | ||
"tape": "^3.5.0", | ||
"tape-run": "^1.0.0" | ||
}, | ||
"homepage": "https://github.com/strml/ampersand-collection-lodash-mixin", | ||
"homepage": "https://github.com/ampersandjs/ampersand-collection-lodash-mixin", | ||
"keywords": [ | ||
"ampersand", | ||
"collection", | ||
"underscore", | ||
"lodash" | ||
@@ -33,9 +71,14 @@ ], | ||
"type": "git", | ||
"url": "git://github.com/strml/ampersand-collection-lodash-mixin" | ||
"url": "git://github.com/ampersandjs/ampersand-collection-lodash-mixin" | ||
}, | ||
"scripts": { | ||
"test": "node test/index.js", | ||
"validate": "jshint .", | ||
"start": "run-browser test/index.js" | ||
"test": "browserify test/index.js | tape-run | tap-spec", | ||
"lint": "jshint .", | ||
"start": "run-browser test/index.js", | ||
"list": "node -e \"console.log(Object.keys(require('./ampersand-collection-lodash-mixin')).sort().join(', '))\"" | ||
}, | ||
"pre-commit": [ | ||
"lint", | ||
"test" | ||
], | ||
"testling": { | ||
@@ -42,0 +85,0 @@ "files": "test/*.js", |
# ampersand-collection-lodash-mixin | ||
A mixin for extending ampersand-collection with lodash methods. | ||
This is a fork of [ampersand-collection-underscore-mixin](https://github.com/AmpersandJS/ampersand-collection-underscore-mixin). | ||
Lead Maintainer: [Clemens Stolle](https://github.com/klaemo) | ||
If you're using an [ampersand-rest-collection](http://ampersandjs.com/docs/#ampersand-rest-collection) this is already mixed in for you. | ||
A mixin for extending ampersand-collection with a bunch of lodash methods. | ||
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 lodash methods, or better browser support, you can use this. | ||
~~If you're using an [ampersand-rest-collection](http://ampersandjs.com/docs/#ampersand-rest-collection) this is already mixed in for you.~~ | ||
(Not yet, but maybe soon? :D) | ||
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 lodash methods, or better browser support, you can use this. | ||
This mixin adds the following lodash methods: | ||
``` | ||
countBy, difference, drop, each, every, filter, find, findWhere, first, | ||
forEach, groupBy, includes, indexBy, indexOf, initial, invoke, isEmpty, last, | ||
lastIndexOf, map, max, min, partition, pluck, reduce, reduceRight, reject, | ||
rest, sample, shuffle, size, some, sortBy, take, where, without | ||
``` | ||
It weighs in at about `7.5kb` gzipped. | ||
## install | ||
@@ -20,6 +33,6 @@ | ||
var Collection = require('ampersand-collection'); | ||
var underscoreMixin = require('ampersand-collection-lodash-mixin'); | ||
var lodashMixin = require('ampersand-collection-lodash-mixin'); | ||
module.exports = Collection.extend(underscoreMixin, { | ||
module.exports = Collection.extend(lodashMixin, { | ||
sampleMethod: function () { | ||
@@ -38,7 +51,8 @@ // now we've got lodash methods | ||
All credit for this approach in backbone goes to Jeremy Ashkenas and the rest of the Backbone authors. | ||
All credit for underscore and this approach in backbone goes to Jeremy Ashkenas and the rest of the Backbone and Underscore authors. | ||
All credit for lodash goes to John-David Dalton. | ||
Lodash is written by John-David Dalton. | ||
Big thanks to [@STRML](https://github.com/STRML) who generously gave us this module name on npm. If you're interested in his version, it's still there public, you can just keep using version `1.0.2`. | ||
If you like this follow [@HenrikJoreteg](http://twitter.com/henrikjoreteg) on twitter. | ||
If you like this follow [@HenrikJoreteg](http://twitter.com/henrikjoreteg) and/or [@klaemo](http://twitter.com/klaemo) on twitter. | ||
@@ -45,0 +59,0 @@ ## license |
@@ -1,2 +0,1 @@ | ||
var _ = require('lodash'); | ||
var test = require('tape'); | ||
@@ -6,2 +5,3 @@ var AmpersandState = require('ampersand-state'); | ||
var AmpersandLodashMixins = require('../ampersand-collection-lodash-mixin'); | ||
var without = require('lodash.without'); | ||
var collection; | ||
@@ -21,8 +21,8 @@ | ||
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', | ||
var methods = ['forEach', 'each', 'map', 'reduce', | ||
'reduceRight', 'find', 'filter', | ||
'reject', 'every', 'some', 'includes', 'invoke', | ||
'max', 'min', 'size', 'first', 'take', 'initial', 'rest', | ||
'drop', 'last', 'without', 'difference', 'indexOf', 'shuffle', | ||
'lastIndexOf', 'isEmpty', 'sample', 'partition', | ||
'groupBy', 'countBy', 'sortBy', 'indexBy' | ||
@@ -32,3 +32,3 @@ ]; | ||
test('extended collection contains all necessary methods', function (t) { | ||
_.each(methods, function (method) { | ||
methods.forEach(function (method) { | ||
t.ok(Collection.prototype[method], 'extended collection contains ' + method + ' method'); | ||
@@ -39,2 +39,26 @@ }); | ||
test('methods should be callable on the collection instance', function (t) { | ||
var collection = new Collection([ | ||
{ id: 1, foo: 'baz', bar: 'baz' }, | ||
{ id: 2, foo: 'baz', bar: 'baz' }, | ||
]); | ||
without(methods, 'groupBy', 'countBy', 'sortBy', 'indexBy').forEach(function (method) { | ||
t.doesNotThrow(function () { collection[method](); }, method + ' should be callable'); | ||
}); | ||
t.end(); | ||
}); | ||
test('groupBy, countBy, sortBy, indexBy should be callable on the collection instance', function (t) { | ||
var collection = new Collection([ | ||
{ id: 1, foo: 'baz', bar: 'baz' }, | ||
{ id: 2, foo: 'baz', bar: 'baz' }, | ||
]); | ||
['groupBy', 'countBy', 'sortBy', 'indexBy'].forEach(function (method) { | ||
t.doesNotThrow(function () { | ||
collection[method]('foo'); | ||
}, method + ' should be callable'); | ||
}); | ||
t.end(); | ||
}); | ||
test('`where` and `findWhere` methods should filter a collection based on a given attributes', function (t) { | ||
@@ -84,3 +108,3 @@ var collection = new Collection([ | ||
var foo = collection.pluck('foo'); | ||
t.deepEqual(foo, ['baz', 'qux']), 'verify that returned attribute values are correct'; | ||
t.deepEqual(foo, ['baz', 'qux'], 'verify that returned attribute values are correct'); | ||
@@ -92,1 +116,46 @@ var bar = collection.pluck('bar'); | ||
}); | ||
test('`first` method should return first model', function (t) { | ||
var collection = new Collection([ | ||
{ id: 1, foo: 'baz', bar: 'qux' }, | ||
{ id: 2, foo: 'qux', bar: 'baz' } | ||
]); | ||
var first = collection.first().toJSON(); | ||
t.deepEqual(first, { id: 1, foo: 'baz', bar: 'qux' }, 'verify that returned attribute values are correct'); | ||
collection = new Collection(); | ||
t.strictEqual(collection.first(), undefined, 'verify that returned attribute values are correct'); | ||
t.end(); | ||
}); | ||
test('`last` method should return last model', function (t) { | ||
var collection = new Collection([ | ||
{ id: 1, foo: 'baz', bar: 'qux' }, | ||
{ id: 2, foo: 'qux', bar: 'baz' } | ||
]); | ||
var last = collection.last().toJSON(); | ||
t.deepEqual(last, { id: 2, foo: 'qux', bar: 'baz' }, 'verify that returned attribute values are correct'); | ||
collection = new Collection(); | ||
t.strictEqual(collection.last(), undefined, 'verify that returned attribute values are correct'); | ||
t.end(); | ||
}); | ||
test('`size` method should return size of collection', function (t) { | ||
var collection = new Collection([ | ||
{ id: 1, foo: 'baz', bar: 'qux' }, | ||
{ id: 2, foo: 'qux', bar: 'baz' } | ||
]); | ||
var foo = collection.pluck('foo'); | ||
t.strictEqual(collection.size(), 2, 'verify that returned size is correct'); | ||
collection = new Collection(); | ||
t.strictEqual(collection.size(), 0, 'verify that returned size is correct'); | ||
t.end(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15367
10
239
61
31
10
2
+ Addedampersand-version@^1.0.0
+ Addedlodash.countby@^3.1.0
+ Addedlodash.difference@^3.1.0
+ Addedlodash.drop@^3.0.0
+ Addedlodash.every@^3.2.0
+ Addedlodash.filter@^3.1.0
+ Addedlodash.find@^3.2.0
+ Addedlodash.foreach@^3.0.2
+ Addedlodash.groupby@^3.1.0
+ Addedlodash.includes@^3.1.0
+ Addedlodash.indexby@^3.1.0
+ Addedlodash.indexof@^3.0.2
+ Addedlodash.initial@^3.0.0
+ Addedlodash.invoke@^3.1.0
+ Addedlodash.isempty@^3.0.1
+ Addedlodash.isfunction@^3.0.2
+ Addedlodash.lastindexof@^3.0.2
+ Addedlodash.map@^3.1.0
+ Addedlodash.max@^3.2.0
+ Addedlodash.min@^3.2.0
+ Addedlodash.partition@^3.1.0
+ Addedlodash.reduce@^3.1.0
+ Addedlodash.reduceright@^3.1.0
+ Addedlodash.reject@^3.1.0
+ Addedlodash.rest@^3.0.0
+ Addedlodash.sample@^3.0.0
+ Addedlodash.shuffle@^3.0.0
+ Addedlodash.some@^3.2.0
+ Addedlodash.sortby@^3.1.0
+ Addedlodash.take@^3.0.0
+ Addedlodash.without@^3.1.0
+ Addedampersand-version@1.0.2(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedfind-root@0.1.2(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedlodash._arraycopy@3.0.0(transitive)
+ Addedlodash._arrayeach@3.0.0(transitive)
+ Addedlodash._arrayevery@3.0.0(transitive)
+ Addedlodash._arrayfilter@3.0.0(transitive)
+ Addedlodash._arraymap@3.0.0(transitive)
+ Addedlodash._basecallback@3.3.1(transitive)
+ Addedlodash._basecompareascending@3.0.2(transitive)
+ Addedlodash._basedifference@3.0.3(transitive)
+ Addedlodash._baseeach@3.0.4(transitive)
+ Addedlodash._baseeachright@3.0.3(transitive)
+ Addedlodash._basefilter@3.0.0(transitive)
+ Addedlodash._basefind@3.0.0(transitive)
+ Addedlodash._basefindindex@3.6.0(transitive)
+ Addedlodash._baseflatten@3.1.4(transitive)
+ Addedlodash._baseforright@3.0.2(transitive)
+ Addedlodash._baseget@3.7.2(transitive)
+ Addedlodash._baseindexof@3.1.0(transitive)
+ Addedlodash._baseisequal@3.0.7(transitive)
+ Addedlodash._baserandom@3.0.1(transitive)
+ Addedlodash._basereduce@3.0.2(transitive)
+ Addedlodash._baseslice@3.0.3(transitive)
+ Addedlodash._basesortby@3.0.0(transitive)
+ Addedlodash._basevalues@3.0.0(transitive)
+ Addedlodash._binaryindex@3.0.1(transitive)
+ Addedlodash._binaryindexby@3.0.3(transitive)
+ Addedlodash._bindcallback@3.0.1(transitive)
+ Addedlodash._cacheindexof@3.0.2(transitive)
+ Addedlodash._createaggregator@3.0.0(transitive)
+ Addedlodash._createcache@3.1.2(transitive)
+ Addedlodash._getnative@3.9.1(transitive)
+ Addedlodash._invokepath@3.7.2(transitive)
+ Addedlodash._isiterateecall@3.0.9(transitive)
+ Addedlodash._toiterable@3.0.4(transitive)
+ Addedlodash._topath@3.8.1(transitive)
+ Addedlodash.countby@3.1.1(transitive)
+ Addedlodash.difference@3.2.2(transitive)
+ Addedlodash.drop@3.0.0(transitive)
+ Addedlodash.every@3.2.3(transitive)
+ Addedlodash.filter@3.1.1(transitive)
+ Addedlodash.find@3.2.1(transitive)
+ Addedlodash.foreach@3.0.3(transitive)
+ Addedlodash.groupby@3.1.1(transitive)
+ Addedlodash.gt@3.9.2(transitive)
+ Addedlodash.includes@3.1.3(transitive)
+ Addedlodash.indexby@3.1.1(transitive)
+ Addedlodash.indexof@3.0.3(transitive)
+ Addedlodash.initial@3.0.0(transitive)
+ Addedlodash.invoke@3.2.3(transitive)
+ Addedlodash.isarguments@3.1.0(transitive)
+ Addedlodash.isarray@3.0.4(transitive)
+ Addedlodash.isempty@3.0.4(transitive)
+ Addedlodash.isfunction@3.0.9(transitive)
+ Addedlodash.isstring@3.0.1(transitive)
+ Addedlodash.istypedarray@3.0.6(transitive)
+ Addedlodash.keys@3.1.2(transitive)
+ Addedlodash.lastindexof@3.0.2(transitive)
+ Addedlodash.lt@3.9.2(transitive)
+ Addedlodash.map@3.1.4(transitive)
+ Addedlodash.max@3.4.0(transitive)
+ Addedlodash.min@3.4.0(transitive)
+ Addedlodash.pairs@3.0.1(transitive)
+ Addedlodash.partition@3.1.1(transitive)
+ Addedlodash.reduce@3.1.2(transitive)
+ Addedlodash.reduceright@3.1.2(transitive)
+ Addedlodash.reject@3.1.1(transitive)
+ Addedlodash.rest@3.0.0(transitive)
+ Addedlodash.restparam@3.6.1(transitive)
+ Addedlodash.sample@3.1.0(transitive)
+ Addedlodash.shuffle@3.1.0(transitive)
+ Addedlodash.some@3.2.3(transitive)
+ Addedlodash.sortby@3.1.5(transitive)
+ Addedlodash.take@3.0.0(transitive)
+ Addedlodash.toarray@3.0.2(transitive)
+ Addedlodash.values@3.0.0(transitive)
+ Addedlodash.without@3.2.1(transitive)
+ Addedreadable-stream@1.0.34(transitive)
+ Addedstring_decoder@0.10.31(transitive)
+ Addedthrough2@0.6.5(transitive)
+ Addedxtend@4.0.2(transitive)
- Removedlodash@^2.4.1
- Removedlodash@2.4.2(transitive)