@byte_fluffy/nexra-sdk 0.1.0-alpha.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/README.md +35 -0
- package/dist/catalog-mytCeh5t.d.ts +313 -0
- package/dist/catalog.d.ts +1 -0
- package/dist/catalog.js +10 -0
- package/dist/chunk-ADKRTC6X.js +1512 -0
- package/dist/chunk-FCH5IQP5.js +34 -0
- package/dist/decimal.d.ts +4 -0
- package/dist/decimal.js +8 -0
- package/dist/index.d.ts +1006 -0
- package/dist/index.js +1428 -0
- package/package.json +44 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// src/decimal.ts
|
|
2
|
+
var DECIMAL = /^(?:0|[1-9]\d*)(?:\.(\d{1,18}))?$/;
|
|
3
|
+
function multiplyDecimal(value, multiplier) {
|
|
4
|
+
const left = parseDecimal(value);
|
|
5
|
+
const right = parseDecimal(multiplier);
|
|
6
|
+
return formatDecimal(left.units * right.units, left.scale + right.scale);
|
|
7
|
+
}
|
|
8
|
+
function ceilDecimal(value, scale) {
|
|
9
|
+
if (!Number.isSafeInteger(scale) || scale < 0 || scale > 18) {
|
|
10
|
+
throw new Error("Decimal scale must be an integer between 0 and 18");
|
|
11
|
+
}
|
|
12
|
+
const parsed = parseDecimal(value);
|
|
13
|
+
if (parsed.scale <= scale) return formatDecimal(parsed.units, parsed.scale);
|
|
14
|
+
const divisor = 10n ** BigInt(parsed.scale - scale);
|
|
15
|
+
return formatDecimal((parsed.units + divisor - 1n) / divisor, scale);
|
|
16
|
+
}
|
|
17
|
+
function parseDecimal(value) {
|
|
18
|
+
const match = DECIMAL.exec(value);
|
|
19
|
+
if (!match) throw new Error(`Invalid non-negative decimal: ${value}`);
|
|
20
|
+
const fraction = match[1] ?? "";
|
|
21
|
+
return { units: BigInt(value.replace(".", "")), scale: fraction.length };
|
|
22
|
+
}
|
|
23
|
+
function formatDecimal(units, scale) {
|
|
24
|
+
if (scale === 0) return units.toString();
|
|
25
|
+
const digits = units.toString().padStart(scale + 1, "0");
|
|
26
|
+
const whole = digits.slice(0, -scale);
|
|
27
|
+
const fraction = digits.slice(-scale).replace(/0+$/, "");
|
|
28
|
+
return fraction ? `${whole}.${fraction}` : whole;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
multiplyDecimal,
|
|
33
|
+
ceilDecimal
|
|
34
|
+
};
|