From 881dfb91d5953703fe120806e5cfeff132cd3af5 Mon Sep 17 00:00:00 2001 From: khusbuchandra Date: Thu, 8 Feb 2018 23:27:56 -0800 Subject: [PATCH] Add isNegativeInteger function Add isNegativeInteger function returns false postive non-integer numbers or 0 --- index.js | 1 + lib/isNegativeInteger.js | 16 ++++++++++++++++ spec/isNegativeIntegerSpec.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 lib/isNegativeInteger.js create mode 100644 spec/isNegativeIntegerSpec.js diff --git a/index.js b/index.js index ee12970..6d40d32 100644 --- a/index.js +++ b/index.js @@ -25,4 +25,5 @@ module.exports = Object.assign( require('./lib/sampleVariance.js'), require('./lib/isPositiveInteger.js'), require('./lib/isDecimal.js'), + require('./lib/isNegativeInteger.js'), ); diff --git a/lib/isNegativeInteger.js b/lib/isNegativeInteger.js new file mode 100644 index 0000000..b52966c --- /dev/null +++ b/lib/isNegativeInteger.js @@ -0,0 +1,16 @@ +exports.isNegativeInteger = (...params) => { + for (const param of params) { + // Checks the type to be Number + if (Object.prototype.toString.call(param) !== '[object Number]' || + // Is it an integer? + param % 1 !== 0 || + // Is it positive? + param >= 0 || + // maximum safe integer check + // / / https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER + param < Number.MIN_SAFE_INTEGER) { + return false; + } + } + return true; +}; diff --git a/spec/isNegativeIntegerSpec.js b/spec/isNegativeIntegerSpec.js new file mode 100644 index 0000000..c35ef4d --- /dev/null +++ b/spec/isNegativeIntegerSpec.js @@ -0,0 +1,29 @@ +const { isNegativeInteger } = require('../lib/isNegativeInteger'); + +const MAX_SAFE_INTEGER = 9007199254740991; +describe('IsNegativeInteger', () => { + it('should return false for positive integers or 0', () => { + expect(isNegativeInteger(2, 4, 200)).toBe(false); + expect(isNegativeInteger(MAX_SAFE_INTEGER, 4, 200)).toBe(false); + expect(isNegativeInteger(0, 98, 225, 56)).toBe(false); + }); + + it('should return true for negative numbers', () => { + expect(isNegativeInteger(-3, -4, -200)).toBe(true); + expect(isNegativeInteger(-34, -98, -225, -56)).toBe(true); + }); + + it('should return false for floats', () => { + expect(isNegativeInteger(2.6, 4.9, 23.67)).toBe(false); + expect(isNegativeInteger(2, 4, 23.67)).toBe(false); + expect(isNegativeInteger(-10.2, -4.6, -200.4)).toBe(false); + expect(isNegativeInteger(-32.7, 98, 225, 56)).toBe(false); + }); + + it('should return false for other values', () => { + expect(isNegativeInteger({})).toBe(false); + expect(isNegativeInteger(2, [])).toBe(false); + expect(isNegativeInteger(1, 'S')).toBe(false); + expect(isNegativeInteger(MAX_SAFE_INTEGER + 1)).toBe(false); + }); +});