-
-
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
1 parent
84ad447
commit 6c3ba5d
Showing
4 changed files
with
58 additions
and
0 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
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
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,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']); | ||
}); | ||
}; |