mirror of
https://github.com/josdejong/mathjs.git
synced 2026-01-18 14:59:29 +00:00
Dropped support for string and undefined from logical operators
This commit is contained in:
parent
73b67bfc09
commit
33be634ffa
@ -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));
|
||||
};
|
||||
};
|
||||
|
||||
@ -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));
|
||||
};
|
||||
};
|
||||
|
||||
@ -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));
|
||||
};
|
||||
};
|
||||
|
||||
@ -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));
|
||||
};
|
||||
};
|
||||
|
||||
@ -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 () {
|
||||
|
||||
@ -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 () {
|
||||
|
||||
@ -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 () {
|
||||
|
||||
@ -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 () {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user