Skip to content

Commit

Permalink
Merge branch 'pr/316' into feature/diffUsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrmnn committed Mar 29, 2023
2 parents 2e822c9 + e01a2d4 commit 2b3dd64
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 1 deletion.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ All available methods
- [diff](#diff)
- [diffAssoc](#diffassoc)
- [diffKeys](#diffkeys)
- [diffUsing](#diffusing)
- [doesntContain](#doesntcontain)
- [dump](#dump)
- [duplicates](#duplicates)
Expand Down Expand Up @@ -553,6 +554,30 @@ diff.all();
// { a: 'a', c: 'c' }
```

#### `diffUsing()`

The diffUsing method compares the collection against another collection or a plain array based on its values using a callback. This method will return the values in the original collection that are not present in the given collection:

```js
const collection = collect([
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 },
]);

const users = [
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 },
{ name: 'David', age: 40 },
];

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

diff.all();

// [{ name: 'Alice', age: 25 }]
```

#### `doesntContain()`

The `doesntContain` method determines whether the collection does not contain a given item. You may pass a closure to the `doesntContain` method to determine if an element does not exist in the collection matching a given truth test:
Expand Down Expand Up @@ -3330,4 +3355,4 @@ PRs are welcomed to this project, and help is needed in order to keep up with th

### License

MIT 漏 [Daniel Eckermann](https://danieleckermann.com)
MIT 漏 [Daniel Eckermann](https://danieleckermann.com)
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);
};
25 changes: 25 additions & 0 deletions docs/api/diffUsing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# `diffUsing()`

The diffUsing method compares the collection against another collection or a plain array based on its values using a callback. This method will return the values in the original collection that are not present in the given collection:

```js
const collection = collect([
{ name: 'Alice', age: 25 },
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 },
]);

const users = [
{ name: 'Bob', age: 30 },
{ name: 'Charlie', age: 35 },
{ name: 'David', age: 40 },
];

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

diff.all();

// [{ name: 'Alice', age: 25 }]
```

[View source on GitHub](https://github.com/ecrmnn/collect.js/blob/master/src/methods/diffUsing.js)
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 2b3dd64

Please sign in to comment.