Skip to content

Commit

Permalink
Merge branch 'release/4.35.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ecrmnn committed Mar 29, 2023
2 parents 84ad447 + 34169cf commit 7706950
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 3 deletions.
26 changes: 26 additions & 0 deletions 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,31 @@ 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
14 changes: 13 additions & 1 deletion build/collect.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/collect.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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
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)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "collect.js",
"version": "4.34.3",
"version": "4.35.0",
"description": "Convenient and dependency free wrapper for working with arrays and objects.",
"main": "dist/index.js",
"typings": "index.d.ts",
Expand Down
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);
};
46 changes: 46 additions & 0 deletions test/methods/diffUsing_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'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 with null', () => {
const collection = collect(['fr', 'en_gb', 'hr']);

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

expect(diff.all()).to.eql(collection.all());
});

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 7706950

Please sign in to comment.