numeric-utils 2.1.0 → 2.1.1
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/dist/mod.js +2 -659
- package/package.json +13 -4
- package/dist/mod.d.ts +0 -564
package/dist/mod.js
CHANGED
|
@@ -1,659 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* @author [ZakaHaceCosas](https://github.com/ZakaHaceCosas/)
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```ts
|
|
7
|
-
* import { average } from "@zakahacecosas/number-utils";
|
|
8
|
-
*
|
|
9
|
-
* const grades = [5, 7, 4, 9, 5, 6.1];
|
|
10
|
-
* const averageGrade = average(grades); // get the avg
|
|
11
|
-
* return `You scored an average of ${averageGrade}`;
|
|
12
|
-
* ```
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```ts
|
|
16
|
-
* import { roundTo, min, max } from "@zakahacecosas/number-utils";
|
|
17
|
-
*
|
|
18
|
-
* function analyzeWages(wages: number[]) {
|
|
19
|
-
* const avgWage = roundTo(average(avgWage), 2);
|
|
20
|
-
* const highestWage = max(avgWage);
|
|
21
|
-
* const lowestWage = min(avgWage);
|
|
22
|
-
*
|
|
23
|
-
* console.log(`Average wage: ${avgWage}€`);
|
|
24
|
-
* console.log(`Highest wage: ${highestWage}€`);
|
|
25
|
-
* console.log(`Lowest wage: ${lowestWage}€`);
|
|
26
|
-
* }
|
|
27
|
-
*
|
|
28
|
-
* const wages = [1800, 2200, 2500, 1900, 2750];
|
|
29
|
-
* analyzeWages(wages);
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* @module
|
|
33
|
-
*/
|
|
34
|
-
// * SECTION: MODULE * //
|
|
35
|
-
/**
|
|
36
|
-
* Converts degrees to radians.
|
|
37
|
-
*
|
|
38
|
-
* @param {number} deg Degrees.
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```ts
|
|
42
|
-
* degreesToRadians(180); // 3.141592653589793
|
|
43
|
-
* ```
|
|
44
|
-
*
|
|
45
|
-
* @returns {number} Radians.
|
|
46
|
-
* @available Also served from [geo-utils](https://jsr.io/@zakahacecosas/geo-utils).
|
|
47
|
-
*/
|
|
48
|
-
export function degreesToRadians(deg) {
|
|
49
|
-
return (deg * Math.PI) / 180;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Converts radians to degrees.
|
|
53
|
-
*
|
|
54
|
-
* @param {number} rad Radians.
|
|
55
|
-
*
|
|
56
|
-
* @example
|
|
57
|
-
* ```ts
|
|
58
|
-
* radiansToDegrees(Math.PI); // 180
|
|
59
|
-
* ```
|
|
60
|
-
*
|
|
61
|
-
* @returns {number} Degrees.
|
|
62
|
-
* @available Also served from [geo-utils](https://jsr.io/@zakahacecosas/geo-utils).
|
|
63
|
-
*/
|
|
64
|
-
export function radiansToDegrees(rad) {
|
|
65
|
-
return (rad * 180) / Math.PI;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Converts kilometers to miles.
|
|
69
|
-
*
|
|
70
|
-
* @param {number} km Kilometers.
|
|
71
|
-
*
|
|
72
|
-
* @example
|
|
73
|
-
* ```ts
|
|
74
|
-
* kilometersToMiles(5); // 3.106855
|
|
75
|
-
* ```
|
|
76
|
-
* @available Also served from [geo-utils](https://jsr.io/@zakahacecosas/geo-utils).
|
|
77
|
-
|
|
78
|
-
* @returns {number} Miles.
|
|
79
|
-
*/
|
|
80
|
-
export function kilometersToMiles(km) {
|
|
81
|
-
return km * 0.621371;
|
|
82
|
-
}
|
|
83
|
-
/**
|
|
84
|
-
* Converts miles to kilometers.
|
|
85
|
-
*
|
|
86
|
-
* @param {number} miles Miles.
|
|
87
|
-
*
|
|
88
|
-
* @example
|
|
89
|
-
* ```ts
|
|
90
|
-
* milesToKilometers(5); // 8.046722
|
|
91
|
-
* ```
|
|
92
|
-
*
|
|
93
|
-
* @returns {number} Degrees.
|
|
94
|
-
* @available Also served from [geo-utils](https://jsr.io/@zakahacecosas/geo-utils).
|
|
95
|
-
*/
|
|
96
|
-
export function milesToKilometers(miles) {
|
|
97
|
-
return miles / 0.621371;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* Converts meters to feet.
|
|
101
|
-
*
|
|
102
|
-
* @param {number} meters Meters.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```ts
|
|
106
|
-
* metersToFeet(5); // 16.4042
|
|
107
|
-
* ```
|
|
108
|
-
*
|
|
109
|
-
* @returns {number} Feet.
|
|
110
|
-
* @available Also served from [geo-utils](https://jsr.io/@zakahacecosas/geo-utils).
|
|
111
|
-
*/
|
|
112
|
-
export function metersToFeet(meters) {
|
|
113
|
-
return meters * 3.28084;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Converts feet to meters.
|
|
117
|
-
*
|
|
118
|
-
* @param {number} feet Feet.
|
|
119
|
-
*
|
|
120
|
-
* @example
|
|
121
|
-
* ```ts
|
|
122
|
-
* feetToMeters(5); // 1.524
|
|
123
|
-
* ```
|
|
124
|
-
*
|
|
125
|
-
* @returns {number} Meters.
|
|
126
|
-
* @available Also served from [geo-utils](https://jsr.io/@zakahacecosas/geo-utils).
|
|
127
|
-
*/
|
|
128
|
-
export function feetToMeters(feet) {
|
|
129
|
-
return feet / 3.28084;
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* _A classic._ Checks if a given number is even or not. Analog to {@linkcode isOdd}.
|
|
133
|
-
*
|
|
134
|
-
* @param {number} num Number to check.
|
|
135
|
-
*
|
|
136
|
-
* @example
|
|
137
|
-
* ```ts
|
|
138
|
-
* isEven(2); // true
|
|
139
|
-
* isEven(3); // false
|
|
140
|
-
* ```
|
|
141
|
-
*
|
|
142
|
-
* @returns {boolean} True if it's even, false if otherwise.
|
|
143
|
-
*/
|
|
144
|
-
export function isEven(num) {
|
|
145
|
-
return num % 2 === 0;
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* _A classic._ Checks if a given number is odd or not. Analog to {@linkcode isEven}.
|
|
149
|
-
*
|
|
150
|
-
* @param {number} num Number to check.
|
|
151
|
-
*
|
|
152
|
-
* @example
|
|
153
|
-
* ```ts
|
|
154
|
-
* isOdd(2); // false
|
|
155
|
-
* isOdd(3); // true
|
|
156
|
-
* ```
|
|
157
|
-
*
|
|
158
|
-
* @returns {boolean} True if it's odd, false if otherwise.
|
|
159
|
-
*/
|
|
160
|
-
export function isOdd(num) {
|
|
161
|
-
return num % 2 !== 0;
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Rounds a given number to a specified decimal precision.
|
|
165
|
-
*
|
|
166
|
-
* @param {number} num Number to round.
|
|
167
|
-
* @param {number} [precision=0] Amount of decimals to include. Defaults to 0.
|
|
168
|
-
*
|
|
169
|
-
* @example
|
|
170
|
-
* ```ts
|
|
171
|
-
* roundTo(3.14159, 2) // 3.14
|
|
172
|
-
* ```
|
|
173
|
-
*
|
|
174
|
-
* @returns {number} The rounded number.
|
|
175
|
-
*/
|
|
176
|
-
export function roundTo(num, precision = 0) {
|
|
177
|
-
return Number(num.toFixed(precision));
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Generates a random integer between min and max (inclusive).
|
|
181
|
-
*
|
|
182
|
-
* @param {number} min Minimum number, inclusive.
|
|
183
|
-
* @param {number} max Maximum number, inclusive.
|
|
184
|
-
*
|
|
185
|
-
* @example
|
|
186
|
-
* ```ts
|
|
187
|
-
* random(1, 10) // returns a random num between 1 and 10, e.g. "7"
|
|
188
|
-
* random(65, 105) // returns a random num between 65 and 105, e.g. "81"
|
|
189
|
-
* ```
|
|
190
|
-
*
|
|
191
|
-
* @returns {number} A random number between `min` and `max`.
|
|
192
|
-
*/
|
|
193
|
-
export function random(min, max) {
|
|
194
|
-
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Checks if a given number is prime.
|
|
198
|
-
*
|
|
199
|
-
* @param {number} num Number to check.
|
|
200
|
-
*
|
|
201
|
-
* @example
|
|
202
|
-
* ```ts
|
|
203
|
-
* isPrime(7) // true
|
|
204
|
-
* isPrime(8) // false
|
|
205
|
-
* ```
|
|
206
|
-
*
|
|
207
|
-
* @returns {boolean} True if the given number is prime, false if otherwise.
|
|
208
|
-
*/
|
|
209
|
-
export function isPrime(num) {
|
|
210
|
-
if (num < 2)
|
|
211
|
-
return false;
|
|
212
|
-
for (let i = 2; i <= Math.sqrt(num); i++) {
|
|
213
|
-
if (num % i === 0)
|
|
214
|
-
return false;
|
|
215
|
-
}
|
|
216
|
-
return true;
|
|
217
|
-
}
|
|
218
|
-
/**
|
|
219
|
-
* Returns the n-th prime number. **Computationally expensive.**
|
|
220
|
-
*
|
|
221
|
-
* @export
|
|
222
|
-
* @param {number} index Index of the prime number, as in a JS array. Index 0 is number 2, for example.
|
|
223
|
-
*
|
|
224
|
-
* @example
|
|
225
|
-
* ```ts
|
|
226
|
-
* primeAt(0); // 2
|
|
227
|
-
* primeAt(3); // 7
|
|
228
|
-
* primeAt(9999999999); // your PC has likely crashed at this point
|
|
229
|
-
* ```
|
|
230
|
-
*
|
|
231
|
-
* @returns {number} The prime number at the given index.
|
|
232
|
-
*/
|
|
233
|
-
export function primeAt(index) {
|
|
234
|
-
if (index < 0)
|
|
235
|
-
throw new Error("Index must be a non-negative integer.");
|
|
236
|
-
const primes = [];
|
|
237
|
-
let c = 0;
|
|
238
|
-
let i = 0;
|
|
239
|
-
while (primes.length <= index) {
|
|
240
|
-
console.log(primes);
|
|
241
|
-
if (isPrime(c)) {
|
|
242
|
-
primes.push(c);
|
|
243
|
-
i++;
|
|
244
|
-
}
|
|
245
|
-
c++;
|
|
246
|
-
}
|
|
247
|
-
return primes[index];
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Returns the sum of all numbers in an array.
|
|
251
|
-
*
|
|
252
|
-
* @param {number[]} arr Array of numbers.
|
|
253
|
-
*
|
|
254
|
-
* @example
|
|
255
|
-
* ```ts
|
|
256
|
-
* sumArray([1, 2, 3]) // 6
|
|
257
|
-
* ```
|
|
258
|
-
*
|
|
259
|
-
* @returns {number} The sum of all numbers in the array.
|
|
260
|
-
*/
|
|
261
|
-
export function sumArray(arr) {
|
|
262
|
-
return arr.reduce((acc, curr) => acc + curr, 0);
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Returns the average of all numbers in an array.
|
|
266
|
-
*
|
|
267
|
-
* @param {number[]} arr Array of numbers.
|
|
268
|
-
*
|
|
269
|
-
* @example
|
|
270
|
-
* ```ts
|
|
271
|
-
* average([1, 2, 3, 4]) // 2.5
|
|
272
|
-
* ```
|
|
273
|
-
*
|
|
274
|
-
* @returns {number} The average of all numbers in the array.
|
|
275
|
-
*/
|
|
276
|
-
export function average(arr) {
|
|
277
|
-
return sumArray(arr) / arr.length;
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Checks if a given number is between two numbers, both of them inclusive.
|
|
281
|
-
*
|
|
282
|
-
* @param {number} num Number to check.
|
|
283
|
-
* @param {number} min Minimum number. Inclusive.
|
|
284
|
-
* @param {number} max Maximum number. Inclusive.
|
|
285
|
-
*
|
|
286
|
-
* @example
|
|
287
|
-
* ```ts
|
|
288
|
-
* isBetween(5, 1, 10); // true
|
|
289
|
-
* isBetween(15, 1, 10); // false
|
|
290
|
-
* ```
|
|
291
|
-
*
|
|
292
|
-
* @returns {boolean} True if the number is between `min` and `max`, false if otherwise.
|
|
293
|
-
*/
|
|
294
|
-
export function isBetween(num, min, max) {
|
|
295
|
-
return num >= min && num <= max;
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Calculates the factorial of a number. For reference, the factorial of `n` (`n!`), is multiplying that number by each number below of it and above zero.
|
|
299
|
-
*
|
|
300
|
-
* @param {number} num Number to calculate the factorial for.
|
|
301
|
-
*
|
|
302
|
-
* @example
|
|
303
|
-
* ```ts
|
|
304
|
-
* factorial(5) // 120, since 5 * 4 * 3 * 2 * 1 = 120
|
|
305
|
-
* ```
|
|
306
|
-
*
|
|
307
|
-
* @returns {number} The factorial of the given number.
|
|
308
|
-
*/
|
|
309
|
-
export function factorial(num) {
|
|
310
|
-
return (num <= 1 ? 1 : num * factorial(num - 1));
|
|
311
|
-
}
|
|
312
|
-
/**
|
|
313
|
-
* Checks if a number is a perfect square. For reference, a perfect square is a number that, when squared, returns an exact value.
|
|
314
|
-
*
|
|
315
|
-
* @param {number} num Number to check for.
|
|
316
|
-
*
|
|
317
|
-
* @example
|
|
318
|
-
* ```ts
|
|
319
|
-
* isPerfectSquare(16); // true
|
|
320
|
-
* isPerfectSquare(18); // false
|
|
321
|
-
* ```
|
|
322
|
-
*
|
|
323
|
-
* @returns {boolean} True if it's a perfect square, false if otherwise.
|
|
324
|
-
*/
|
|
325
|
-
export function isPerfectSquare(num) {
|
|
326
|
-
return Number.isInteger(Math.sqrt(num));
|
|
327
|
-
}
|
|
328
|
-
/**
|
|
329
|
-
* Finds the greatest common divisor (GCD) of two numbers.
|
|
330
|
-
*
|
|
331
|
-
* @param {number} a First number.
|
|
332
|
-
* @param {number} b Second number.
|
|
333
|
-
*
|
|
334
|
-
* @example
|
|
335
|
-
* ```ts
|
|
336
|
-
* gcd(12, 18); // 6.
|
|
337
|
-
* ```
|
|
338
|
-
*
|
|
339
|
-
* @returns {number} The GCD of both numbers.
|
|
340
|
-
*/
|
|
341
|
-
export function gcd(a, b) {
|
|
342
|
-
while (b !== 0) {
|
|
343
|
-
const temp = b;
|
|
344
|
-
b = a % b;
|
|
345
|
-
a = temp;
|
|
346
|
-
}
|
|
347
|
-
return a;
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Finds the least common multiple (LCM) of two numbers.
|
|
351
|
-
*
|
|
352
|
-
* @param {number} a First number.
|
|
353
|
-
* @param {number} b Second number.
|
|
354
|
-
*
|
|
355
|
-
* @example
|
|
356
|
-
* ```ts
|
|
357
|
-
* lcm(12, 18); // 36.
|
|
358
|
-
* ```
|
|
359
|
-
*
|
|
360
|
-
* @returns {number} The LCM of both numbers.
|
|
361
|
-
*/
|
|
362
|
-
export function lcm(a, b) {
|
|
363
|
-
return (a * b) / gcd(a, b);
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* Returns the absolute difference between two numbers.
|
|
367
|
-
*
|
|
368
|
-
* @param {number} a First number.
|
|
369
|
-
* @param {number} b Second number.
|
|
370
|
-
*
|
|
371
|
-
* @example
|
|
372
|
-
* ```ts
|
|
373
|
-
* absDiff(10, 3); // 7
|
|
374
|
-
* ```
|
|
375
|
-
*
|
|
376
|
-
* @returns {number} Their absolute difference.
|
|
377
|
-
*/
|
|
378
|
-
export function absDiff(a, b) {
|
|
379
|
-
return Math.abs(a - b);
|
|
380
|
-
}
|
|
381
|
-
/**
|
|
382
|
-
* _A classic._ Checks if a given number is negative or not. Analog to {@linkcode isPositive}.
|
|
383
|
-
*
|
|
384
|
-
* @param {number} num Number to check.
|
|
385
|
-
*
|
|
386
|
-
* @example
|
|
387
|
-
* ```ts
|
|
388
|
-
* isNegative(-2); // true
|
|
389
|
-
* isNegative(3); // false
|
|
390
|
-
* ```
|
|
391
|
-
*
|
|
392
|
-
* @returns {boolean} True if it's negative, false if otherwise.
|
|
393
|
-
*/
|
|
394
|
-
export function isNegative(num) {
|
|
395
|
-
return num < 0;
|
|
396
|
-
}
|
|
397
|
-
/**
|
|
398
|
-
* _A classic._ Checks if a given number is positive or not. Analog to {@linkcode isNegative}.
|
|
399
|
-
*
|
|
400
|
-
* @param {number} num Number to check.
|
|
401
|
-
*
|
|
402
|
-
* @example
|
|
403
|
-
* ```ts
|
|
404
|
-
* isPositive(-2); // false
|
|
405
|
-
* isPositive(3); // true
|
|
406
|
-
* ```
|
|
407
|
-
*
|
|
408
|
-
* @returns {boolean} True if it's positive, false if otherwise.
|
|
409
|
-
*/
|
|
410
|
-
export function isPositive(num) {
|
|
411
|
-
return num > 0;
|
|
412
|
-
}
|
|
413
|
-
/**
|
|
414
|
-
* Returns the smallest number in an array.
|
|
415
|
-
*
|
|
416
|
-
* @param {number[]} arr Array of numbers.
|
|
417
|
-
*
|
|
418
|
-
* @example
|
|
419
|
-
* ```ts
|
|
420
|
-
* min([4, 2, 7, 1]); // 1
|
|
421
|
-
* ```
|
|
422
|
-
*
|
|
423
|
-
* @returns {number} The smallest number in the array.
|
|
424
|
-
*/
|
|
425
|
-
export function min(arr) {
|
|
426
|
-
return Math.min(...arr);
|
|
427
|
-
}
|
|
428
|
-
/**
|
|
429
|
-
* Returns the highest number in an array.
|
|
430
|
-
*
|
|
431
|
-
* @param {number[]} arr Array of numbers.
|
|
432
|
-
*
|
|
433
|
-
* @example
|
|
434
|
-
* ```ts
|
|
435
|
-
* max([4, 2, 7, 1]); // 7
|
|
436
|
-
* ```
|
|
437
|
-
*
|
|
438
|
-
* @returns {number} The highest number in the array.
|
|
439
|
-
*/
|
|
440
|
-
export function max(arr) {
|
|
441
|
-
return Math.max(...arr);
|
|
442
|
-
}
|
|
443
|
-
/**
|
|
444
|
-
* Turns centimeters (CM) to inches (IN).
|
|
445
|
-
*
|
|
446
|
-
* @param {number} cms Centimeters.
|
|
447
|
-
*
|
|
448
|
-
* @example
|
|
449
|
-
* ```ts
|
|
450
|
-
* centimetersToInches(10); // 3.94
|
|
451
|
-
* ```
|
|
452
|
-
*
|
|
453
|
-
* @returns {number} Inches.
|
|
454
|
-
*/
|
|
455
|
-
export function centimetersToInches(cms) {
|
|
456
|
-
return cms / 2.54;
|
|
457
|
-
}
|
|
458
|
-
/**
|
|
459
|
-
* Turns inches (IN) to centimeters (CM).
|
|
460
|
-
*
|
|
461
|
-
* @param {number} ins Inches.
|
|
462
|
-
*
|
|
463
|
-
* @example
|
|
464
|
-
* ```ts
|
|
465
|
-
* inchesToCentimeters(10); // 25.4
|
|
466
|
-
* ```
|
|
467
|
-
*
|
|
468
|
-
* @returns {number} Inches.
|
|
469
|
-
*/
|
|
470
|
-
export function inchesToCentimeters(ins) {
|
|
471
|
-
return ins * 2.54;
|
|
472
|
-
}
|
|
473
|
-
/**
|
|
474
|
-
* Turns Celsius (ºC) to Fahrenheit (ºF). Analog to {@linkcode fahrenheitToCelsius}.
|
|
475
|
-
*
|
|
476
|
-
* @param {number} celsius Celsius.
|
|
477
|
-
*
|
|
478
|
-
* @example
|
|
479
|
-
* ```ts
|
|
480
|
-
* celsiusToFahrenheit(25); // 77
|
|
481
|
-
* ```
|
|
482
|
-
*
|
|
483
|
-
* @returns {number} Fahrenheit.
|
|
484
|
-
*/
|
|
485
|
-
export function celsiusToFahrenheit(celsius) {
|
|
486
|
-
return ((celsius * 1.8) + 32);
|
|
487
|
-
}
|
|
488
|
-
/**
|
|
489
|
-
* Turns Fahrenheit (ºF) to Celsius (ºC). Analog to {@linkcode celsiusToFahrenheit}.
|
|
490
|
-
*
|
|
491
|
-
* @param {number} fahrenheit Celsius.
|
|
492
|
-
*
|
|
493
|
-
* @example
|
|
494
|
-
* ```ts
|
|
495
|
-
* fahrenheitToCelsius(104); // 40
|
|
496
|
-
* ```
|
|
497
|
-
*
|
|
498
|
-
* @returns {number} Celsius.
|
|
499
|
-
*/
|
|
500
|
-
export function fahrenheitToCelsius(fahrenheit) {
|
|
501
|
-
return ((fahrenheit - 32) * (1 / 1.8));
|
|
502
|
-
}
|
|
503
|
-
/**
|
|
504
|
-
* Turns kilometers per hour (KPH or KM/H) to Miles per hour (MPH). Analog to {@linkcode milesPerHourToKilometersPerHour}.
|
|
505
|
-
*
|
|
506
|
-
* @param {number} kph KPH.
|
|
507
|
-
*
|
|
508
|
-
* @example
|
|
509
|
-
* ```ts
|
|
510
|
-
* kilometersPerHourToMilesPerHour(180); // 111.846815
|
|
511
|
-
* ```
|
|
512
|
-
*
|
|
513
|
-
* @returns {number} MPH.
|
|
514
|
-
*/
|
|
515
|
-
export function kilometersPerHourToMilesPerHour(kph) {
|
|
516
|
-
return kph / 1.609344;
|
|
517
|
-
}
|
|
518
|
-
/**
|
|
519
|
-
* Turns Miles per hour (MPH) to Kilometers per hour (KPH or KM/H). Analog to {@linkcode milesPerHourToKilometersPerHour}.
|
|
520
|
-
*
|
|
521
|
-
* @param {number} mph MPH.
|
|
522
|
-
*
|
|
523
|
-
* @example
|
|
524
|
-
* ```ts
|
|
525
|
-
* milesPerHourToKilometersPerHour(180); // 289.68192
|
|
526
|
-
* ```
|
|
527
|
-
*
|
|
528
|
-
* @returns {number} MPH.
|
|
529
|
-
*/
|
|
530
|
-
export function milesPerHourToKilometersPerHour(mph) {
|
|
531
|
-
return mph * 1.609344;
|
|
532
|
-
}
|
|
533
|
-
/**
|
|
534
|
-
* Calculates the number of combinations of `n` items taken `r` at a time.
|
|
535
|
-
*
|
|
536
|
-
* @param {number} n Total number of items.
|
|
537
|
-
* @param {number} r Number of items to choose.
|
|
538
|
-
*
|
|
539
|
-
* @example
|
|
540
|
-
* ```ts
|
|
541
|
-
* combinations(5, 3); // 10
|
|
542
|
-
* ```
|
|
543
|
-
*
|
|
544
|
-
* @returns {number} The number of combinations.
|
|
545
|
-
*/
|
|
546
|
-
export function combinations(n, r) {
|
|
547
|
-
if (r > n || 0 >= n || 0 >= r)
|
|
548
|
-
return 0;
|
|
549
|
-
return factorial(n) / (factorial(r) * factorial(n - r));
|
|
550
|
-
}
|
|
551
|
-
/**
|
|
552
|
-
* Calculates the number of permutations of `n` items.
|
|
553
|
-
*
|
|
554
|
-
* @param {number} n Total number of items.
|
|
555
|
-
*
|
|
556
|
-
* @example
|
|
557
|
-
* ```ts
|
|
558
|
-
* permutations(5); // 120
|
|
559
|
-
* ```
|
|
560
|
-
*
|
|
561
|
-
* @returns {number} The number of permutations.
|
|
562
|
-
*/
|
|
563
|
-
export function permutations(n) {
|
|
564
|
-
if (0 >= n)
|
|
565
|
-
return 0;
|
|
566
|
-
return factorial(n);
|
|
567
|
-
}
|
|
568
|
-
/**
|
|
569
|
-
* Calculates the number of variations of `n` items taken `r` at a time.
|
|
570
|
-
*
|
|
571
|
-
* @param {number} n Total number of items.
|
|
572
|
-
* @param {number} r Number of items to choose.
|
|
573
|
-
*
|
|
574
|
-
* @example
|
|
575
|
-
* ```ts
|
|
576
|
-
* variations(5, 3); // 60
|
|
577
|
-
* ```
|
|
578
|
-
*
|
|
579
|
-
* @returns {number} The number of variations.
|
|
580
|
-
*/
|
|
581
|
-
export function variations(n, r) {
|
|
582
|
-
if (r > n || 0 >= n || 0 >= r)
|
|
583
|
-
return 0;
|
|
584
|
-
return factorial(n) / factorial(n - r);
|
|
585
|
-
}
|
|
586
|
-
/**
|
|
587
|
-
* Calculates the number of variations of `n` items taken `r` at a time, with repetitions.
|
|
588
|
-
*
|
|
589
|
-
* @param {number} n Total number of items.
|
|
590
|
-
* @param {number[]} rs Array of repetitions for each item.
|
|
591
|
-
*
|
|
592
|
-
* @example
|
|
593
|
-
* ```ts
|
|
594
|
-
* variationsR(5, [2, 3]); // 10
|
|
595
|
-
* ```
|
|
596
|
-
*
|
|
597
|
-
* @returns {number} The number of variations with repetitions.
|
|
598
|
-
*/
|
|
599
|
-
export function permutationsR(n, rs) {
|
|
600
|
-
if (sumArray(rs) !== n)
|
|
601
|
-
return 0;
|
|
602
|
-
return (factorial(n) / (rs.reduce((acc, curr) => acc * factorial(curr))));
|
|
603
|
-
}
|
|
604
|
-
/**
|
|
605
|
-
* Calculates the number of variations of `n` items taken `r` at a time, with repetitions.
|
|
606
|
-
*
|
|
607
|
-
* @param {number} n Total number of items.
|
|
608
|
-
* @param {number} r Number of items to choose.
|
|
609
|
-
*
|
|
610
|
-
* @example
|
|
611
|
-
* ```ts
|
|
612
|
-
* variationsR(5, 3); // 125
|
|
613
|
-
* ```
|
|
614
|
-
*
|
|
615
|
-
* @returns {number} The number of variations with repetitions.
|
|
616
|
-
*/
|
|
617
|
-
export function variationsR(n, r) {
|
|
618
|
-
if (r > n || 0 >= n || 0 >= r)
|
|
619
|
-
return 0;
|
|
620
|
-
return Math.pow(n, r);
|
|
621
|
-
}
|
|
622
|
-
/**
|
|
623
|
-
* Calculates the number of possibilities of a given set of options.
|
|
624
|
-
*
|
|
625
|
-
* @param {number} options Total number of options.
|
|
626
|
-
* @param {number | number[]} possibleCombinations Number of possible combinations.
|
|
627
|
-
* @param {ICombinatorialOptions} settings Settings for the calculation.
|
|
628
|
-
*
|
|
629
|
-
* @example
|
|
630
|
-
* ```ts
|
|
631
|
-
* "Given the numbers 1, 2, and 3, how many combinations of 2 can I make?"
|
|
632
|
-
* possibilities(3, 2, { elementsCanRepeat: false, orderMatters: false }); // 3
|
|
633
|
-
* ```
|
|
634
|
-
*
|
|
635
|
-
* @returns {number} The number of possibilities.
|
|
636
|
-
*/
|
|
637
|
-
export function possibilities(options, possibleCombinations, settings) {
|
|
638
|
-
const { elementsCanRepeat = false, orderMatters = false, allElementsUsed = false } = settings;
|
|
639
|
-
if (!orderMatters) {
|
|
640
|
-
if (Array.isArray(possibleCombinations))
|
|
641
|
-
throw new Error("Impossible to calculate.");
|
|
642
|
-
return combinations(options, possibleCombinations);
|
|
643
|
-
}
|
|
644
|
-
if (!elementsCanRepeat) {
|
|
645
|
-
if (Array.isArray(possibleCombinations))
|
|
646
|
-
throw new Error("Impossible to calculate.");
|
|
647
|
-
if (allElementsUsed)
|
|
648
|
-
return permutations(options);
|
|
649
|
-
return variations(options, possibleCombinations);
|
|
650
|
-
}
|
|
651
|
-
if (!allElementsUsed) {
|
|
652
|
-
if (Array.isArray(possibleCombinations))
|
|
653
|
-
throw new Error("Impossible to calculate.");
|
|
654
|
-
return variationsR(options, possibleCombinations);
|
|
655
|
-
}
|
|
656
|
-
if (!Array.isArray(possibleCombinations))
|
|
657
|
-
throw new Error("Impossible to calculate.");
|
|
658
|
-
return permutationsR(options, possibleCombinations);
|
|
659
|
-
}
|
|
1
|
+
function e(e){return e*Math.PI/180}function t(e){return e*180/Math.PI}function n(e){return e*.621371}function r(e){return e/.621371}function i(e){return e*3.28084}function a(e){return e/3.28084}function o(e){return e%2==0}function s(e){return e%2!=0}function c(e,t=0){return Number(e.toFixed(t))}function l(e,t){return Math.floor(Math.random()*(t-e+1))+e}function u(e){if(e<2)return!1;for(let t=2;t<=Math.sqrt(e);t++)if(e%t===0)return!1;return!0}function d(e){if(e<0)throw Error(`Index must be a non-negative integer.`);let t=[],n=0,r=0;for(;t.length<=e;)console.log(t),u(n)&&(t.push(n),r++),n++;return t[e]}function f(e){return e.reduce((e,t)=>e+t,0)}function p(e){return f(e)/e.length}function m(e,t,n){return e>=t&&e<=n}function h(e){return e<=1?1:e*h(e-1)}function g(e){return Number.isInteger(Math.sqrt(e))}function _(e,t){for(;t!==0;){let n=t;t=e%t,e=n}return e}function v(e,t){return e*t/_(e,t)}function y(e,t){return Math.abs(e-t)}function b(e){return e<0}function x(e){return e>0}function S(e){return Math.min(...e)}function C(e){return Math.max(...e)}function w(e){return e/2.54}function T(e){return e*2.54}function E(e){return e*1.8+32}function D(e){return(e-32)*(1/1.8)}function O(e){return e/1.609344}function k(e){return e*1.609344}function A(e,t){return t>e||0>=e||0>=t?0:h(e)/(h(t)*h(e-t))}function j(e){return 0>=e?0:h(e)}function M(e,t){return t>e||0>=e||0>=t?0:h(e)/h(e-t)}function N(e,t){return f(t)===e?h(e)/t.reduce((e,t)=>e*h(t)):0}function P(e,t){return t>e||0>=e||0>=t?0:e**+t}function F(e,t,n){let{elementsCanRepeat:r=!1,orderMatters:i=!1,allElementsUsed:a=!1}=n;if(!i){if(Array.isArray(t))throw Error(`Impossible to calculate.`);return A(e,t)}if(!r){if(Array.isArray(t))throw Error(`Impossible to calculate.`);return a?j(e):M(e,t)}if(!a){if(Array.isArray(t))throw Error(`Impossible to calculate.`);return P(e,t)}if(!Array.isArray(t))throw Error(`Impossible to calculate.`);return N(e,t)}export{y as absDiff,p as average,E as celsiusToFahrenheit,w as centimetersToInches,A as combinations,e as degreesToRadians,h as factorial,D as fahrenheitToCelsius,a as feetToMeters,_ as gcd,T as inchesToCentimeters,m as isBetween,o as isEven,b as isNegative,s as isOdd,g as isPerfectSquare,x as isPositive,u as isPrime,O as kilometersPerHourToMilesPerHour,n as kilometersToMiles,v as lcm,C as max,i as metersToFeet,k as milesPerHourToKilometersPerHour,r as milesToKilometers,S as min,j as permutations,N as permutationsR,F as possibilities,d as primeAt,t as radiansToDegrees,l as random,c as roundTo,f as sumArray,M as variations,P as variationsR};
|
|
2
|
+
//# sourceMappingURL=mod.js.map
|
package/package.json
CHANGED
|
@@ -8,19 +8,28 @@
|
|
|
8
8
|
},
|
|
9
9
|
"description": "A great set of utilities for interacting with numbers. Serving 36 functions.",
|
|
10
10
|
"homepage": "https://github.com/ZakaHaceCosas/dev-utils",
|
|
11
|
-
"version": "2.1.
|
|
11
|
+
"version": "2.1.1",
|
|
12
12
|
"type": "module",
|
|
13
13
|
"main": "./dist/mod.js",
|
|
14
14
|
"name": "numeric-utils",
|
|
15
15
|
"exports": {
|
|
16
16
|
".": {
|
|
17
|
-
"
|
|
17
|
+
"default": "./dist/mod.js",
|
|
18
18
|
"types": "./dist/mod.d.ts"
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
|
-
"files": [
|
|
21
|
+
"files": [
|
|
22
|
+
"dist/*.js",
|
|
23
|
+
"dist/*.d.ts"
|
|
24
|
+
],
|
|
22
25
|
"types": "./dist/mod.d.ts",
|
|
23
26
|
"license": "MIT",
|
|
24
|
-
"keywords": [
|
|
27
|
+
"keywords": [
|
|
28
|
+
"numbers",
|
|
29
|
+
"number",
|
|
30
|
+
"math",
|
|
31
|
+
"number utils",
|
|
32
|
+
"math utils"
|
|
33
|
+
],
|
|
25
34
|
"repository": "git+https://github.com/ZakaHaceCosas/dev-utils.git"
|
|
26
35
|
}
|
package/dist/mod.d.ts
DELETED
|
@@ -1,564 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A great set of utilities for interacting with numbers. Serving 36 functions.
|
|
3
|
-
* @author [ZakaHaceCosas](https://github.com/ZakaHaceCosas/)
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* ```ts
|
|
7
|
-
* import { average } from "@zakahacecosas/number-utils";
|
|
8
|
-
*
|
|
9
|
-
* const grades = [5, 7, 4, 9, 5, 6.1];
|
|
10
|
-
* const averageGrade = average(grades); // get the avg
|
|
11
|
-
* return `You scored an average of ${averageGrade}`;
|
|
12
|
-
* ```
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```ts
|
|
16
|
-
* import { roundTo, min, max } from "@zakahacecosas/number-utils";
|
|
17
|
-
*
|
|
18
|
-
* function analyzeWages(wages: number[]) {
|
|
19
|
-
* const avgWage = roundTo(average(avgWage), 2);
|
|
20
|
-
* const highestWage = max(avgWage);
|
|
21
|
-
* const lowestWage = min(avgWage);
|
|
22
|
-
*
|
|
23
|
-
* console.log(`Average wage: ${avgWage}€`);
|
|
24
|
-
* console.log(`Highest wage: ${highestWage}€`);
|
|
25
|
-
* console.log(`Lowest wage: ${lowestWage}€`);
|
|
26
|
-
* }
|
|
27
|
-
*
|
|
28
|
-
* const wages = [1800, 2200, 2500, 1900, 2750];
|
|
29
|
-
* analyzeWages(wages);
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* @module
|
|
33
|
-
*/
|
|
34
|
-
/**
|
|
35
|
-
* Options for combinatorial functions, {@linkcode possibilities}, {@linkcode combinations}, {@linkcode permutations}, and {@linkcode variations}.
|
|
36
|
-
*
|
|
37
|
-
* @export
|
|
38
|
-
* @interface ICombinatorialOptions
|
|
39
|
-
*/
|
|
40
|
-
export interface ICombinatorialOptions {
|
|
41
|
-
/** Whether elements are repeatable.
|
|
42
|
-
*
|
|
43
|
-
* Set it to true if, given options `1`, `2`, and `3`, `111` is considered a valid combination.
|
|
44
|
-
*/
|
|
45
|
-
elementsCanRepeat?: boolean;
|
|
46
|
-
/** Whether the order matters.
|
|
47
|
-
*
|
|
48
|
-
* Set it to false if, given options `A` and `B`, `AB` and `BA` are considered the same.
|
|
49
|
-
*
|
|
50
|
-
* When set to false, other settings from this interface are ignored.
|
|
51
|
-
*
|
|
52
|
-
* @default_value true
|
|
53
|
-
*/
|
|
54
|
-
orderMatters?: boolean;
|
|
55
|
-
/** Whether all elements will be used. This is used to determine the calculation method.
|
|
56
|
-
*
|
|
57
|
-
* Set it to false if not all elements are used. This is better understood with an example:
|
|
58
|
-
* ```txt
|
|
59
|
-
* Out of 10 people, 3 people can be elected to be part of a committee.
|
|
60
|
-
* ```
|
|
61
|
-
* In this case, `allElementsUsed` is false, since we are only using 3 out of 10 elements - yet the 10 elements count (as each of the 10 persons can be part of a possible combination).
|
|
62
|
-
*/
|
|
63
|
-
allElementsUsed?: boolean;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Converts degrees to radians.
|
|
67
|
-
*
|
|
68
|
-
* @param {number} deg Degrees.
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* ```ts
|
|
72
|
-
* degreesToRadians(180); // 3.141592653589793
|
|
73
|
-
* ```
|
|
74
|
-
*
|
|
75
|
-
* @returns {number} Radians.
|
|
76
|
-
* @available Also served from [geo-utils](https://jsr.io/@zakahacecosas/geo-utils).
|
|
77
|
-
*/
|
|
78
|
-
export declare function degreesToRadians(deg: number): number;
|
|
79
|
-
/**
|
|
80
|
-
* Converts radians to degrees.
|
|
81
|
-
*
|
|
82
|
-
* @param {number} rad Radians.
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
* ```ts
|
|
86
|
-
* radiansToDegrees(Math.PI); // 180
|
|
87
|
-
* ```
|
|
88
|
-
*
|
|
89
|
-
* @returns {number} Degrees.
|
|
90
|
-
* @available Also served from [geo-utils](https://jsr.io/@zakahacecosas/geo-utils).
|
|
91
|
-
*/
|
|
92
|
-
export declare function radiansToDegrees(rad: number): number;
|
|
93
|
-
/**
|
|
94
|
-
* Converts kilometers to miles.
|
|
95
|
-
*
|
|
96
|
-
* @param {number} km Kilometers.
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* ```ts
|
|
100
|
-
* kilometersToMiles(5); // 3.106855
|
|
101
|
-
* ```
|
|
102
|
-
* @available Also served from [geo-utils](https://jsr.io/@zakahacecosas/geo-utils).
|
|
103
|
-
|
|
104
|
-
* @returns {number} Miles.
|
|
105
|
-
*/
|
|
106
|
-
export declare function kilometersToMiles(km: number): number;
|
|
107
|
-
/**
|
|
108
|
-
* Converts miles to kilometers.
|
|
109
|
-
*
|
|
110
|
-
* @param {number} miles Miles.
|
|
111
|
-
*
|
|
112
|
-
* @example
|
|
113
|
-
* ```ts
|
|
114
|
-
* milesToKilometers(5); // 8.046722
|
|
115
|
-
* ```
|
|
116
|
-
*
|
|
117
|
-
* @returns {number} Degrees.
|
|
118
|
-
* @available Also served from [geo-utils](https://jsr.io/@zakahacecosas/geo-utils).
|
|
119
|
-
*/
|
|
120
|
-
export declare function milesToKilometers(miles: number): number;
|
|
121
|
-
/**
|
|
122
|
-
* Converts meters to feet.
|
|
123
|
-
*
|
|
124
|
-
* @param {number} meters Meters.
|
|
125
|
-
*
|
|
126
|
-
* @example
|
|
127
|
-
* ```ts
|
|
128
|
-
* metersToFeet(5); // 16.4042
|
|
129
|
-
* ```
|
|
130
|
-
*
|
|
131
|
-
* @returns {number} Feet.
|
|
132
|
-
* @available Also served from [geo-utils](https://jsr.io/@zakahacecosas/geo-utils).
|
|
133
|
-
*/
|
|
134
|
-
export declare function metersToFeet(meters: number): number;
|
|
135
|
-
/**
|
|
136
|
-
* Converts feet to meters.
|
|
137
|
-
*
|
|
138
|
-
* @param {number} feet Feet.
|
|
139
|
-
*
|
|
140
|
-
* @example
|
|
141
|
-
* ```ts
|
|
142
|
-
* feetToMeters(5); // 1.524
|
|
143
|
-
* ```
|
|
144
|
-
*
|
|
145
|
-
* @returns {number} Meters.
|
|
146
|
-
* @available Also served from [geo-utils](https://jsr.io/@zakahacecosas/geo-utils).
|
|
147
|
-
*/
|
|
148
|
-
export declare function feetToMeters(feet: number): number;
|
|
149
|
-
/**
|
|
150
|
-
* _A classic._ Checks if a given number is even or not. Analog to {@linkcode isOdd}.
|
|
151
|
-
*
|
|
152
|
-
* @param {number} num Number to check.
|
|
153
|
-
*
|
|
154
|
-
* @example
|
|
155
|
-
* ```ts
|
|
156
|
-
* isEven(2); // true
|
|
157
|
-
* isEven(3); // false
|
|
158
|
-
* ```
|
|
159
|
-
*
|
|
160
|
-
* @returns {boolean} True if it's even, false if otherwise.
|
|
161
|
-
*/
|
|
162
|
-
export declare function isEven(num: number): boolean;
|
|
163
|
-
/**
|
|
164
|
-
* _A classic._ Checks if a given number is odd or not. Analog to {@linkcode isEven}.
|
|
165
|
-
*
|
|
166
|
-
* @param {number} num Number to check.
|
|
167
|
-
*
|
|
168
|
-
* @example
|
|
169
|
-
* ```ts
|
|
170
|
-
* isOdd(2); // false
|
|
171
|
-
* isOdd(3); // true
|
|
172
|
-
* ```
|
|
173
|
-
*
|
|
174
|
-
* @returns {boolean} True if it's odd, false if otherwise.
|
|
175
|
-
*/
|
|
176
|
-
export declare function isOdd(num: number): boolean;
|
|
177
|
-
/**
|
|
178
|
-
* Rounds a given number to a specified decimal precision.
|
|
179
|
-
*
|
|
180
|
-
* @param {number} num Number to round.
|
|
181
|
-
* @param {number} [precision=0] Amount of decimals to include. Defaults to 0.
|
|
182
|
-
*
|
|
183
|
-
* @example
|
|
184
|
-
* ```ts
|
|
185
|
-
* roundTo(3.14159, 2) // 3.14
|
|
186
|
-
* ```
|
|
187
|
-
*
|
|
188
|
-
* @returns {number} The rounded number.
|
|
189
|
-
*/
|
|
190
|
-
export declare function roundTo(num: number, precision?: number): number;
|
|
191
|
-
/**
|
|
192
|
-
* Generates a random integer between min and max (inclusive).
|
|
193
|
-
*
|
|
194
|
-
* @param {number} min Minimum number, inclusive.
|
|
195
|
-
* @param {number} max Maximum number, inclusive.
|
|
196
|
-
*
|
|
197
|
-
* @example
|
|
198
|
-
* ```ts
|
|
199
|
-
* random(1, 10) // returns a random num between 1 and 10, e.g. "7"
|
|
200
|
-
* random(65, 105) // returns a random num between 65 and 105, e.g. "81"
|
|
201
|
-
* ```
|
|
202
|
-
*
|
|
203
|
-
* @returns {number} A random number between `min` and `max`.
|
|
204
|
-
*/
|
|
205
|
-
export declare function random(min: number, max: number): number;
|
|
206
|
-
/**
|
|
207
|
-
* Checks if a given number is prime.
|
|
208
|
-
*
|
|
209
|
-
* @param {number} num Number to check.
|
|
210
|
-
*
|
|
211
|
-
* @example
|
|
212
|
-
* ```ts
|
|
213
|
-
* isPrime(7) // true
|
|
214
|
-
* isPrime(8) // false
|
|
215
|
-
* ```
|
|
216
|
-
*
|
|
217
|
-
* @returns {boolean} True if the given number is prime, false if otherwise.
|
|
218
|
-
*/
|
|
219
|
-
export declare function isPrime(num: number): boolean;
|
|
220
|
-
/**
|
|
221
|
-
* Returns the n-th prime number. **Computationally expensive.**
|
|
222
|
-
*
|
|
223
|
-
* @export
|
|
224
|
-
* @param {number} index Index of the prime number, as in a JS array. Index 0 is number 2, for example.
|
|
225
|
-
*
|
|
226
|
-
* @example
|
|
227
|
-
* ```ts
|
|
228
|
-
* primeAt(0); // 2
|
|
229
|
-
* primeAt(3); // 7
|
|
230
|
-
* primeAt(9999999999); // your PC has likely crashed at this point
|
|
231
|
-
* ```
|
|
232
|
-
*
|
|
233
|
-
* @returns {number} The prime number at the given index.
|
|
234
|
-
*/
|
|
235
|
-
export declare function primeAt(index: number): number;
|
|
236
|
-
/**
|
|
237
|
-
* Returns the sum of all numbers in an array.
|
|
238
|
-
*
|
|
239
|
-
* @param {number[]} arr Array of numbers.
|
|
240
|
-
*
|
|
241
|
-
* @example
|
|
242
|
-
* ```ts
|
|
243
|
-
* sumArray([1, 2, 3]) // 6
|
|
244
|
-
* ```
|
|
245
|
-
*
|
|
246
|
-
* @returns {number} The sum of all numbers in the array.
|
|
247
|
-
*/
|
|
248
|
-
export declare function sumArray(arr: number[]): number;
|
|
249
|
-
/**
|
|
250
|
-
* Returns the average of all numbers in an array.
|
|
251
|
-
*
|
|
252
|
-
* @param {number[]} arr Array of numbers.
|
|
253
|
-
*
|
|
254
|
-
* @example
|
|
255
|
-
* ```ts
|
|
256
|
-
* average([1, 2, 3, 4]) // 2.5
|
|
257
|
-
* ```
|
|
258
|
-
*
|
|
259
|
-
* @returns {number} The average of all numbers in the array.
|
|
260
|
-
*/
|
|
261
|
-
export declare function average(arr: number[]): number;
|
|
262
|
-
/**
|
|
263
|
-
* Checks if a given number is between two numbers, both of them inclusive.
|
|
264
|
-
*
|
|
265
|
-
* @param {number} num Number to check.
|
|
266
|
-
* @param {number} min Minimum number. Inclusive.
|
|
267
|
-
* @param {number} max Maximum number. Inclusive.
|
|
268
|
-
*
|
|
269
|
-
* @example
|
|
270
|
-
* ```ts
|
|
271
|
-
* isBetween(5, 1, 10); // true
|
|
272
|
-
* isBetween(15, 1, 10); // false
|
|
273
|
-
* ```
|
|
274
|
-
*
|
|
275
|
-
* @returns {boolean} True if the number is between `min` and `max`, false if otherwise.
|
|
276
|
-
*/
|
|
277
|
-
export declare function isBetween(num: number, min: number, max: number): boolean;
|
|
278
|
-
/**
|
|
279
|
-
* Calculates the factorial of a number. For reference, the factorial of `n` (`n!`), is multiplying that number by each number below of it and above zero.
|
|
280
|
-
*
|
|
281
|
-
* @param {number} num Number to calculate the factorial for.
|
|
282
|
-
*
|
|
283
|
-
* @example
|
|
284
|
-
* ```ts
|
|
285
|
-
* factorial(5) // 120, since 5 * 4 * 3 * 2 * 1 = 120
|
|
286
|
-
* ```
|
|
287
|
-
*
|
|
288
|
-
* @returns {number} The factorial of the given number.
|
|
289
|
-
*/
|
|
290
|
-
export declare function factorial(num: number): number;
|
|
291
|
-
/**
|
|
292
|
-
* Checks if a number is a perfect square. For reference, a perfect square is a number that, when squared, returns an exact value.
|
|
293
|
-
*
|
|
294
|
-
* @param {number} num Number to check for.
|
|
295
|
-
*
|
|
296
|
-
* @example
|
|
297
|
-
* ```ts
|
|
298
|
-
* isPerfectSquare(16); // true
|
|
299
|
-
* isPerfectSquare(18); // false
|
|
300
|
-
* ```
|
|
301
|
-
*
|
|
302
|
-
* @returns {boolean} True if it's a perfect square, false if otherwise.
|
|
303
|
-
*/
|
|
304
|
-
export declare function isPerfectSquare(num: number): boolean;
|
|
305
|
-
/**
|
|
306
|
-
* Finds the greatest common divisor (GCD) of two numbers.
|
|
307
|
-
*
|
|
308
|
-
* @param {number} a First number.
|
|
309
|
-
* @param {number} b Second number.
|
|
310
|
-
*
|
|
311
|
-
* @example
|
|
312
|
-
* ```ts
|
|
313
|
-
* gcd(12, 18); // 6.
|
|
314
|
-
* ```
|
|
315
|
-
*
|
|
316
|
-
* @returns {number} The GCD of both numbers.
|
|
317
|
-
*/
|
|
318
|
-
export declare function gcd(a: number, b: number): number;
|
|
319
|
-
/**
|
|
320
|
-
* Finds the least common multiple (LCM) of two numbers.
|
|
321
|
-
*
|
|
322
|
-
* @param {number} a First number.
|
|
323
|
-
* @param {number} b Second number.
|
|
324
|
-
*
|
|
325
|
-
* @example
|
|
326
|
-
* ```ts
|
|
327
|
-
* lcm(12, 18); // 36.
|
|
328
|
-
* ```
|
|
329
|
-
*
|
|
330
|
-
* @returns {number} The LCM of both numbers.
|
|
331
|
-
*/
|
|
332
|
-
export declare function lcm(a: number, b: number): number;
|
|
333
|
-
/**
|
|
334
|
-
* Returns the absolute difference between two numbers.
|
|
335
|
-
*
|
|
336
|
-
* @param {number} a First number.
|
|
337
|
-
* @param {number} b Second number.
|
|
338
|
-
*
|
|
339
|
-
* @example
|
|
340
|
-
* ```ts
|
|
341
|
-
* absDiff(10, 3); // 7
|
|
342
|
-
* ```
|
|
343
|
-
*
|
|
344
|
-
* @returns {number} Their absolute difference.
|
|
345
|
-
*/
|
|
346
|
-
export declare function absDiff(a: number, b: number): number;
|
|
347
|
-
/**
|
|
348
|
-
* _A classic._ Checks if a given number is negative or not. Analog to {@linkcode isPositive}.
|
|
349
|
-
*
|
|
350
|
-
* @param {number} num Number to check.
|
|
351
|
-
*
|
|
352
|
-
* @example
|
|
353
|
-
* ```ts
|
|
354
|
-
* isNegative(-2); // true
|
|
355
|
-
* isNegative(3); // false
|
|
356
|
-
* ```
|
|
357
|
-
*
|
|
358
|
-
* @returns {boolean} True if it's negative, false if otherwise.
|
|
359
|
-
*/
|
|
360
|
-
export declare function isNegative(num: number): boolean;
|
|
361
|
-
/**
|
|
362
|
-
* _A classic._ Checks if a given number is positive or not. Analog to {@linkcode isNegative}.
|
|
363
|
-
*
|
|
364
|
-
* @param {number} num Number to check.
|
|
365
|
-
*
|
|
366
|
-
* @example
|
|
367
|
-
* ```ts
|
|
368
|
-
* isPositive(-2); // false
|
|
369
|
-
* isPositive(3); // true
|
|
370
|
-
* ```
|
|
371
|
-
*
|
|
372
|
-
* @returns {boolean} True if it's positive, false if otherwise.
|
|
373
|
-
*/
|
|
374
|
-
export declare function isPositive(num: number): boolean;
|
|
375
|
-
/**
|
|
376
|
-
* Returns the smallest number in an array.
|
|
377
|
-
*
|
|
378
|
-
* @param {number[]} arr Array of numbers.
|
|
379
|
-
*
|
|
380
|
-
* @example
|
|
381
|
-
* ```ts
|
|
382
|
-
* min([4, 2, 7, 1]); // 1
|
|
383
|
-
* ```
|
|
384
|
-
*
|
|
385
|
-
* @returns {number} The smallest number in the array.
|
|
386
|
-
*/
|
|
387
|
-
export declare function min(arr: number[]): number;
|
|
388
|
-
/**
|
|
389
|
-
* Returns the highest number in an array.
|
|
390
|
-
*
|
|
391
|
-
* @param {number[]} arr Array of numbers.
|
|
392
|
-
*
|
|
393
|
-
* @example
|
|
394
|
-
* ```ts
|
|
395
|
-
* max([4, 2, 7, 1]); // 7
|
|
396
|
-
* ```
|
|
397
|
-
*
|
|
398
|
-
* @returns {number} The highest number in the array.
|
|
399
|
-
*/
|
|
400
|
-
export declare function max(arr: number[]): number;
|
|
401
|
-
/**
|
|
402
|
-
* Turns centimeters (CM) to inches (IN).
|
|
403
|
-
*
|
|
404
|
-
* @param {number} cms Centimeters.
|
|
405
|
-
*
|
|
406
|
-
* @example
|
|
407
|
-
* ```ts
|
|
408
|
-
* centimetersToInches(10); // 3.94
|
|
409
|
-
* ```
|
|
410
|
-
*
|
|
411
|
-
* @returns {number} Inches.
|
|
412
|
-
*/
|
|
413
|
-
export declare function centimetersToInches(cms: number): number;
|
|
414
|
-
/**
|
|
415
|
-
* Turns inches (IN) to centimeters (CM).
|
|
416
|
-
*
|
|
417
|
-
* @param {number} ins Inches.
|
|
418
|
-
*
|
|
419
|
-
* @example
|
|
420
|
-
* ```ts
|
|
421
|
-
* inchesToCentimeters(10); // 25.4
|
|
422
|
-
* ```
|
|
423
|
-
*
|
|
424
|
-
* @returns {number} Inches.
|
|
425
|
-
*/
|
|
426
|
-
export declare function inchesToCentimeters(ins: number): number;
|
|
427
|
-
/**
|
|
428
|
-
* Turns Celsius (ºC) to Fahrenheit (ºF). Analog to {@linkcode fahrenheitToCelsius}.
|
|
429
|
-
*
|
|
430
|
-
* @param {number} celsius Celsius.
|
|
431
|
-
*
|
|
432
|
-
* @example
|
|
433
|
-
* ```ts
|
|
434
|
-
* celsiusToFahrenheit(25); // 77
|
|
435
|
-
* ```
|
|
436
|
-
*
|
|
437
|
-
* @returns {number} Fahrenheit.
|
|
438
|
-
*/
|
|
439
|
-
export declare function celsiusToFahrenheit(celsius: number): number;
|
|
440
|
-
/**
|
|
441
|
-
* Turns Fahrenheit (ºF) to Celsius (ºC). Analog to {@linkcode celsiusToFahrenheit}.
|
|
442
|
-
*
|
|
443
|
-
* @param {number} fahrenheit Celsius.
|
|
444
|
-
*
|
|
445
|
-
* @example
|
|
446
|
-
* ```ts
|
|
447
|
-
* fahrenheitToCelsius(104); // 40
|
|
448
|
-
* ```
|
|
449
|
-
*
|
|
450
|
-
* @returns {number} Celsius.
|
|
451
|
-
*/
|
|
452
|
-
export declare function fahrenheitToCelsius(fahrenheit: number): number;
|
|
453
|
-
/**
|
|
454
|
-
* Turns kilometers per hour (KPH or KM/H) to Miles per hour (MPH). Analog to {@linkcode milesPerHourToKilometersPerHour}.
|
|
455
|
-
*
|
|
456
|
-
* @param {number} kph KPH.
|
|
457
|
-
*
|
|
458
|
-
* @example
|
|
459
|
-
* ```ts
|
|
460
|
-
* kilometersPerHourToMilesPerHour(180); // 111.846815
|
|
461
|
-
* ```
|
|
462
|
-
*
|
|
463
|
-
* @returns {number} MPH.
|
|
464
|
-
*/
|
|
465
|
-
export declare function kilometersPerHourToMilesPerHour(kph: number): number;
|
|
466
|
-
/**
|
|
467
|
-
* Turns Miles per hour (MPH) to Kilometers per hour (KPH or KM/H). Analog to {@linkcode milesPerHourToKilometersPerHour}.
|
|
468
|
-
*
|
|
469
|
-
* @param {number} mph MPH.
|
|
470
|
-
*
|
|
471
|
-
* @example
|
|
472
|
-
* ```ts
|
|
473
|
-
* milesPerHourToKilometersPerHour(180); // 289.68192
|
|
474
|
-
* ```
|
|
475
|
-
*
|
|
476
|
-
* @returns {number} MPH.
|
|
477
|
-
*/
|
|
478
|
-
export declare function milesPerHourToKilometersPerHour(mph: number): number;
|
|
479
|
-
/**
|
|
480
|
-
* Calculates the number of combinations of `n` items taken `r` at a time.
|
|
481
|
-
*
|
|
482
|
-
* @param {number} n Total number of items.
|
|
483
|
-
* @param {number} r Number of items to choose.
|
|
484
|
-
*
|
|
485
|
-
* @example
|
|
486
|
-
* ```ts
|
|
487
|
-
* combinations(5, 3); // 10
|
|
488
|
-
* ```
|
|
489
|
-
*
|
|
490
|
-
* @returns {number} The number of combinations.
|
|
491
|
-
*/
|
|
492
|
-
export declare function combinations(n: number, r: number): number;
|
|
493
|
-
/**
|
|
494
|
-
* Calculates the number of permutations of `n` items.
|
|
495
|
-
*
|
|
496
|
-
* @param {number} n Total number of items.
|
|
497
|
-
*
|
|
498
|
-
* @example
|
|
499
|
-
* ```ts
|
|
500
|
-
* permutations(5); // 120
|
|
501
|
-
* ```
|
|
502
|
-
*
|
|
503
|
-
* @returns {number} The number of permutations.
|
|
504
|
-
*/
|
|
505
|
-
export declare function permutations(n: number): number;
|
|
506
|
-
/**
|
|
507
|
-
* Calculates the number of variations of `n` items taken `r` at a time.
|
|
508
|
-
*
|
|
509
|
-
* @param {number} n Total number of items.
|
|
510
|
-
* @param {number} r Number of items to choose.
|
|
511
|
-
*
|
|
512
|
-
* @example
|
|
513
|
-
* ```ts
|
|
514
|
-
* variations(5, 3); // 60
|
|
515
|
-
* ```
|
|
516
|
-
*
|
|
517
|
-
* @returns {number} The number of variations.
|
|
518
|
-
*/
|
|
519
|
-
export declare function variations(n: number, r: number): number;
|
|
520
|
-
/**
|
|
521
|
-
* Calculates the number of variations of `n` items taken `r` at a time, with repetitions.
|
|
522
|
-
*
|
|
523
|
-
* @param {number} n Total number of items.
|
|
524
|
-
* @param {number[]} rs Array of repetitions for each item.
|
|
525
|
-
*
|
|
526
|
-
* @example
|
|
527
|
-
* ```ts
|
|
528
|
-
* variationsR(5, [2, 3]); // 10
|
|
529
|
-
* ```
|
|
530
|
-
*
|
|
531
|
-
* @returns {number} The number of variations with repetitions.
|
|
532
|
-
*/
|
|
533
|
-
export declare function permutationsR(n: number, rs: number[]): number;
|
|
534
|
-
/**
|
|
535
|
-
* Calculates the number of variations of `n` items taken `r` at a time, with repetitions.
|
|
536
|
-
*
|
|
537
|
-
* @param {number} n Total number of items.
|
|
538
|
-
* @param {number} r Number of items to choose.
|
|
539
|
-
*
|
|
540
|
-
* @example
|
|
541
|
-
* ```ts
|
|
542
|
-
* variationsR(5, 3); // 125
|
|
543
|
-
* ```
|
|
544
|
-
*
|
|
545
|
-
* @returns {number} The number of variations with repetitions.
|
|
546
|
-
*/
|
|
547
|
-
export declare function variationsR(n: number, r: number): number;
|
|
548
|
-
/**
|
|
549
|
-
* Calculates the number of possibilities of a given set of options.
|
|
550
|
-
*
|
|
551
|
-
* @param {number} options Total number of options.
|
|
552
|
-
* @param {number | number[]} possibleCombinations Number of possible combinations.
|
|
553
|
-
* @param {ICombinatorialOptions} settings Settings for the calculation.
|
|
554
|
-
*
|
|
555
|
-
* @example
|
|
556
|
-
* ```ts
|
|
557
|
-
* "Given the numbers 1, 2, and 3, how many combinations of 2 can I make?"
|
|
558
|
-
* possibilities(3, 2, { elementsCanRepeat: false, orderMatters: false }); // 3
|
|
559
|
-
* ```
|
|
560
|
-
*
|
|
561
|
-
* @returns {number} The number of possibilities.
|
|
562
|
-
*/
|
|
563
|
-
export declare function possibilities(options: number, possibleCombinations: number | number[], settings: ICombinatorialOptions): number;
|
|
564
|
-
//# sourceMappingURL=mod.d.ts.map
|