Skip to content

Commit ba49c93

Browse files
authored
Merge pull request #60 from khusbuchandra/develop
Issue #45 : Add isNegativeInteger function
2 parents 31783c6 + 881dfb9 commit ba49c93

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ module.exports = Object.assign(
2525
require('./lib/sampleVariance.js'),
2626
require('./lib/isPositiveInteger.js'),
2727
require('./lib/isDecimal.js'),
28+
require('./lib/isNegativeInteger.js'),
2829
);

lib/isNegativeInteger.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
exports.isNegativeInteger = (...params) => {
2+
for (const param of params) {
3+
// Checks the type to be Number
4+
if (Object.prototype.toString.call(param) !== '[object Number]' ||
5+
// Is it an integer?
6+
param % 1 !== 0 ||
7+
// Is it positive?
8+
param >= 0 ||
9+
// maximum safe integer check
10+
// / / https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
11+
param < Number.MIN_SAFE_INTEGER) {
12+
return false;
13+
}
14+
}
15+
return true;
16+
};

spec/isNegativeIntegerSpec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const { isNegativeInteger } = require('../lib/isNegativeInteger');
2+
3+
const MAX_SAFE_INTEGER = 9007199254740991;
4+
describe('IsNegativeInteger', () => {
5+
it('should return false for positive integers or 0', () => {
6+
expect(isNegativeInteger(2, 4, 200)).toBe(false);
7+
expect(isNegativeInteger(MAX_SAFE_INTEGER, 4, 200)).toBe(false);
8+
expect(isNegativeInteger(0, 98, 225, 56)).toBe(false);
9+
});
10+
11+
it('should return true for negative numbers', () => {
12+
expect(isNegativeInteger(-3, -4, -200)).toBe(true);
13+
expect(isNegativeInteger(-34, -98, -225, -56)).toBe(true);
14+
});
15+
16+
it('should return false for floats', () => {
17+
expect(isNegativeInteger(2.6, 4.9, 23.67)).toBe(false);
18+
expect(isNegativeInteger(2, 4, 23.67)).toBe(false);
19+
expect(isNegativeInteger(-10.2, -4.6, -200.4)).toBe(false);
20+
expect(isNegativeInteger(-32.7, 98, 225, 56)).toBe(false);
21+
});
22+
23+
it('should return false for other values', () => {
24+
expect(isNegativeInteger({})).toBe(false);
25+
expect(isNegativeInteger(2, [])).toBe(false);
26+
expect(isNegativeInteger(1, 'S')).toBe(false);
27+
expect(isNegativeInteger(MAX_SAFE_INTEGER + 1)).toBe(false);
28+
});
29+
});

0 commit comments

Comments
 (0)