From 33be634ffadbb99ecb0b56256bd2dfaf62528cef Mon Sep 17 00:00:00 2001 From: jos Date: Thu, 25 Dec 2014 14:49:43 +0100 Subject: [PATCH] Dropped support for string and undefined from logical operators --- lib/function/logical/and.js | 13 ++++++++++--- lib/function/logical/not.js | 10 ++++++++-- lib/function/logical/or.js | 13 ++++++++++--- lib/function/logical/xor.js | 13 ++++++++++--- test/function/logical/and.test.js | 27 ++++----------------------- test/function/logical/not.test.js | 19 +++++-------------- test/function/logical/or.test.js | 31 ++++--------------------------- test/function/logical/xor.test.js | 30 ++++-------------------------- 8 files changed, 55 insertions(+), 101 deletions(-) diff --git a/lib/function/logical/and.js b/lib/function/logical/and.js index df11370dd..56da1820f 100644 --- a/lib/function/logical/and.js +++ b/lib/function/logical/and.js @@ -8,6 +8,8 @@ module.exports = function (math) { Unit = require('../../type/Unit'), collection = require('../../type/collection'), + isNumber = util.number.isNumber, + isBoolean = util['boolean'].isBoolean, isComplex = Complex.isComplex, isUnit = Unit.isUnit, isCollection = collection.isCollection; @@ -35,8 +37,8 @@ module.exports = function (math) { * * not, or, xor * - * @param {Number | BigNumber | Boolean | Complex | Unit | String | Array | Matrix | null | undefined} x First value to check - * @param {Number | BigNumber | Boolean | Complex | Unit | String | Array | Matrix | null | undefined} y Second value to check + * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null} x First value to check + * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null} y Second value to check * @return {Boolean | Matrix} * Returns true when both inputs are defined with a nonzero/nonempty value. */ @@ -45,6 +47,11 @@ module.exports = function (math) { throw new math.error.ArgumentsError('and', arguments.length, 2); } + if ((isNumber(x) || isBoolean(x) || x === null) && + (isNumber(y) || isBoolean(y) || y === null)) { + return !!(x && y); + } + if (isComplex(x)) { if (x.re == 0 && x.im == 0) { return false; @@ -94,6 +101,6 @@ module.exports = function (math) { return collection.deepMap2(x, y, and); } - return !!(x && y); + throw new math.error.UnsupportedTypeError('and', math['typeof'](x), math['typeof'](y)); }; }; diff --git a/lib/function/logical/not.js b/lib/function/logical/not.js index cb24f9617..94cf80e79 100644 --- a/lib/function/logical/not.js +++ b/lib/function/logical/not.js @@ -8,6 +8,8 @@ module.exports = function (math) { Unit = require('../../type/Unit'), collection = require('../../type/collection'), + isNumber = util.number.isNumber, + isBoolean = util['boolean'].isBoolean, isComplex = Complex.isComplex, isUnit = Unit.isUnit, isCollection = collection.isCollection; @@ -33,7 +35,7 @@ module.exports = function (math) { * * and, or, xor * - * @param {Number | BigNumber | Boolean | Complex | Unit | String | Array | Matrix | null | undefined} x First value to check + * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null} x First value to check * @return {Boolean | Matrix} * Returns true when input is a zero or empty value. */ @@ -42,6 +44,10 @@ module.exports = function (math) { throw new math.error.ArgumentsError('not', arguments.length, 1); } + if (isNumber(x) || isBoolean(x) || x === null) { + return !x; + } + if (isComplex(x)) { return x.re == 0 && x.im == 0; } @@ -58,6 +64,6 @@ module.exports = function (math) { return collection.deepMap(x, not); } - return !x; + throw new math.error.UnsupportedTypeError('not', math['typeof'](x)); }; }; diff --git a/lib/function/logical/or.js b/lib/function/logical/or.js index d7ea44d2c..6e025347a 100644 --- a/lib/function/logical/or.js +++ b/lib/function/logical/or.js @@ -8,6 +8,8 @@ module.exports = function (math) { Unit = require('../../type/Unit'), collection = require('../../type/collection'), + isNumber = util.number.isNumber, + isBoolean = util['boolean'].isBoolean, isComplex = Complex.isComplex, isUnit = Unit.isUnit, isCollection = collection.isCollection; @@ -35,8 +37,8 @@ module.exports = function (math) { * * and, not, xor * - * @param {Number | BigNumber | Boolean | Complex | Unit | String | Array | Matrix | null | undefined} x First value to check - * @param {Number | BigNumber | Boolean | Complex | Unit | String | Array | Matrix | null | undefined} y Second value to check + * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null} x First value to check + * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null} y Second value to check * @return {Boolean | Matrix} * Returns true when one of the inputs is defined with a nonzero/nonempty value. */ @@ -45,6 +47,11 @@ module.exports = function (math) { throw new math.error.ArgumentsError('or', arguments.length, 2); } + if ((isNumber(x) || isBoolean(x) || x === null) && + (isNumber(y) || isBoolean(y) || y === null)) { + return !!(x || y); + } + if (isComplex(x)) { if (x.re == 0 && x.im == 0) { return or(false, y); @@ -88,6 +95,6 @@ module.exports = function (math) { return collection.deepMap2(x, y, or); } - return !!(x || y); + throw new math.error.UnsupportedTypeError('or', math['typeof'](x), math['typeof'](y)); }; }; diff --git a/lib/function/logical/xor.js b/lib/function/logical/xor.js index 9d651d703..4ce9b6da8 100644 --- a/lib/function/logical/xor.js +++ b/lib/function/logical/xor.js @@ -8,6 +8,8 @@ module.exports = function (math) { Unit = require('../../type/Unit'), collection = require('../../type/collection'), + isNumber = util.number.isNumber, + isBoolean = util['boolean'].isBoolean, isComplex = Complex.isComplex, isUnit = Unit.isUnit, isCollection = collection.isCollection; @@ -35,8 +37,8 @@ module.exports = function (math) { * * and, not, or * - * @param {Number | BigNumber | Boolean | Complex | Unit | String | Array | Matrix | null | undefined} x First value to check - * @param {Number | BigNumber | Boolean | Complex | Unit | String | Array | Matrix | null | undefined} y Second value to check + * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null} x First value to check + * @param {Number | BigNumber | Boolean | Complex | Unit | Array | Matrix | null} y Second value to check * @return {Boolean | Matrix} * Returns true when one and only one input is defined with a nonzero/nonempty value. */ @@ -45,6 +47,11 @@ module.exports = function (math) { throw new math.error.ArgumentsError('xor', arguments.length, 2); } + if ((isNumber(x) || isBoolean(x) || x === null) && + (isNumber(y) || isBoolean(y) || y === null)) { + return !!(!!x ^ !!y); + } + if (isComplex(x)) { return xor(!(x.re == 0 && x.im == 0), y); } @@ -70,6 +77,6 @@ module.exports = function (math) { return collection.deepMap2(x, y, xor); } - return !!(!!x ^ !!y); + throw new math.error.UnsupportedTypeError('xor', math['typeof'](x), math['typeof'](y)); }; }; diff --git a/test/function/logical/and.test.js b/test/function/logical/and.test.js index bab8a196f..44c758d0c 100644 --- a/test/function/logical/and.test.js +++ b/test/function/logical/and.test.js @@ -74,11 +74,6 @@ describe('and', function () { assert.equal(and(null, 2), false); }); - it('should and mixed numbers and undefined', function () { - assert.equal(and(2, undefined), false); - assert.equal(and(undefined, 2), false); - }); - it('should and bignumbers', function () { assert.equal(and(bignumber(1), bignumber(1)), true); assert.equal(and(bignumber(-1), bignumber(1)), true); @@ -122,22 +117,6 @@ describe('and', function () { assert.equal(and(unit('2in'), 0), false); }); - it('should and two strings', function () { - assert.equal(and('0', 'NaN'), true); - - assert.equal(and('abd', ' '), true); - assert.equal(and('abc', ''), false); - assert.equal(and('', 'abd'), false); - assert.equal(and('', ''), false); - }); - - it('should and mixed numbers and strings', function () { - assert.equal(and(1, 'NaN'), true); - assert.equal(and('abd', 1), true); - assert.equal(and(1, ''), false); - assert.equal(and('', 1), false); - }); - it('should and two arrays', function () { assert.deepEqual(and([0, 1, 0, 12], [0, 0, 1, 22]), [false, false, false, true]); assert.deepEqual(and([], []), []); @@ -158,8 +137,10 @@ describe('and', function () { assert.deepEqual(and(matrix([0, 2]), 10), matrix([false, true])); }); - it('should and two objects', function () { - assert.equal(and(new Date(), new Date()), true); + it('should throw an error in case of invalid type if arguments', function () { + assert.throws(function () {and(new Date(), new Date())}, TypeError); + assert.throws(function () {and(2, '23')}, TypeError); + assert.throws(function () {and(2, undefined)}, TypeError); }); it('should throw an error in case of invalid number of arguments', function () { diff --git a/test/function/logical/not.test.js b/test/function/logical/not.test.js index 01c003dd1..61150146a 100644 --- a/test/function/logical/not.test.js +++ b/test/function/logical/not.test.js @@ -37,9 +37,8 @@ describe('not', function () { assert.equal(not(false), true); }); - it('should not null/undefined values', function () { + it('should not null', function () { assert.equal(not(null), true); - assert.equal(not(undefined), true); }); it('should not bignumbers', function () { @@ -62,16 +61,6 @@ describe('not', function () { assert.equal(not(unit('-10inch')), false); }); - it('should not strings', function () { - assert.equal(not('0'), false); - assert.equal(not('NaN'), false); - - assert.equal(not('abd'), false); - assert.equal(not(''), true); - assert.equal(not('\0'), false); - assert.equal(not(' '), false); - }); - it('should not arrays', function () { assert.deepEqual(not([0, 10]), [true, false]); assert.deepEqual(not([]), []); @@ -82,8 +71,10 @@ describe('not', function () { assert.deepEqual(not(matrix([])), matrix([])); }); - it('should not object', function () { - assert.equal(not(new Date()), false); + it('should throw an error in case of invalid type if arguments', function () { + assert.throws(function () {not(new Date())}, TypeError); + assert.throws(function () {not('23')}, TypeError); + assert.throws(function () {not({})}, TypeError); }); it('should throw an error in case of invalid number of arguments', function () { diff --git a/test/function/logical/or.test.js b/test/function/logical/or.test.js index 1d40ddd52..b3abc5651 100644 --- a/test/function/logical/or.test.js +++ b/test/function/logical/or.test.js @@ -85,12 +85,6 @@ describe('or', function () { assert.equal(or(null, null), false); }); - it('should or mixed numbers and undefined', function () { - assert.equal(or(2, undefined), true); - assert.equal(or(undefined, 2), true); - assert.equal(or(undefined, undefined), false); - }); - it('should or bignumbers', function () { assert.equal(or(bignumber(1), bignumber(1)), true); assert.equal(or(bignumber(-1), bignumber(1)), true); @@ -143,25 +137,6 @@ describe('or', function () { assert.equal(or(unit('0in'), 0), false); }); - it('should or two strings', function () { - assert.equal(or('0', 'NaN'), true); - - assert.equal(or('abd', ' '), true); - assert.equal(or('abc', ''), true); - assert.equal(or('', 'abd'), true); - assert.equal(or('', ''), false); - assert.equal(or(' ', ''), true); - }); - - it('should or mixed numbers and strings', function () { - assert.equal(or(1, 'NaN'), true); - assert.equal(or('abd', 1), true); - assert.equal(or(1, ''), true); - assert.equal(or(0, ''), false); - assert.equal(or('', 1), true); - assert.equal(or('', 0), false); - }); - it('should or two arrays', function () { assert.deepEqual(or([0, 1, 0, 12], [0, 0, 1, 22]), [false, true, true, true]); assert.deepEqual(or([], []), []); @@ -186,8 +161,10 @@ describe('or', function () { assert.deepEqual(or(matrix([0, 2]), 0), matrix([false, true])); }); - it('should or two objects', function () { - assert.equal(or(new Date(), new Date()), true); + it('should throw an error in case of invalid type if arguments', function () { + assert.throws(function () {or(new Date(), new Date())}, TypeError); + assert.throws(function () {or(2, '23')}, TypeError); + assert.throws(function () {or(2, undefined)}, TypeError); }); it('should throw an error in case of invalid number of arguments', function () { diff --git a/test/function/logical/xor.test.js b/test/function/logical/xor.test.js index c27a0fd4a..e008356fb 100644 --- a/test/function/logical/xor.test.js +++ b/test/function/logical/xor.test.js @@ -83,12 +83,6 @@ describe('xor', function () { assert.equal(xor(null, 2), true); }); - it('should xor mixed numbers and undefined', function () { - assert.equal(xor(2, undefined), true); - assert.equal(xor(undefined, 2), true); - assert.equal(xor(null, null), false); - }); - it('should xor bignumbers', function () { assert.equal(xor(bignumber(1), bignumber(1)), false); assert.equal(xor(bignumber(-1), bignumber(1)), false); @@ -139,24 +133,6 @@ describe('xor', function () { assert.equal(xor(unit('0in'), 0), false); }); - it('should xor two strings', function () { - assert.equal(xor('0', 'NaN'), false); - - assert.equal(xor('abd', ' '), false); - assert.equal(xor('abc', ''), true); - assert.equal(xor('', 'abd'), true); - assert.equal(xor('', ''), false); - }); - - it('should xor mixed numbers and strings', function () { - assert.equal(xor(1, 'NaN'), false); - assert.equal(xor('abd', 1), false); - assert.equal(xor(1, ''), true); - assert.equal(xor('', 1), true); - assert.equal(xor('', 0), false); - assert.equal(xor(0, ''), false); - }); - it('should xor two arrays', function () { assert.deepEqual(xor([0, 1, 0, 12], [0, 0, 1, 22]), [false, true, true, false]); assert.deepEqual(xor([], []), []); @@ -181,8 +157,10 @@ describe('xor', function () { assert.deepEqual(xor(matrix([0, 2]), 0), matrix([false, true])); }); - it('should xor two objects', function () { - assert.equal(xor(new Date(), new Date()), false); + it('should throw an error in case of invalid type if arguments', function () { + assert.throws(function () {xor(new Date(), new Date())}, TypeError); + assert.throws(function () {xor(2, '23')}, TypeError); + assert.throws(function () {xor(2, undefined)}, TypeError); }); it('should throw an error in case of invalid number of arguments', function () {