-
-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
}); | ||
}; |