Skip to content

Commit

Permalink
fix: correctly count undefined and null args
Browse files Browse the repository at this point in the history
upgrade dev dependencies
  • Loading branch information
JaKXz committed Aug 15, 2019
1 parent 608b591 commit 3c91abc
Show file tree
Hide file tree
Showing 4 changed files with 773 additions and 672 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ module.exports = function xor(...args) {
return false;
}

const truthyCount = args.reduce((result, val) => result + !!val);
const truthyCount = args.reduce((result, val) => result + !!val, 0);
return truthyCount === 1;
};
8 changes: 8 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
const xor = require('.');

test('not enough arguments', () => {
expect(xor()).toBe(false);
expect(xor(undefined)).toBe(false);
expect(xor(null)).toBe(false);
expect(xor(true)).toBe(false);
expect(xor(false)).toBe(false);
});

test('2 args', () => {
expect(xor(undefined, undefined)).toBe(false);
expect(xor(undefined, true)).toBe(true);
expect(xor(null, true)).toBe(true);
expect(xor(true, false)).toBe(true);
expect(xor(false, true)).toBe(true);
expect(xor(true, true)).toBe(false);
Expand All @@ -17,5 +23,7 @@ test('many arguments', () => {
expect(xor(true, true, true)).toBe(false);
expect(xor(true, true, false)).toBe(false);
expect(xor(false, false, false)).toBe(false);
expect(xor(undefined, true, false, false)).toBe(true);
expect(xor(undefined, null, false, false)).toBe(false);
expect(xor(false, false, false, false, false, true)).toBe(true);
});
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@
"node": ">= 4.9"
},
"scripts": {
"precommit": "pretty-quick --staged",
"test": "jest"
},
"devDependencies": {
"husky": "^0.14.3",
"jest": "^24.7.1",
"prettier": "^1.17.0",
"pretty-quick": "^1.10.0"
"husky": "^3.0.3",
"jest": "^24.8.0",
"prettier": "^1.18.2",
"pretty-quick": "^1.11.1"
},
"prettier": {
"singleQuote": true
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
}
}

0 comments on commit 3c91abc

Please sign in to comment.