Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1955395

Browse files
Merge pull request #58 from variadicjs/minimum-function
Create minimum function and spec file
2 parents a338497 + eb7b2e4 commit 1955395

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
@@ -16,6 +16,7 @@ module.exports = Object.assign(
1616
require('./lib/isDescending.js'),
1717
require('./lib/isPrime.js'),
1818
require('./lib/median.js'),
19+
require('./lib/minimum.js'),
1920
require('./lib/mode.js'),
2021
require('./lib/populationStandardDeviation.js'),
2122
require('./lib/populationVariance.js'),

‎lib/minimum.js‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const { floatPrecise } = require('./floatPrecise');
2+
3+
const handleErrors = (params) => {
4+
if (params.length === 0) throw new Error('Must provide one or more paramters');
5+
if (params.some(param => typeof param !== 'number')) {
6+
throw new Error('One of your parameters does not evaluate to a number');
7+
}
8+
// JS can only safely represent and compare integers
9+
// between Number.MAX_SAFE_INTEGER and Number.MAX_SAFE_INTEGER
10+
if (params.some(param => param > Number.MAX_SAFE_INTEGER || param < Number.MIN_SAFE_INTEGER)) {
11+
throw new Error('Cannot reliably test primality of numbers larger than 9,007,199,254,740,991 or smaller than -9,007,199,254,740,991');
12+
}
13+
};
14+
15+
exports.minimum = (...params) => {
16+
handleErrors(params);
17+
18+
const result = params.reduce((prev, cur) => (cur < prev ? cur : prev));
19+
20+
return floatPrecise(result);
21+
};

‎spec/minimumSpec.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { minimum } = require('../lib/minimum');
2+
3+
describe('minimum', () => {
4+
it('should return the minimum of all parameters, given that all parameters evaluate to numbers', () => {
5+
const result = minimum(1, 5, -7, 15, 10);
6+
expect(result).toBe(-7);
7+
});
8+
9+
it('should throw an error when no parameters are provided', () => {
10+
expect(minimum).toThrow();
11+
});
12+
13+
it('should throw an error when at least one parameter does not evaluate to a number', () => {
14+
expect(() => minimum('yo')).toThrow();
15+
});
16+
17+
it('should throw an error when at least one parameter evaluates to a number larger than Number.MAX_SAFE_INTEGER', () => {
18+
expect(() => minimum(Number.MAX_SAFE_INTEGER + 1)).toThrow();
19+
});
20+
21+
it('should throw an error when at least one parameter evaluates to a number smaller than Number.MIN_SAFE_INTEGER', () => {
22+
expect(() => minimum(Number.MIN_SAFE_INTEGER - 1)).toThrow();
23+
});
24+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /