Skip to content

Commit 8fb6142

Browse files
committed
Adding isPositiveInteger function
Added isPositiveInteger function to check for positive integers not more than max safe integer. Added spec for the same.
1 parent b0d6478 commit 8fb6142

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
exports.isPositiveInteger = (...params) => {
3+
for (const param of params) {
4+
// Checks the type to be Number
5+
if( params.some(param => typeof param !== '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+
{
14+
return false;
15+
}
16+
}
17+
return true;
18+
};

spec/isPositiveIntegerSpec.js

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

0 commit comments

Comments
 (0)