Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lib): Diff of array should consider both sides #46

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/lib/array-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ function difference(a, b) {
var hash = {}
var diff = {}

b.forEach(function createHash(item) {
b.forEach(createHash(hash))
a.forEach(findDiff(hash, diff))

// return the other way round
hash = {}
a.forEach(createHash(hash))
b.forEach(findDiff(hash, diff))

return Object.keys(diff)

}

function createHash(hash) {
return function curried(item) {
hash[item] = true
})
}
}

a.forEach(function findDiff(item) {
function findDiff(hash, diff) {
return function curried(item) {
if (!hash[item] && !diff[item]) {
diff[item] = true
}
})

return Object.keys(diff)

}
}

module.exports = difference
7 changes: 3 additions & 4 deletions test/lib/array-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ describe('difference', function() {
it('should return difference', function() {
assert.deepEqual(
difference(['a', 'b', 'c'], ['x', 'y', 'z']),
['a', 'b', 'c']
['a', 'b', 'c', 'x', 'y', 'z']
)
assert.deepEqual(
difference(['a', 'b', 'c'], ['a', 'y', 'z']),
['b', 'c']
['b', 'c', 'y', 'z']
)
assert.deepEqual(
difference(['a', 'b', 'c'], ['a', 'b', 'z']),
['c']
['c', 'z']
)

assert.deepEqual(
Expand All @@ -22,4 +22,3 @@ describe('difference', function() {
)
})
})