@etsoo/shared 1.2.71 → 1.2.72
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/README.md +1 -0
- package/__tests__/NumberUtils.ts +9 -0
- package/lib/cjs/NumberUtils.d.ts +5 -0
- package/lib/cjs/NumberUtils.js +5 -0
- package/lib/mjs/NumberUtils.d.ts +5 -0
- package/lib/mjs/NumberUtils.js +5 -0
- package/package.json +1 -1
- package/src/NumberUtils.ts +12 -0
package/README.md
CHANGED
|
@@ -276,6 +276,7 @@ Numbers related utilities
|
|
|
276
276
|
| getCurrencySymbol | Get currency symbol or name from ISO code |
|
|
277
277
|
| parse | Parse to number, with or without default value |
|
|
278
278
|
| toExact | To the exact precision number avoiding precision lost |
|
|
279
|
+
| toStep | To the step number, like 0, 10, 20, 30 for step 10 |
|
|
279
280
|
|
|
280
281
|
## StorageUtils
|
|
281
282
|
|
package/__tests__/NumberUtils.ts
CHANGED
|
@@ -50,3 +50,12 @@ test("Tests for toFileSize", () => {
|
|
|
50
50
|
expect(NumberUtils.formatFileSize(1125000)).toBe("1.07 MB");
|
|
51
51
|
expect(NumberUtils.formatFileSize(1125000, 1)).toBe("1.1 MB");
|
|
52
52
|
});
|
|
53
|
+
|
|
54
|
+
test("Tests for toStep", () => {
|
|
55
|
+
const nums = [9, 13, 20, 33, 99, 101, 3009];
|
|
56
|
+
const results = nums.map((num) => num.toStep(10));
|
|
57
|
+
expect(results).toStrictEqual([0, 10, 20, 30, 90, 100, 3000]);
|
|
58
|
+
|
|
59
|
+
const results2 = nums.map((num) => num.toStep(8));
|
|
60
|
+
expect(results2).toStrictEqual([8, 8, 16, 32, 96, 96, 3008]);
|
|
61
|
+
});
|
package/lib/cjs/NumberUtils.d.ts
CHANGED
|
@@ -5,6 +5,11 @@ declare global {
|
|
|
5
5
|
* @param precision Precision
|
|
6
6
|
*/
|
|
7
7
|
toExact(precision?: number): number;
|
|
8
|
+
/**
|
|
9
|
+
* To the step number, like 0, 10, 20, 30 for step 10
|
|
10
|
+
* @param step Step
|
|
11
|
+
*/
|
|
12
|
+
toStep(step: number): number;
|
|
8
13
|
}
|
|
9
14
|
}
|
|
10
15
|
export declare namespace NumberUtils {
|
package/lib/cjs/NumberUtils.js
CHANGED
|
@@ -9,6 +9,11 @@ Number.prototype.toExact = function (precision) {
|
|
|
9
9
|
const p = Math.pow(10, precision);
|
|
10
10
|
return Math.round(this * p) / p;
|
|
11
11
|
};
|
|
12
|
+
Number.prototype.toStep = function (step) {
|
|
13
|
+
if (step <= 0)
|
|
14
|
+
return this;
|
|
15
|
+
return Math.floor(this / step) * step;
|
|
16
|
+
};
|
|
12
17
|
var NumberUtils;
|
|
13
18
|
(function (NumberUtils) {
|
|
14
19
|
/**
|
package/lib/mjs/NumberUtils.d.ts
CHANGED
|
@@ -5,6 +5,11 @@ declare global {
|
|
|
5
5
|
* @param precision Precision
|
|
6
6
|
*/
|
|
7
7
|
toExact(precision?: number): number;
|
|
8
|
+
/**
|
|
9
|
+
* To the step number, like 0, 10, 20, 30 for step 10
|
|
10
|
+
* @param step Step
|
|
11
|
+
*/
|
|
12
|
+
toStep(step: number): number;
|
|
8
13
|
}
|
|
9
14
|
}
|
|
10
15
|
export declare namespace NumberUtils {
|
package/lib/mjs/NumberUtils.js
CHANGED
|
@@ -6,6 +6,11 @@ Number.prototype.toExact = function (precision) {
|
|
|
6
6
|
const p = Math.pow(10, precision);
|
|
7
7
|
return Math.round(this * p) / p;
|
|
8
8
|
};
|
|
9
|
+
Number.prototype.toStep = function (step) {
|
|
10
|
+
if (step <= 0)
|
|
11
|
+
return this;
|
|
12
|
+
return Math.floor(this / step) * step;
|
|
13
|
+
};
|
|
9
14
|
export var NumberUtils;
|
|
10
15
|
(function (NumberUtils) {
|
|
11
16
|
/**
|
package/package.json
CHANGED
package/src/NumberUtils.ts
CHANGED
|
@@ -5,6 +5,12 @@ declare global {
|
|
|
5
5
|
* @param precision Precision
|
|
6
6
|
*/
|
|
7
7
|
toExact(precision?: number): number;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* To the step number, like 0, 10, 20, 30 for step 10
|
|
11
|
+
* @param step Step
|
|
12
|
+
*/
|
|
13
|
+
toStep(step: number): number;
|
|
8
14
|
}
|
|
9
15
|
}
|
|
10
16
|
|
|
@@ -17,6 +23,12 @@ Number.prototype.toExact = function (this: number, precision?: number) {
|
|
|
17
23
|
return Math.round(this * p) / p;
|
|
18
24
|
};
|
|
19
25
|
|
|
26
|
+
Number.prototype.toStep = function (this: number, step: number) {
|
|
27
|
+
if (step <= 0) return this;
|
|
28
|
+
|
|
29
|
+
return Math.floor(this / step) * step;
|
|
30
|
+
};
|
|
31
|
+
|
|
20
32
|
export namespace NumberUtils {
|
|
21
33
|
/**
|
|
22
34
|
* Format number
|