Skip to content

Commit

Permalink
feat: add a diffUsing method
Browse files Browse the repository at this point in the history
  • Loading branch information
zpwparsons committed Mar 20, 2023
1 parent 84ad447 commit 6c3ba5d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions dist/methods/diffUsing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

module.exports = function diffUsing(values, callback) {
var collection = this.items.filter(function (item) {
return !(values && values.some(function (otherItem) {
return callback(item, otherItem) === 0;
}));
});
return new this.constructor(collection);
};
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Collection.prototype.dd = require('./methods/dd');
Collection.prototype.diff = require('./methods/diff');
Collection.prototype.diffAssoc = require('./methods/diffAssoc');
Collection.prototype.diffKeys = require('./methods/diffKeys');
Collection.prototype.diffUsing = require('./methods/diffUsing');
Collection.prototype.doesntContain = require('./methods/doesntContain');
Collection.prototype.dump = require('./methods/dump');
Collection.prototype.duplicates = require('./methods/duplicates');
Expand Down
9 changes: 9 additions & 0 deletions src/methods/diffUsing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function diffUsing(values, callback) {
const collection = this.items.filter(item => (
!(values && values.some(otherItem => callback(item, otherItem) === 0))
));

return new this.constructor(collection);
};
38 changes: 38 additions & 0 deletions test/methods/diffUsing_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

module.exports = (it, expect, collect) => {
it('should compare the collection against a javascript array using a callback', () => {
const collection = collect(['fr', 'en_gb', 'hr']);
const diffArray = ['en_gb', 'hr'];

const diff = collection.diffUsing(diffArray, (a, b) => a.localeCompare(b));

expect(diff.all()).to.eql(['fr']);
expect(collection.all()).to.eql(['fr', 'en_gb', 'hr']);
});

it('should compare the collection against another collection using on a callback', () => {
const collection = collect([
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 },
]);

const diffCollection = collect([
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 },
{ name: 'David', age: 40 },
]);

const diff = collection.diffUsing(diffCollection, (a, b) => a.age - b.age);

expect(diff.all()).to.be.eql([{ name: 'Alice', age: 25 }]);
});

it('does not return a difference when the comparison is null', () => {
const collection = collect(['apple', 'banana', 'cherry']);
const diff = collection.diffUsing(null, (a, b) => a.localeCompare(b));

expect(diff.all()).to.eql(['apple', 'banana', 'cherry']);
});
};

0 comments on commit 6c3ba5d

Please sign in to comment.