Skip to content

Commit b8a6ac9

Browse files
authored
Merge pull request #47 from khusbuchandra/develop
Adding isPositiveInteger function
2 parents 00e7f91 + 1b1b4f3 commit b8a6ac9

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ module.exports = Object.assign(
2020
require('./lib/populationStandardDeviation.js'),
2121
require('./lib/populationVariance.js'),
2222
require('./lib/sampleStandardDeviation.js'),
23-
require('./lib/sampleVariance.js')
23+
require('./lib/sampleVariance.js'),
24+
require('./lib/isPositiveInteger.js')
2425
);

lib/isPositiveInteger.js

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

spec/isPositiveIntegerSpec.js

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

0 commit comments

Comments
 (0)