nhb-toolbox 4.0.21 → 4.0.30
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/cjs/date/Chronos.js +109 -6
- package/dist/cjs/index.js +30 -12
- package/dist/cjs/number/basics.js +86 -20
- package/dist/cjs/number/convert.js +6 -4
- package/dist/cjs/number/fibonacci.js +89 -0
- package/dist/cjs/number/utilities.js +12 -8
- package/dist/dts/date/Chronos.d.ts +32 -1
- package/dist/dts/date/Chronos.d.ts.map +1 -1
- package/dist/dts/date/types.d.ts +2 -0
- package/dist/dts/date/types.d.ts.map +1 -1
- package/dist/dts/index.d.ts +4 -3
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/dts/number/basics.d.ts +35 -9
- package/dist/dts/number/basics.d.ts.map +1 -1
- package/dist/dts/number/convert.d.ts +3 -2
- package/dist/dts/number/convert.d.ts.map +1 -1
- package/dist/dts/number/fibonacci.d.ts +31 -0
- package/dist/dts/number/fibonacci.d.ts.map +1 -0
- package/dist/dts/number/utilities.d.ts +5 -5
- package/dist/dts/number/utilities.d.ts.map +1 -1
- package/dist/esm/date/Chronos.js +110 -7
- package/dist/esm/index.js +4 -3
- package/dist/esm/number/basics.js +82 -19
- package/dist/esm/number/convert.js +6 -4
- package/dist/esm/number/fibonacci.js +83 -0
- package/dist/esm/number/utilities.js +12 -8
- package/package.json +1 -1
|
@@ -5,7 +5,8 @@ import { _convertLessThanThousand } from './helpers';
|
|
|
5
5
|
* @param number - The number to convert into words.
|
|
6
6
|
* @returns The number converted in words.
|
|
7
7
|
*/
|
|
8
|
-
export function numberToWords(
|
|
8
|
+
export function numberToWords(num) {
|
|
9
|
+
let number = Number(num);
|
|
9
10
|
const isNegative = number < 0;
|
|
10
11
|
if (number === 0)
|
|
11
12
|
return 'zero';
|
|
@@ -31,7 +32,8 @@ export function numberToWords(number) {
|
|
|
31
32
|
* @example convertToRomanNumerals(29) → "XXIX"
|
|
32
33
|
*/
|
|
33
34
|
export const convertToRomanNumerals = (num) => {
|
|
34
|
-
|
|
35
|
+
let number = Number(num);
|
|
36
|
+
if (number <= 0 || number >= 4000)
|
|
35
37
|
throw new RangeError('Number must be between 1 and 3999');
|
|
36
38
|
const romanMap = [
|
|
37
39
|
[1000, 'M'],
|
|
@@ -50,9 +52,9 @@ export const convertToRomanNumerals = (num) => {
|
|
|
50
52
|
];
|
|
51
53
|
let result = '';
|
|
52
54
|
for (const [value, numeral] of romanMap) {
|
|
53
|
-
while (
|
|
55
|
+
while (number >= value) {
|
|
54
56
|
result += numeral;
|
|
55
|
-
|
|
57
|
+
number -= value;
|
|
56
58
|
}
|
|
57
59
|
}
|
|
58
60
|
return result;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* * Generates the first `limit` Fibonacci numbers.
|
|
3
|
+
*
|
|
4
|
+
* @param limit The number of Fibonacci numbers to generate.
|
|
5
|
+
* @returns An array containing the first `limit` Fibonacci numbers.
|
|
6
|
+
*/
|
|
7
|
+
export function getFibonacciSeries(limit) {
|
|
8
|
+
const cLimit = Number(limit);
|
|
9
|
+
if (!Number.isFinite(cLimit) || cLimit <= 0) {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
if (cLimit === 1)
|
|
13
|
+
return [0];
|
|
14
|
+
const series = [0, 1];
|
|
15
|
+
for (let i = 2; i < cLimit; i++) {
|
|
16
|
+
series.push(series[i - 1] + series[i - 2]);
|
|
17
|
+
}
|
|
18
|
+
return series;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* * Generates the first `limit` Fibonacci numbers using recursion with memoization.
|
|
22
|
+
*
|
|
23
|
+
* @param limit - The number of Fibonacci numbers to generate.
|
|
24
|
+
* @returns An array containing the first `limit` Fibonacci numbers.
|
|
25
|
+
*/
|
|
26
|
+
export function getFibonacciSeriesMemo(limit) {
|
|
27
|
+
const cLimit = Number(limit);
|
|
28
|
+
if (!Number.isFinite(cLimit) || cLimit <= 0)
|
|
29
|
+
return [];
|
|
30
|
+
if (cLimit === 1)
|
|
31
|
+
return [0];
|
|
32
|
+
const memo = new Map([
|
|
33
|
+
[0, 0],
|
|
34
|
+
[1, 1],
|
|
35
|
+
]);
|
|
36
|
+
const fib = (n) => {
|
|
37
|
+
if (memo.has(n))
|
|
38
|
+
return memo.get(n);
|
|
39
|
+
const val = fib(n - 1) + fib(n - 2);
|
|
40
|
+
memo.set(n, val);
|
|
41
|
+
return val;
|
|
42
|
+
};
|
|
43
|
+
return Array.from({ length: cLimit }, (_, i) => fib(i));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* * Generator function for Fibonacci sequence up to a given limit.
|
|
47
|
+
*
|
|
48
|
+
* @param limit - Number of Fibonacci numbers to generate.
|
|
49
|
+
* @param onYield - Optional callback triggered on each yield with the current value and index.
|
|
50
|
+
* @returns A generator yielding Fibonacci numbers one by one.
|
|
51
|
+
*/
|
|
52
|
+
export function* fibonacciGenerator(limit, onYield) {
|
|
53
|
+
const cLimit = Number(limit);
|
|
54
|
+
if (!Number.isFinite(cLimit) || cLimit < 0)
|
|
55
|
+
return;
|
|
56
|
+
let a = 0;
|
|
57
|
+
let b = 1;
|
|
58
|
+
for (let i = 0; i < cLimit; i++) {
|
|
59
|
+
onYield?.(a, i);
|
|
60
|
+
yield a;
|
|
61
|
+
[a, b] = [b, a + b];
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* * Calculates the `n`-th Fibonacci number using optimized space.
|
|
66
|
+
*
|
|
67
|
+
* @param index - The index (0-based) of the Fibonacci number.
|
|
68
|
+
* @returns The index=`n`-th Fibonacci number.
|
|
69
|
+
*/
|
|
70
|
+
export function getNthFibonacci(index) {
|
|
71
|
+
const n = Number(index);
|
|
72
|
+
if (!Number.isFinite(n) || n < 0)
|
|
73
|
+
return NaN;
|
|
74
|
+
if (n === 0)
|
|
75
|
+
return 0;
|
|
76
|
+
if (n === 1)
|
|
77
|
+
return 1;
|
|
78
|
+
let a = 0, b = 1;
|
|
79
|
+
for (let i = 2; i <= n; i++) {
|
|
80
|
+
[a, b] = [b, a + b];
|
|
81
|
+
}
|
|
82
|
+
return b;
|
|
83
|
+
}
|
|
@@ -6,7 +6,9 @@ import { CURRENCY_LOCALES } from './constants';
|
|
|
6
6
|
* @returns The number rounded to the nearest interval.
|
|
7
7
|
* @example roundToNearest(27, 5) → 25
|
|
8
8
|
*/
|
|
9
|
-
export const roundToNearest = (value, interval = 5) =>
|
|
9
|
+
export const roundToNearest = (value, interval = 5) => {
|
|
10
|
+
return Math.round(Number(value) / interval) * interval;
|
|
11
|
+
};
|
|
10
12
|
/**
|
|
11
13
|
* * Formats a number as a currency string.
|
|
12
14
|
* @param value - The number to format.
|
|
@@ -34,7 +36,9 @@ export const formatCurrency = (value, currency = 'USD', locale) => {
|
|
|
34
36
|
* @example clampNumber(5, 10, 20) → 10
|
|
35
37
|
* @example clampNumber(25, 10, 20) → 20
|
|
36
38
|
*/
|
|
37
|
-
export const clampNumber = (value, min, max) =>
|
|
39
|
+
export const clampNumber = (value, min, max) => {
|
|
40
|
+
return Math.max(min, Math.min(value, max));
|
|
41
|
+
};
|
|
38
42
|
/**
|
|
39
43
|
* * Generates a random floating-point number within a range.
|
|
40
44
|
* @param min - The minimum value.
|
|
@@ -43,7 +47,7 @@ export const clampNumber = (value, min, max) => Math.max(min, Math.min(value, ma
|
|
|
43
47
|
* @example randomFloat(1.5, 3.5) → 2.84623
|
|
44
48
|
*/
|
|
45
49
|
export const getRandomFloat = (min, max) => {
|
|
46
|
-
return Math.random() * (max - min) + min;
|
|
50
|
+
return Math.random() * (Number(max) - Number(min)) + Number(min);
|
|
47
51
|
};
|
|
48
52
|
/**
|
|
49
53
|
* * Returns the ordinal suffix for a given number (e.g., 1 -> 'st', 2 -> 'nd', 3 -> 'rd', 4 -> 'th' etc.).
|
|
@@ -51,13 +55,13 @@ export const getRandomFloat = (min, max) => {
|
|
|
51
55
|
* If the `withNumber` parameter is `true`, the function returns the number along with its ordinal suffix (e.g., "1st").
|
|
52
56
|
* Otherwise, it returns only the ordinal suffix (e.g., "st").
|
|
53
57
|
*
|
|
54
|
-
* @param
|
|
58
|
+
* @param num - The number or number string to get the ordinal suffix for.
|
|
55
59
|
* @param withNumber - Whether to include the number along with its ordinal suffix (defaults to `true`).
|
|
56
60
|
* @returns The appropriate ordinal suffix, optionally with the number (e.g., '1st' or 'st`, '2nd' or 'nd' and so on.).
|
|
57
61
|
*/
|
|
58
|
-
export const getOrdinal = (
|
|
59
|
-
const remainder10 = Number(
|
|
60
|
-
const remainder100 = Number(
|
|
62
|
+
export const getOrdinal = (num, withNumber = true) => {
|
|
63
|
+
const remainder10 = Number(num) % 10;
|
|
64
|
+
const remainder100 = Number(num) % 100;
|
|
61
65
|
let suffix;
|
|
62
66
|
if (remainder10 === 1 && remainder100 !== 11) {
|
|
63
67
|
suffix = 'st';
|
|
@@ -71,5 +75,5 @@ export const getOrdinal = (n, withNumber = true) => {
|
|
|
71
75
|
else {
|
|
72
76
|
suffix = 'th';
|
|
73
77
|
}
|
|
74
|
-
return withNumber ? String(
|
|
78
|
+
return withNumber ? String(num).concat(suffix) : suffix;
|
|
75
79
|
};
|
package/package.json
CHANGED