@c9up/atom 0.1.3 → 0.1.4

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/math.js ADDED
@@ -0,0 +1,187 @@
1
+ export function parseDecimal(input) {
2
+ const s = input.trim();
3
+ if (!s) {
4
+ throw new Error("Invalid decimal: empty string");
5
+ }
6
+ let sign = 1n;
7
+ let body = s;
8
+ if (body.startsWith("-")) {
9
+ sign = -1n;
10
+ body = body.slice(1);
11
+ }
12
+ else if (body.startsWith("+")) {
13
+ body = body.slice(1);
14
+ }
15
+ const parts = body.split(".");
16
+ if (parts.length > 2) {
17
+ throw new Error(`Invalid decimal: ${input}`);
18
+ }
19
+ const whole = parts[0] ?? "";
20
+ const frac = parts[1] ?? "";
21
+ if (!/^\d*$/.test(whole) || !/^\d*$/.test(frac)) {
22
+ throw new Error(`Invalid decimal: ${input}`);
23
+ }
24
+ const digits = `${whole}${frac}` || "0";
25
+ const int = BigInt(digits) * sign;
26
+ return { int, scale: frac.length };
27
+ }
28
+ export function formatDecimal(int, scale) {
29
+ if (scale === 0)
30
+ return int.toString();
31
+ const negative = int < 0n;
32
+ let s = (negative ? -int : int).toString();
33
+ if (s.length <= scale) {
34
+ s = `${"0".repeat(scale + 1 - s.length)}${s}`;
35
+ }
36
+ const split = s.length - scale;
37
+ const whole = s.slice(0, split);
38
+ let frac = s.slice(split);
39
+ frac = frac.replace(/0+$/, "");
40
+ let out = frac ? `${whole}.${frac}` : whole;
41
+ if (negative && out !== "0")
42
+ out = `-${out}`;
43
+ return out;
44
+ }
45
+ export function addTs(a, b) {
46
+ const da = parseDecimal(a);
47
+ const db = parseDecimal(b);
48
+ const [ai, bi, scale] = alignScale(da, db);
49
+ return formatDecimal(ai + bi, scale);
50
+ }
51
+ export function subTs(a, b) {
52
+ const da = parseDecimal(a);
53
+ const db = parseDecimal(b);
54
+ const [ai, bi, scale] = alignScale(da, db);
55
+ return formatDecimal(ai - bi, scale);
56
+ }
57
+ export function mulTs(a, b) {
58
+ const da = parseDecimal(a);
59
+ const db = parseDecimal(b);
60
+ return formatDecimal(da.int * db.int, da.scale + db.scale);
61
+ }
62
+ export function divTs(a, b, precision) {
63
+ const da = parseDecimal(a);
64
+ const db = parseDecimal(b);
65
+ if (db.int === 0n) {
66
+ throw new Error("Division by zero");
67
+ }
68
+ const numerator = da.int * pow10BigInt(precision + db.scale);
69
+ const denominator = db.int * pow10BigInt(da.scale);
70
+ const q = numerator / denominator;
71
+ return formatDecimal(q, precision);
72
+ }
73
+ export function cmpTs(a, b) {
74
+ const da = parseDecimal(a);
75
+ const db = parseDecimal(b);
76
+ const [ai, bi] = alignScale(da, db);
77
+ if (ai < bi)
78
+ return -1;
79
+ if (ai > bi)
80
+ return 1;
81
+ return 0;
82
+ }
83
+ /**
84
+ * Align two parsed decimals to the same scale by multiplying the less-precise
85
+ * one by `10^(scaleDelta)`. Exported so `Decimal.ts` can reuse it — previously
86
+ * both files had their own copy under different names (`alignScale` vs
87
+ * `alignScaleParts`), which is a maintenance hazard.
88
+ */
89
+ export function alignScale(a, b) {
90
+ if (a.scale === b.scale)
91
+ return [a.int, b.int, a.scale];
92
+ if (a.scale > b.scale) {
93
+ const factor = pow10BigInt(a.scale - b.scale);
94
+ return [a.int, b.int * factor, a.scale];
95
+ }
96
+ const factor = pow10BigInt(b.scale - a.scale);
97
+ return [a.int * factor, b.int, b.scale];
98
+ }
99
+ /** Exact `10^exp` as a `bigint`. Exported — single definition for the whole TS side. */
100
+ export function pow10BigInt(exp) {
101
+ let acc = 1n;
102
+ for (let i = 0; i < exp; i++)
103
+ acc *= 10n;
104
+ return acc;
105
+ }
106
+ /**
107
+ * Pure-TS modulo — fallback for when the native engine is unavailable. The
108
+ * Rust `rem` path should be preferred when `isNativeAvailable()`.
109
+ */
110
+ export function modTs(a, b) {
111
+ const da = parseDecimal(a);
112
+ const db = parseDecimal(b);
113
+ if (db.int === 0n) {
114
+ throw new Error("Division by zero");
115
+ }
116
+ const [ai, bi, scale] = alignScale(da, db);
117
+ return formatDecimal(ai % bi, scale);
118
+ }
119
+ /**
120
+ * Pure-TS integer exponentiation with truncating div for negative exponents.
121
+ * Mirrors the Rust `pow` contract so the two paths produce identical results.
122
+ */
123
+ export function powTs(a, exp, precision) {
124
+ if (!Number.isInteger(exp)) {
125
+ throw new Error(`Invalid exponent: ${exp}`);
126
+ }
127
+ if (exp === 0)
128
+ return "1";
129
+ if (exp < 0) {
130
+ return divTs("1", powTs(a, -exp, precision), precision);
131
+ }
132
+ const base = parseDecimal(a);
133
+ let resultInt = 1n;
134
+ let resultScale = 0;
135
+ let currentInt = base.int;
136
+ let currentScale = base.scale;
137
+ let e = exp;
138
+ while (e > 0) {
139
+ if (e & 1) {
140
+ resultInt *= currentInt;
141
+ resultScale += currentScale;
142
+ }
143
+ e >>= 1;
144
+ if (e > 0) {
145
+ currentInt *= currentInt;
146
+ currentScale *= 2;
147
+ }
148
+ }
149
+ return formatDecimal(resultInt, resultScale);
150
+ }
151
+ /**
152
+ * Pure-TS integer square root via Newton's iteration on BigInt, scaled to
153
+ * produce `precision` fractional digits. Truncates toward zero; rounding
154
+ * modes are applied by the caller.
155
+ */
156
+ export function sqrtTs(a, precision) {
157
+ const parsed = parseDecimal(a);
158
+ if (parsed.int < 0n) {
159
+ throw new Error("Cannot compute sqrt of a negative decimal");
160
+ }
161
+ if (parsed.int === 0n)
162
+ return "0";
163
+ const factorExp = 2 * precision - parsed.scale;
164
+ let radicand = parsed.int;
165
+ if (factorExp >= 0) {
166
+ radicand *= pow10BigInt(factorExp);
167
+ }
168
+ else {
169
+ radicand /= pow10BigInt(-factorExp);
170
+ }
171
+ const root = bigintSqrt(radicand);
172
+ return formatDecimal(root, precision);
173
+ }
174
+ function bigintSqrt(value) {
175
+ if (value < 0n)
176
+ throw new Error("Cannot compute sqrt of negative bigint");
177
+ if (value < 2n)
178
+ return value;
179
+ let x0 = value;
180
+ let x1 = (x0 + value / x0) / 2n;
181
+ while (x1 < x0) {
182
+ x0 = x1;
183
+ x1 = (x0 + value / x0) / 2n;
184
+ }
185
+ return x0;
186
+ }
187
+ //# sourceMappingURL=math.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"math.js","sourceRoot":"","sources":["../src/math.ts"],"names":[],"mappings":"AAKA,MAAM,UAAU,YAAY,CAAC,KAAa;IACzC,MAAM,CAAC,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IACvB,IAAI,CAAC,CAAC,EAAE,CAAC;QACR,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;IAClD,CAAC;IAED,IAAI,IAAI,GAAG,EAAE,CAAC;IACd,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QAC1B,IAAI,GAAG,CAAC,EAAE,CAAC;QACX,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACtB,CAAC;SAAM,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QACjC,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IACtB,CAAC;IAED,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC9B,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,oBAAoB,KAAK,EAAE,CAAC,CAAC;IAC9C,CAAC;IACD,MAAM,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAC7B,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;IAC5B,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACjD,MAAM,IAAI,KAAK,CAAC,oBAAoB,KAAK,EAAE,CAAC,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IAAI,WAAW,CAAC,SAAS,CAAC,CAAC;IACpC,CAAC;SAAM,CAAC;QACP,QAAQ,IAAI,WAAW,CAAC,CAAC,SAAS,CAAC,CAAC;IACrC,CAAC;IACD,MAAM,IAAI,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;IAClC,OAAO,aAAa,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;AACvC,CAAC;AAED,SAAS,UAAU,CAAC,KAAa;IAChC,IAAI,KAAK,GAAG,EAAE;QAAE,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC1E,IAAI,KAAK,GAAG,EAAE;QAAE,OAAO,KAAK,CAAC;IAC7B,IAAI,EAAE,GAAG,KAAK,CAAC;IACf,IAAI,EAAE,GAAG,CAAC,EAAE,GAAG,KAAK,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC;IAChC,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC;QAChB,EAAE,GAAG,EAAE,CAAC;QACR,EAAE,GAAG,CAAC,EAAE,GAAG,KAAK,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC;IAC7B,CAAC;IACD,OAAO,EAAE,CAAC;AACX,CAAC"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Universal engine loader — auto-detects Node (NAPI) vs Browser (WASM).
3
+ *
4
+ * - **Node**: loads the prebuilt `.node` binary via `createRequire` (sync, fast)
5
+ * - **Browser**: loads the `.wasm` binary via the wasm-pack JS glue (async init on
6
+ * first module import via top-level await, then sync function calls)
7
+ *
8
+ * The consumer imports `nativeAtom()` and gets the same `NativeAtom` interface
9
+ * regardless of the environment. No conditional imports needed at the call site.
10
+ */
11
+ export interface NativeAtom {
12
+ add(a: string, b: string): string;
13
+ sub(a: string, b: string): string;
14
+ mul(a: string, b: string): string;
15
+ div(a: string, b: string, precision: number): string;
16
+ rem(a: string, b: string): string;
17
+ pow(a: string, exp: number, precision: number): string;
18
+ sqrt(a: string, precision: number): string;
19
+ cmp(a: string, b: string): number;
20
+ }
21
+ /** Whether the native engine (NAPI or WASM) loaded successfully. */
22
+ export declare function isNativeAvailable(): boolean;
23
+ export declare function nativeAtom(): NativeAtom;
24
+ export declare function __overrideNativeForTesting(impl: NativeAtom | null | undefined): void;
25
+ //# sourceMappingURL=native.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"native.d.ts","sourceRoot":"","sources":["../src/native.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,MAAM,WAAW,UAAU;IAC1B,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAClC,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAClC,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAClC,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAAC;IACrD,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IAClC,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAAC;IACvD,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAAC;IAC3C,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CAClC;AAgDD,oEAAoE;AACpE,wBAAgB,iBAAiB,IAAI,OAAO,CAI3C;AAED,wBAAgB,UAAU,IAAI,UAAU,CAmBvC;AAKD,wBAAgB,0BAA0B,CACzC,IAAI,EAAE,UAAU,GAAG,IAAI,GAAG,SAAS,GACjC,IAAI,CAEN"}
package/dist/native.js ADDED
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Universal engine loader — auto-detects Node (NAPI) vs Browser (WASM).
3
+ *
4
+ * - **Node**: loads the prebuilt `.node` binary via `createRequire` (sync, fast)
5
+ * - **Browser**: loads the `.wasm` binary via the wasm-pack JS glue (async init on
6
+ * first module import via top-level await, then sync function calls)
7
+ *
8
+ * The consumer imports `nativeAtom()` and gets the same `NativeAtom` interface
9
+ * regardless of the environment. No conditional imports needed at the call site.
10
+ */
11
+ let native;
12
+ let loadError;
13
+ const isNode = typeof globalThis.process !== "undefined" &&
14
+ typeof globalThis.process.versions?.node === "string";
15
+ if (isNode) {
16
+ // Node path: load NAPI binary (sync).
17
+ try {
18
+ const { createRequire } = await import("node:module");
19
+ const { dirname, join } = await import("node:path");
20
+ const { fileURLToPath } = await import("node:url");
21
+ const { arch, platform } = await import("node:process");
22
+ const nodeRequire = createRequire(import.meta.url);
23
+ const currentDir = dirname(fileURLToPath(import.meta.url));
24
+ const platformMap = {
25
+ "linux-x64": "linux-x64-gnu",
26
+ "linux-arm64": "linux-arm64-gnu",
27
+ "darwin-x64": "darwin-x64",
28
+ "darwin-arm64": "darwin-arm64",
29
+ "win32-x64": "win32-x64-msvc",
30
+ };
31
+ const suffix = platformMap[`${platform}-${arch}`];
32
+ if (suffix) {
33
+ native = nodeRequire(join(currentDir, `../index.${suffix}.node`));
34
+ }
35
+ }
36
+ catch (e) {
37
+ loadError = e;
38
+ }
39
+ }
40
+ else {
41
+ // Browser path: load WASM (async init, then sync calls).
42
+ try {
43
+ const wasm = await import("../wasm/atom_engine_wasm.js");
44
+ await wasm.default();
45
+ native = wasm;
46
+ }
47
+ catch (e) {
48
+ loadError = e;
49
+ }
50
+ }
51
+ /** Whether the native engine (NAPI or WASM) loaded successfully. */
52
+ export function isNativeAvailable() {
53
+ if (overrideNative === null)
54
+ return false;
55
+ if (overrideNative !== undefined)
56
+ return true;
57
+ return native !== undefined;
58
+ }
59
+ export function nativeAtom() {
60
+ if (overrideNative !== undefined) {
61
+ if (overrideNative === null) {
62
+ throw new Error("[ATOM_NAPI_DISABLED] Native engine is disabled (test override)");
63
+ }
64
+ return overrideNative;
65
+ }
66
+ if (!native) {
67
+ throw new Error(`[ATOM_ENGINE_NOT_FOUND] Decimal engine not available.\n` +
68
+ ` Environment: ${isNode ? "Node" : "Browser"}\n` +
69
+ ` Reason: ${loadError ?? "binary not found"}\n` +
70
+ ` Fix (Node): cd packages/atom && pnpm build:napi\n` +
71
+ ` Fix (Browser): cd packages/atom && pnpm build:wasm`);
72
+ }
73
+ return native;
74
+ }
75
+ // Test override (unchanged from before)
76
+ let overrideNative;
77
+ export function __overrideNativeForTesting(impl) {
78
+ overrideNative = impl;
79
+ }
80
+ //# sourceMappingURL=native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"native.js","sourceRoot":"","sources":["../src/native.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAaH,IAAI,MAA8B,CAAC;AACnC,IAAI,SAAkB,CAAC;AAEvB,MAAM,MAAM,GACX,OAAO,UAAU,CAAC,OAAO,KAAK,WAAW;IACzC,OAAO,UAAU,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,KAAK,QAAQ,CAAC;AAEvD,IAAI,MAAM,EAAE,CAAC;IACZ,sCAAsC;IACtC,IAAI,CAAC;QACJ,MAAM,EAAE,aAAa,EAAE,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,CAAC;QACtD,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,WAAW,CAAC,CAAC;QACpD,MAAM,EAAE,aAAa,EAAE,GAAG,MAAM,MAAM,CAAC,UAAU,CAAC,CAAC;QACnD,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,MAAM,MAAM,CAAC,cAAc,CAAC,CAAC;QAExD,MAAM,WAAW,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACnD,MAAM,UAAU,GAAG,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC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,CACd,yDAAyD;YACxD,kBAAkB,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,IAAI;YACjD,aAAa,SAAS,IAAI,kBAAkB,IAAI;YAChD,qDAAqD;YACrD,sDAAsD,CACvD,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AACf,CAAC;AAED,wCAAwC;AACxC,IAAI,cAA6C,CAAC;AAElD,MAAM,UAAU,0BAA0B,CACzC,IAAmC;IAEnC,cAAc,GAAG,IAAI,CAAC;AACvB,CAAC"}
Binary file
Binary file
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@c9up/atom",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Atom — exact decimal arithmetic for the Ream ecosystem (TypeScript + Rust N-API)",
5
5
  "license": "MIT",
6
6
  "type": "module",