Skip to content

Commit 21b43c3

Browse files
committed
Merge branch 'develop' of https://github.com/variadicjs/variadic.js into develop
2 parents 9fb057c + a338497 commit 21b43c3

21 files changed

+572
-126
lines changed

.babelrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"presets": [
3+
"babel-preset-env"
4+
],
5+
"env": {
6+
"production": {
7+
"presets": [
8+
"minify"
9+
]
10+
}
11+
}
12+
}

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"extends": ["airbnb-standard"],
2+
"extends": ["airbnb-base"],
33
"rules": {
44
"strict": 0,
55
"no-restricted-syntax": [

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.js text eol=lf

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ typings/
6363
# coverall token
6464
.coveralls.yml
6565

66+
dist
6667
.DS_Store
6768
untitled.sublime-project
6869
untitled.sublime-workspace

.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!dist

index.js

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

lib/average.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
const { sum } = require('./sum');
22
const { floatPrecise } = require('./floatPrecise');
33

4-
exports.average = (...params) => {
5-
handleErrors(params);
6-
7-
const result = sum(...params) / params.length;
8-
9-
// I couldn't actually find any examples of float imprecision
10-
// when dividing a float by a whole number
11-
// so this might not be neccessary. I just put it in for safety's sake - jmbothe
12-
return floatPrecise(result);
13-
};
14-
154
const handleErrors = (params) => {
165
if (params.length === 0) throw new Error('Must provide one or more paramters');
176
if (params.some(param => typeof param !== 'number')) {
@@ -23,3 +12,15 @@ const handleErrors = (params) => {
2312
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');
2413
}
2514
};
15+
16+
exports.average = (...params) => {
17+
handleErrors(params);
18+
19+
const result = sum(...params) / params.length;
20+
21+
// I couldn't actually find any examples of float imprecision
22+
// when dividing a float by a whole number
23+
// so this might not be neccessary. I just put it in for safety's sake - jmbothe
24+
return floatPrecise(result);
25+
};
26+

lib/isDecimal.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
exports.isDecimal = (...params) => {
3+
for (const param of params) {
4+
// Only accept floating point numbers, infinite numbers can be allowed ie. repeating decimal
5+
// A decimal is a number with digits 0-9, and a decimal point, applies to negative numbers also
6+
// without a decimal point a number can be represented as a decimal, but wont use the case here
7+
if (Number.isNaN(parseFloat(param)) || !/\d/g.test(param) || !/\./.test(param)) return false;
8+
}
9+
return true;
10+
};

lib/isPositiveInteger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ exports.isPositiveInteger = (...params) => {
88
// Is it positive?
99
param <= 0 ||
1010
// maximum safe integer check
11-
/// / https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
11+
// / / https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER
1212
param > Number.MAX_SAFE_INTEGER) {
1313
return false;
1414
}

lib/isPrime.js

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,3 @@
1-
exports.isPrime = (...params) => {
2-
if (params.length === 0) throw new Error('Must provide one or more paramters');
3-
if (params.some(param => Number.isNaN(Number(param)))) throw new Error('One of your parameters does not evaluate to a number');
4-
// JS can only safely represent and compare integers
5-
// up to Number.MAX_SAFE_INTEGER: i.e., 9,007,199,254,740,991.
6-
// This also covers checking if any of the parameters evaluates to Infinity
7-
if (params.some(param => param > Number.MAX_SAFE_INTEGER)) {
8-
throw new Error('Cannot reliably test primality of numbers larger than 9,007,199,254,740,991');
9-
}
10-
return params.every(testPrime);
11-
};
12-
// This method tests the primality of a number by trial division.
13-
// In other words, it attempts to divide the test number by a series integers.
14-
// If any of those integers turn out to be factors of the test number,
15-
// then we know the test number is composite: i.e., not prime.
16-
// the most brute force method is to simply divide the test number by every integer
17-
// smaller than itself, but we can use our knowledge of number properties to greatly
18-
// reduce the list of potential factors that we need to test against.
19-
const testPrime = (num) => {
20-
// num % 1 checks that num is an integer.
21-
// There are no prime numbers less than 2, hence check for num < 2.
22-
if (num % 1 || num < 2) return false;
23-
// if the least factor of num is itself (aside from 1), then num is prime.
24-
if (num === leastFactor(num)) return true;
25-
return false;
26-
};
27-
281
const leastFactor = (num) => {
292
// Check if 2, 3, or 5 is a factor of num AND
303
// eliminate from future consideration all possible factors that are multiples of 2, 3, and 5.
@@ -46,3 +19,31 @@ const leastFactor = (num) => {
4619
}
4720
return num;
4821
};
22+
23+
// This method tests the primality of a number by trial division.
24+
// In other words, it attempts to divide the test number by a series integers.
25+
// If any of those integers turn out to be factors of the test number,
26+
// then we know the test number is composite: i.e., not prime.
27+
// the most brute force method is to simply divide the test number by every integer
28+
// smaller than itself, but we can use our knowledge of number properties to greatly
29+
// reduce the list of potential factors that we need to test against.
30+
const testPrime = (num) => {
31+
// num % 1 checks that num is an integer.
32+
// There are no prime numbers less than 2, hence check for num < 2.
33+
if (num % 1 || num < 2) return false;
34+
// if the least factor of num is itself (aside from 1), then num is prime.
35+
if (num === leastFactor(num)) return true;
36+
return false;
37+
};
38+
39+
exports.isPrime = (...params) => {
40+
if (params.length === 0) throw new Error('Must provide one or more paramters');
41+
if (params.some(param => Number.isNaN(Number(param)))) throw new Error('One of your parameters does not evaluate to a number');
42+
// JS can only safely represent and compare integers
43+
// up to Number.MAX_SAFE_INTEGER: i.e., 9,007,199,254,740,991.
44+
// This also covers checking if any of the parameters evaluates to Infinity
45+
if (params.some(param => param > Number.MAX_SAFE_INTEGER)) {
46+
throw new Error('Cannot reliably test primality of numbers larger than 9,007,199,254,740,991');
47+
}
48+
return params.every(testPrime);
49+
};

lib/median.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
const { average } = require('./average');
22

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 between
9+
// 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+
315
exports.median = (...params) => {
416
handleErrors(params);
517

@@ -8,21 +20,9 @@ exports.median = (...params) => {
820

921
// if length of list is even, take the average of the middle two numbers
1022
if (n % 2 === 0) {
11-
return average(sorted[n / 2 - 1], sorted[n / 2]);
23+
return average(sorted[(n / 2) - 1], sorted[n / 2]);
1224
}
1325

1426
// else just return the middle number
1527
return sorted[Math.floor(n / 2)];
1628
};
17-
18-
const handleErrors = (params) => {
19-
if (params.length === 0) throw new Error('Must provide one or more paramters');
20-
if (params.some(param => typeof param !== 'number')) {
21-
throw new Error('One of your parameters does not evaluate to a number');
22-
}
23-
// JS can only safely represent and compare integers between
24-
// Number.MAX_SAFE_INTEGER and Number.MAX_SAFE_INTEGER
25-
if (params.some(param => param > Number.MAX_SAFE_INTEGER || param < Number.MIN_SAFE_INTEGER)) {
26-
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');
27-
}
28-
};

lib/mode.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1+
const handleErrors = (params) => {
2+
if (params.length === 0) throw new Error('Must provide one or more paramters');
3+
if (params.some(param => typeof param !== 'number')) {
4+
throw new Error('One of your parameters does not evaluate to a number');
5+
}
6+
// JS can only safely represent and compare integers between
7+
// Number.MAX_SAFE_INTEGER and Number.MAX_SAFE_INTEGER
8+
if (params.some(param => param > Number.MAX_SAFE_INTEGER || param < Number.MIN_SAFE_INTEGER)) {
9+
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');
10+
}
11+
};
12+
113
exports.mode = (...params) => {
214
handleErrors(params);
315

416
// create a list of the values and frequencies of params
517
const valFreq = params.reduce((acc, cur) => {
618
const num = acc.find(obj => obj.value === cur);
7-
num ? num.frequency += 1 : acc.push({ value: cur, frequency: 1 });
19+
if (num) {
20+
num.frequency += 1;
21+
} else {
22+
acc.push({ value: cur, frequency: 1 });
23+
}
824
return acc;
925
}, []);
1026

@@ -25,14 +41,3 @@ exports.mode = (...params) => {
2541
.map(num => num.value);
2642
};
2743

28-
const handleErrors = (params) => {
29-
if (params.length === 0) throw new Error('Must provide one or more paramters');
30-
if (params.some(param => typeof param !== 'number')) {
31-
throw new Error('One of your parameters does not evaluate to a number');
32-
}
33-
// JS can only safely represent and compare integers between
34-
// Number.MAX_SAFE_INTEGER and Number.MAX_SAFE_INTEGER
35-
if (params.some(param => param > Number.MAX_SAFE_INTEGER || param < Number.MIN_SAFE_INTEGER)) {
36-
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');
37-
}
38-
};

lib/populationStandardDeviation.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
const { populationVariance } = require('./populationVariance');
22
const { floatPrecise } = require('./floatPrecise');
33

4-
exports.populationStandardDeviation = (...params) => {
5-
handleErrors(params);
6-
7-
return floatPrecise(Math.sqrt(populationVariance(...params)));
8-
};
9-
104
const handleErrors = (params) => {
115
if (params.length <= 1) throw new Error('Must provide two or more paramters');
126
if (params.some(param => typeof param !== 'number')) {
@@ -18,3 +12,10 @@ const handleErrors = (params) => {
1812
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');
1913
}
2014
};
15+
16+
exports.populationStandardDeviation = (...params) => {
17+
handleErrors(params);
18+
19+
return floatPrecise(Math.sqrt(populationVariance(...params)));
20+
};
21+

lib/populationVariance.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
const { average } = require('./average');
22
const { floatPrecise } = require('./floatPrecise');
33

4-
exports.populationVariance = (...params) => {
5-
handleErrors(params);
6-
7-
const mean = average(...params);
8-
// const sum = params.reduce((acc, v) => acc + ((v - mean) ** 2), 0);
9-
const sum = params.reduce((acc, v) => acc + Math.pow(v - mean, 2), 0);
10-
11-
return floatPrecise(sum / params.length);
12-
};
13-
144
const handleErrors = (params) => {
155
if (params.length <= 1) throw new Error('Must provide two or more paramters');
166
if (params.some(param => typeof param !== 'number')) {
@@ -22,3 +12,14 @@ const handleErrors = (params) => {
2212
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');
2313
}
2414
};
15+
16+
exports.populationVariance = (...params) => {
17+
handleErrors(params);
18+
19+
const mean = average(...params);
20+
// const sum = params.reduce((acc, v) => acc + ((v - mean) ** 2), 0);
21+
const sum = params.reduce((acc, v) => acc + Math.pow(v - mean, 2), 0);
22+
23+
return floatPrecise(sum / params.length);
24+
};
25+

lib/sampleStandardDeviation.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
const { sampleVariance } = require('./sampleVariance');
22
const { floatPrecise } = require('./floatPrecise');
33

4-
exports.sampleStandardDeviation = (...params) => {
5-
handleErrors(params);
6-
7-
return floatPrecise(Math.sqrt(sampleVariance(...params)));
8-
};
9-
104
const handleErrors = (params) => {
115
if (params.length <= 1) throw new Error('Must provide two or more paramters');
126
if (params.some(param => typeof param !== 'number')) {
@@ -18,3 +12,9 @@ const handleErrors = (params) => {
1812
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');
1913
}
2014
};
15+
16+
exports.sampleStandardDeviation = (...params) => {
17+
handleErrors(params);
18+
19+
return floatPrecise(Math.sqrt(sampleVariance(...params)));
20+
};

lib/sampleVariance.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
const { average } = require('./average');
22
const { floatPrecise } = require('./floatPrecise');
33

4-
exports.sampleVariance = (...params) => {
5-
handleErrors(params);
6-
7-
const mean = average(...params);
8-
// const sum = params.reduce((acc, v) => acc + ((v - mean) ** 2), 0);
9-
const sum = params.reduce((acc, v) => acc + Math.pow(v - mean, 2), 0);
10-
11-
return floatPrecise(sum / (params.length - 1));
12-
};
13-
144
const handleErrors = (params) => {
155
if (params.length <= 1) throw new Error('Must provide two or more paramters');
166
if (params.some(param => typeof param !== 'number')) {
@@ -22,3 +12,14 @@ const handleErrors = (params) => {
2212
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');
2313
}
2414
};
15+
16+
exports.sampleVariance = (...params) => {
17+
handleErrors(params);
18+
19+
const mean = average(...params);
20+
// const sum = params.reduce((acc, v) => acc + ((v - mean) ** 2), 0);
21+
const sum = params.reduce((acc, v) => acc + Math.pow(v - mean, 2), 0);
22+
23+
return floatPrecise(sum / (params.length - 1));
24+
};
25+

lib/sum.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
const { floatPrecise } = require('./floatPrecise');
22

3-
exports.sum = (...params) => {
4-
handleErrors(params);
5-
6-
const result = params.reduce((prev, cur) => prev + cur, 0);
7-
8-
return floatPrecise(result);
9-
};
10-
113
const handleErrors = (params) => {
124
if (params.length === 0) throw new Error('Must provide one or more paramters');
135
if (params.some(param => typeof param !== 'number')) {
@@ -19,3 +11,11 @@ const handleErrors = (params) => {
1911
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');
2012
}
2113
};
14+
15+
exports.sum = (...params) => {
16+
handleErrors(params);
17+
18+
const result = params.reduce((prev, cur) => prev + cur, 0);
19+
20+
return floatPrecise(result);
21+
};

0 commit comments

Comments
 (0)