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+
};

0 commit comments

Comments
 (0)