@util.js/numbers 0.41.5 → 0.41.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -1
- package/lib/Numbers.js +0 -266
- package/test/NumbersTest.js +0 -112
package/package.json
CHANGED
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
"work in https://npm.runkit.com."
|
|
9
9
|
],
|
|
10
10
|
"description": "JavaScript utility methods for numbers",
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"index.js"
|
|
14
|
+
],
|
|
11
15
|
"keywords": [
|
|
12
16
|
"int",
|
|
13
17
|
"integer",
|
|
@@ -32,5 +36,5 @@
|
|
|
32
36
|
"type": "git",
|
|
33
37
|
"url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-numbers"
|
|
34
38
|
},
|
|
35
|
-
"version": "0.41.
|
|
39
|
+
"version": "0.41.7"
|
|
36
40
|
}
|
package/lib/Numbers.js
DELETED
|
@@ -1,266 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
class Numbers {
|
|
4
|
-
abs() {
|
|
5
|
-
return Math.abs(...arguments);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
acos() {
|
|
9
|
-
return Math.acos(...arguments);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
acosh() {
|
|
13
|
-
return Math.acosh(...arguments);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
ascendingComparator() {
|
|
17
|
-
const thiz = this;
|
|
18
|
-
return (a, b) => {
|
|
19
|
-
if (thiz.isNaN(a)) {
|
|
20
|
-
if (thiz.isNaN(b)) return 0;
|
|
21
|
-
else return 1;
|
|
22
|
-
} else if (thiz.isNaN(b)) {
|
|
23
|
-
return -1;
|
|
24
|
-
}
|
|
25
|
-
return a - b;
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
asin() {
|
|
30
|
-
return Math.asin(...arguments);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
asinh() {
|
|
34
|
-
return Math.asinh(...arguments);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
atan() {
|
|
38
|
-
return Math.atan(...arguments);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
atan2() {
|
|
42
|
-
return Math.atan2(...arguments);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
atanh() {
|
|
46
|
-
return Math.atanh(...arguments);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
cbrt() {
|
|
50
|
-
return Math.cbrt(...arguments);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
ceil() {
|
|
54
|
-
return Math.ceil(...arguments);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
clz32() {
|
|
58
|
-
return Math.clz32(...arguments);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
cos() {
|
|
62
|
-
return Math.cos(...arguments);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
cosh() {
|
|
66
|
-
return Math.cosh(...arguments);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
descendingComparator() {
|
|
70
|
-
const thiz = this;
|
|
71
|
-
return (a, b) => {
|
|
72
|
-
if (thiz.isNaN(a)) {
|
|
73
|
-
if (thiz.isNaN(b)) return 0;
|
|
74
|
-
else return -1;
|
|
75
|
-
} else if (thiz.isNaN(b)) {
|
|
76
|
-
return 1;
|
|
77
|
-
}
|
|
78
|
-
return b - a;
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
exp() {
|
|
83
|
-
return Math.exp(...arguments);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
expm1() {
|
|
87
|
-
return Math.expm1(...arguments);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
floor() {
|
|
91
|
-
return Math.floor(...arguments);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
fround() {
|
|
95
|
-
return Math.fround(...arguments);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
hypot() {
|
|
99
|
-
return Math.hypot(...arguments);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
imul() {
|
|
103
|
-
return Math.imul(...arguments);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
isFinite() {
|
|
107
|
-
return Number.isFinite(...arguments);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// http://stackoverflow.com/questions/14636536/how-to-check-if-a-variable-is-an-integer-in-javascript
|
|
111
|
-
isInt(value) {
|
|
112
|
-
if (this.isNaN(value)) return false;
|
|
113
|
-
const x = this.parseFloat(value);
|
|
114
|
-
return (x | 0) === x;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
isInteger() {
|
|
118
|
-
return Number.isInteger(...arguments);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
isNaN() {
|
|
122
|
-
return Number.isNaN(...arguments);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
isSafeInteger() {
|
|
126
|
-
return Number.isSafeInteger(...arguments);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
log() {
|
|
130
|
-
return Math.log(...arguments);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
log10() {
|
|
134
|
-
return Math.log10(...arguments);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
log1p() {
|
|
138
|
-
return Math.log1p(...arguments);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
log2() {
|
|
142
|
-
return Math.log2(...arguments);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
max() {
|
|
146
|
-
return Math.max(...arguments);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
min() {
|
|
150
|
-
return Math.min(...arguments);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
parseFloat() {
|
|
154
|
-
return Number.parseFloat(...arguments);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
parseInt() {
|
|
158
|
-
return Number.parseInt(...arguments);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
pow() {
|
|
162
|
-
return Math.pow(...arguments);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
random() {
|
|
166
|
-
return Math.random(...arguments);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
round() {
|
|
170
|
-
return Math.round(...arguments);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
sign() {
|
|
174
|
-
return Math.sign(...arguments);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
sin() {
|
|
178
|
-
return Math.sin(...arguments);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
sinh() {
|
|
182
|
-
return Math.sinh(...arguments);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
sqrt() {
|
|
186
|
-
return Math.sqrt(...arguments);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
tan() {
|
|
190
|
-
return Math.tan(...arguments);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
tanh() {
|
|
194
|
-
return Math.tanh(...arguments);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
trunc() {
|
|
198
|
-
return Math.trunc(...arguments);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
get E() {
|
|
202
|
-
return Math.E;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
get EPSILON() {
|
|
206
|
-
return Number.EPSILON;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
get LN10() {
|
|
210
|
-
return Math.LN10;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
get LN2() {
|
|
214
|
-
return Math.LN2;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
get LOG10E() {
|
|
218
|
-
return Math.LOG10E;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
get LOG2E() {
|
|
222
|
-
return Math.LOG2E;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
get MAX_SAFE_INTEGER() {
|
|
226
|
-
return Number.MAX_SAFE_INTEGER;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
get MAX_VALUE() {
|
|
230
|
-
return Number.MAX_VALUE;
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
get MIN_SAFE_INTEGER() {
|
|
234
|
-
return Number.MIN_SAFE_INTEGER;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
get MIN_VALUE() {
|
|
238
|
-
return Number.MIN_VALUE;
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
get NEGATIVE_INFINITY() {
|
|
242
|
-
return Number.NEGATIVE_INFINITY;
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
get NaN() {
|
|
246
|
-
return Number.NaN;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
get PI() {
|
|
250
|
-
return Math.PI;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
get POSITIVE_INFINITY() {
|
|
254
|
-
return Number.POSITIVE_INFINITY;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
get SQRT1_2() {
|
|
258
|
-
return Math.SQRT1_2;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
get SQRT2() {
|
|
262
|
-
return Math.SQRT2;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
module.exports = Numbers;
|
package/test/NumbersTest.js
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
const { expect } = require("chai");
|
|
4
|
-
const numbers = require("..");
|
|
5
|
-
|
|
6
|
-
describe("Numbers#abs(x)", () => {
|
|
7
|
-
it("should calculate correctly", () => {
|
|
8
|
-
expect(numbers.abs(numbers.NEGATIVE_INFINITY)).to.eql(
|
|
9
|
-
numbers.POSITIVE_INFINITY,
|
|
10
|
-
);
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
describe("Numbers#cos(x)", () => {
|
|
15
|
-
it("should calculate correctly", () => {
|
|
16
|
-
expect(numbers.cos(numbers.PI)).to.eql(-1);
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe("Numbers", () => {
|
|
21
|
-
describe("#ascendingComparator", () => {
|
|
22
|
-
it("should order arrays of numbers in ascending order", () => {
|
|
23
|
-
const expected = [
|
|
24
|
-
numbers.NEGATIVE_INFINITY,
|
|
25
|
-
-33,
|
|
26
|
-
0,
|
|
27
|
-
33.33,
|
|
28
|
-
numbers.POSITIVE_INFINITY,
|
|
29
|
-
numbers.NaN,
|
|
30
|
-
numbers.NaN,
|
|
31
|
-
];
|
|
32
|
-
const actual = [
|
|
33
|
-
numbers.POSITIVE_INFINITY,
|
|
34
|
-
0,
|
|
35
|
-
numbers.NaN,
|
|
36
|
-
numbers.NEGATIVE_INFINITY,
|
|
37
|
-
-33,
|
|
38
|
-
numbers.NaN,
|
|
39
|
-
33.33,
|
|
40
|
-
].sort(numbers.ascendingComparator());
|
|
41
|
-
expect(actual).to.deep.equal(expected);
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
describe("#descendingComparator", () => {
|
|
46
|
-
it("should order arrays of numbers in descending order", () => {
|
|
47
|
-
const expected = [
|
|
48
|
-
numbers.NaN,
|
|
49
|
-
numbers.NaN,
|
|
50
|
-
numbers.NaN,
|
|
51
|
-
numbers.POSITIVE_INFINITY,
|
|
52
|
-
33.33,
|
|
53
|
-
0,
|
|
54
|
-
-33,
|
|
55
|
-
numbers.NEGATIVE_INFINITY,
|
|
56
|
-
];
|
|
57
|
-
const actual = [
|
|
58
|
-
numbers.NaN,
|
|
59
|
-
numbers.POSITIVE_INFINITY,
|
|
60
|
-
0,
|
|
61
|
-
numbers.NaN,
|
|
62
|
-
numbers.NEGATIVE_INFINITY,
|
|
63
|
-
-33,
|
|
64
|
-
numbers.NaN,
|
|
65
|
-
33.33,
|
|
66
|
-
].sort(numbers.descendingComparator());
|
|
67
|
-
expect(actual).to.deep.equal(expected);
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
describe("#isInt(value)", () => {
|
|
72
|
-
it("should return true for integers", () => {
|
|
73
|
-
expect(numbers.isInt(-33)).to.be.true;
|
|
74
|
-
expect(numbers.isInt(0)).to.be.true;
|
|
75
|
-
expect(numbers.isInt(5)).to.be.true;
|
|
76
|
-
});
|
|
77
|
-
it("should return true for integers as strings", () => {
|
|
78
|
-
expect(numbers.isInt("-33")).to.be.true;
|
|
79
|
-
expect(numbers.isInt("0")).to.be.true;
|
|
80
|
-
expect(numbers.isInt("5")).to.be.true;
|
|
81
|
-
});
|
|
82
|
-
it("should return true for objects whose #toString evaluates to an integer", () => {
|
|
83
|
-
expect(numbers.isInt([5])).to.be.true;
|
|
84
|
-
expect(numbers.isInt([5, 6])).to.be.true;
|
|
85
|
-
expect(numbers.isInt({ toString: () => "5" })).to.be.true;
|
|
86
|
-
});
|
|
87
|
-
it("should return false for numbers with a nonzero fractional part", () => {
|
|
88
|
-
expect(numbers.isInt(-33.3)).to.be.false;
|
|
89
|
-
expect(numbers.isInt(0.1)).to.be.false;
|
|
90
|
-
expect(numbers.isInt(1e-5)).to.be.false;
|
|
91
|
-
});
|
|
92
|
-
it("should return false for numbers (as strings) with a nonzero fractional part", () => {
|
|
93
|
-
expect(numbers.isInt("")).to.be.false;
|
|
94
|
-
expect(numbers.isInt("-33.3")).to.be.false;
|
|
95
|
-
expect(numbers.isInt("0.1")).to.be.false;
|
|
96
|
-
expect(numbers.isInt("1e-5")).to.be.false;
|
|
97
|
-
});
|
|
98
|
-
it("should return false for objects whose #toString does not evaluate to an integer", () => {
|
|
99
|
-
expect(numbers.isInt([])).to.be.false;
|
|
100
|
-
expect(numbers.isInt({})).to.be.false;
|
|
101
|
-
expect(numbers.isInt([5.5, 5])).to.be.false;
|
|
102
|
-
expect(numbers.isInt({ toString: () => "5.5" })).to.be.false;
|
|
103
|
-
});
|
|
104
|
-
it("should return false for undefined, null, NaN, or infinite input", () => {
|
|
105
|
-
expect(numbers.isInt()).to.be.false;
|
|
106
|
-
expect(numbers.isInt(null)).to.be.false;
|
|
107
|
-
expect(numbers.isInt(NaN)).to.be.false;
|
|
108
|
-
expect(numbers.isInt(Infinity)).to.be.false;
|
|
109
|
-
expect(numbers.isInt(Number.NEGATIVE_INFINITY)).to.be.false;
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
});
|