Skip to content

Commit d3d5d26

Browse files
committed
add isComposit
1 parent f3fb483 commit d3d5d26

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = Object.assign(
1717
require('./lib/isAscending.js'),
1818
require('./lib/isDescending.js'),
1919
require('./lib/isPrime.js'),
20+
require('./lib/isComposit.js'),
2021
require('./lib/maximum.js'),
2122
require('./lib/median.js'),
2223
require('./lib/minimum.js'),

lib/isComposit.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { isPrime } = require('./isPrime');
2+
3+
4+
// `isPrime` checks if the number is an integer greater than 1
5+
// but since we simply want `isPrime` to check if this number
6+
// is prime, we need to do the check again to avoid a false result.
7+
const testComposit = num => (num % 1 || num < 2 ? false : !isPrime(num));
8+
9+
/**
10+
* This function evaluates whether all numerical parameters are composit
11+
* @memberof variadic
12+
* @author tdnelson2
13+
* @param {...*} params - One or more parameters.
14+
*/
15+
exports.isComposit = (...params) => {
16+
if (params.length === 0) throw new Error('Must provide one or more paramters');
17+
return params.every(testComposit);
18+
};

spec/isCompositSpec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const { isComposit } = require('../lib/isComposit');
2+
3+
describe('IsComposit', () => {
4+
it('should return true when all parameters evaluate to composit numbers', () => {
5+
const result = isComposit(4, 6, 20 / 2, +'8');
6+
expect(result).toBe(true);
7+
});
8+
9+
it('should return false when at least one parameter does not evaluate to a composit number', () => {
10+
const result = isComposit(2, 8, 10, 28);
11+
expect(result).toBe(false);
12+
});
13+
14+
it('should return false when at least one parameter does not evaluate to an integer', () => {
15+
const result = isComposit(2.5, 8, 10, 20);
16+
expect(result).toBe(false);
17+
});
18+
19+
it('should return false when at least one parameter evaluates to a number less than 2', () => {
20+
const result = isComposit(-2, 8, 10, 20);
21+
expect(result).toBe(false);
22+
});
23+
24+
it('should return false when the least factor of a number is not composit', () => {
25+
const result = isComposit(15485863);
26+
expect(result).toBe(false);
27+
});
28+
29+
it('should throw an error when no parameters are provided', () => {
30+
expect(isComposit).toThrow();
31+
});
32+
33+
it('should throw an error when at least one parameter does not evaluate to a number', () => {
34+
expect(() => isComposit('yo')).toThrow();
35+
});
36+
37+
it('should throw an error when at least one parameter evaluates to a number larger than Number.MAX_SAFE_INTEGER', () => {
38+
expect(() => isComposit(9949370777987917)).toThrow();
39+
});
40+
});

0 commit comments

Comments
 (0)