@synnaxlabs/x 0.52.4 → 0.52.5
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/.turbo/turbo-build.log +5 -5
- package/dist/src/math/round.d.ts +0 -38
- package/dist/src/math/round.d.ts.map +1 -1
- package/dist/x.cjs +6 -6
- package/dist/x.js +831 -840
- package/package.json +1 -1
- package/src/math/round.spec.ts +0 -68
- package/src/math/round.ts +0 -52
- package/tsconfig.tsbuildinfo +1 -1
package/package.json
CHANGED
package/src/math/round.spec.ts
CHANGED
|
@@ -14,74 +14,6 @@ import { describe, expect, it } from "vitest";
|
|
|
14
14
|
import { math } from "@/math";
|
|
15
15
|
import { type bounds } from "@/spatial";
|
|
16
16
|
|
|
17
|
-
interface TestCase {
|
|
18
|
-
name: string;
|
|
19
|
-
value: number;
|
|
20
|
-
bounds: bounds.Bounds<number>;
|
|
21
|
-
expected: number;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const TEST_CASES: TestCase[] = [
|
|
25
|
-
{
|
|
26
|
-
name: "should handle large spans (>= 1000)",
|
|
27
|
-
value: 1234.5678,
|
|
28
|
-
bounds: { lower: 0, upper: 2000 },
|
|
29
|
-
expected: 1234.57,
|
|
30
|
-
},
|
|
31
|
-
{
|
|
32
|
-
name: "should handle medium spans (>= 1)",
|
|
33
|
-
value: 1.23456,
|
|
34
|
-
bounds: { lower: 0, upper: 2 },
|
|
35
|
-
expected: 1.235,
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
name: "should handle small spans (< 1)",
|
|
39
|
-
value: 0.123456,
|
|
40
|
-
bounds: { lower: 0, upper: 0.2 },
|
|
41
|
-
expected: 0.123,
|
|
42
|
-
},
|
|
43
|
-
{
|
|
44
|
-
name: "should handle very small spans",
|
|
45
|
-
value: 0.0001234,
|
|
46
|
-
bounds: { lower: 0, upper: 0.0002 },
|
|
47
|
-
expected: 0.000123,
|
|
48
|
-
},
|
|
49
|
-
{
|
|
50
|
-
name: "should handle negative values",
|
|
51
|
-
value: -1.23456,
|
|
52
|
-
bounds: { lower: -2, upper: 0 },
|
|
53
|
-
expected: -1.235,
|
|
54
|
-
},
|
|
55
|
-
{
|
|
56
|
-
name: "should handle NaN",
|
|
57
|
-
value: NaN,
|
|
58
|
-
bounds: { lower: 0, upper: 1 },
|
|
59
|
-
expected: NaN,
|
|
60
|
-
},
|
|
61
|
-
{
|
|
62
|
-
name: "should handle Infinity",
|
|
63
|
-
value: Infinity,
|
|
64
|
-
bounds: { lower: 0, upper: 1 },
|
|
65
|
-
expected: Infinity,
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
name: "should handle zero span bounds",
|
|
69
|
-
value: 1.23456,
|
|
70
|
-
bounds: { lower: 1, upper: 1 },
|
|
71
|
-
expected: 1.23456,
|
|
72
|
-
},
|
|
73
|
-
];
|
|
74
|
-
|
|
75
|
-
describe("roundBySpan", () => {
|
|
76
|
-
TEST_CASES.forEach(({ name, value, bounds, expected }) => {
|
|
77
|
-
it(name, () => {
|
|
78
|
-
const result = math.roundBySpan(value, bounds);
|
|
79
|
-
if (Number.isNaN(expected)) expect(Number.isNaN(result)).toBe(true);
|
|
80
|
-
else expect(result).toBe(expected);
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
|
|
85
17
|
interface SmartRoundTestCase {
|
|
86
18
|
value: number;
|
|
87
19
|
bounds?: bounds.Bounds<number>;
|
package/src/math/round.ts
CHANGED
|
@@ -15,58 +15,6 @@ const MEDIUM_SPAN_DECIMAL_PLACES = 3;
|
|
|
15
15
|
// The number of additional decimal places to show past the precision of the span.
|
|
16
16
|
const EXTRA_DECIMAL_PLACES = 2;
|
|
17
17
|
|
|
18
|
-
/**
|
|
19
|
-
* Rounds a number based on the span of the provided bounds. The function adjusts the
|
|
20
|
-
* number of decimal places based on the magnitude of the bounds.
|
|
21
|
-
*
|
|
22
|
-
* @param value - The number to be rounded.
|
|
23
|
-
* @param bounds - The bounds object containing the min and max values that provide
|
|
24
|
-
* context for rounding.
|
|
25
|
-
* @returns The rounded number.
|
|
26
|
-
*
|
|
27
|
-
* Rules for decimal places:
|
|
28
|
-
* - For spans >= 1000: 2 decimal places
|
|
29
|
-
* - For spans >= 1: 3 decimal places
|
|
30
|
-
* - For spans < 1: 2 decimal places + 2 decimal places past the precision of the span
|
|
31
|
-
*
|
|
32
|
-
* Edge cases:
|
|
33
|
-
* - If the value is `NaN`, returns `NaN`
|
|
34
|
-
* - If the value is `Infinity` or `-Infinity`, returns the original value
|
|
35
|
-
* - If the bounds span is 0, returns the original value
|
|
36
|
-
*
|
|
37
|
-
* Examples:
|
|
38
|
-
* ```typescript
|
|
39
|
-
* // Large spans (>= 1000) use 2 decimal places
|
|
40
|
-
* roundBySpan(1234.5678, { start: 0, end: 2000 }); // 1200
|
|
41
|
-
*
|
|
42
|
-
* // Medium spans (>= 1) use 3 decimal places
|
|
43
|
-
* roundBySpan(1.23456, { start: 0, end: 2 }); // 1.235
|
|
44
|
-
*
|
|
45
|
-
* // Small spans (< 1) adapt based on the span
|
|
46
|
-
* roundBySpan(0.123456, { start: 0, end: 0.2 }); // 0.123 = 1 + 2 decimal places
|
|
47
|
-
* roundBySpan(0.0001234, { start: 0, end: 0.001 }); // 0.00012 = 3 + 2 decimal places
|
|
48
|
-
*
|
|
49
|
-
* // Edge cases
|
|
50
|
-
* roundBySpan(NaN, { start: 0, end: 1 }); // NaN
|
|
51
|
-
* roundBySpan(Infinity, { start: 0, end: 1 }); // Infinity
|
|
52
|
-
* roundBySpan(123, { start: 1, end: 1 }); // 123 (span is 0)
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
export const roundBySpan = (value: number, b: bounds.Bounds<number>): number => {
|
|
56
|
-
if (Number.isNaN(value) || !Number.isFinite(value)) return value;
|
|
57
|
-
const span = bounds.span(b);
|
|
58
|
-
if (span == 0) return value;
|
|
59
|
-
let decimalPlaces: number;
|
|
60
|
-
if (span >= 1000) decimalPlaces = LARGE_SPAN_DECIMAL_PLACES;
|
|
61
|
-
else if (span >= 1) decimalPlaces = MEDIUM_SPAN_DECIMAL_PLACES;
|
|
62
|
-
else {
|
|
63
|
-
const decimalPlacesInSpan = Math.ceil(-Math.log10(span));
|
|
64
|
-
decimalPlaces = decimalPlacesInSpan + EXTRA_DECIMAL_PLACES;
|
|
65
|
-
}
|
|
66
|
-
const multiplier = 10 ** decimalPlaces;
|
|
67
|
-
return Math.round(value * multiplier) / multiplier;
|
|
68
|
-
};
|
|
69
|
-
|
|
70
18
|
const SIGNIFICANT_FIGURES = 5;
|
|
71
19
|
const MIN_SPAN_THRESHOLD = 1e-10;
|
|
72
20
|
|