Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ampersand-collection-lodash-mixin

Package Overview
Dependencies
Maintainers
3
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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

Comparing version 1.0.2 to 2.0.0

.travis.yml

70

ampersand-collection-lodash-mixin.js

@@ -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",

32

README.md
# 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();
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc