@tryganit/core 0.1.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.
@@ -0,0 +1,25 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /**
5
+ * Evaluate a formula with named variables supplied as a JS object.
6
+ *
7
+ * `variables` must be a plain JS object mapping string keys to number/string/bool/null.
8
+ * Passing `undefined` or `null` is safe and is treated as no variables.
9
+ * Returns `{ value, type }` or `{ value: null, type: "error", error: "..." }`.
10
+ */
11
+ export function evaluate(formula: string, variables: any): any;
12
+
13
+ /**
14
+ * Return metadata for all built-in functions as a JS array.
15
+ *
16
+ * Each entry: `{ name, category, syntax, description }`.
17
+ */
18
+ export function list_functions(): any;
19
+
20
+ /**
21
+ * Validate a formula string without evaluating it.
22
+ *
23
+ * Returns `{ valid: true }` on success or `{ valid: false, error: "..." }` on failure.
24
+ */
25
+ export function validate(formula: string): any;
package/ganit_wasm.js ADDED
@@ -0,0 +1,9 @@
1
+ /* @ts-self-types="./ganit_wasm.d.ts" */
2
+ import * as wasm from "./ganit_wasm_bg.wasm";
3
+ import { __wbg_set_wasm } from "./ganit_wasm_bg.js";
4
+
5
+ __wbg_set_wasm(wasm);
6
+
7
+ export {
8
+ evaluate, list_functions, validate
9
+ } from "./ganit_wasm_bg.js";
@@ -0,0 +1,449 @@
1
+ /**
2
+ * Evaluate a formula with named variables supplied as a JS object.
3
+ *
4
+ * `variables` must be a plain JS object mapping string keys to number/string/bool/null.
5
+ * Passing `undefined` or `null` is safe and is treated as no variables.
6
+ * Returns `{ value, type }` or `{ value: null, type: "error", error: "..." }`.
7
+ * @param {string} formula
8
+ * @param {any} variables
9
+ * @returns {any}
10
+ */
11
+ export function evaluate(formula, variables) {
12
+ const ptr0 = passStringToWasm0(formula, wasm.__wbindgen_export, wasm.__wbindgen_export2);
13
+ const len0 = WASM_VECTOR_LEN;
14
+ const ret = wasm.evaluate(ptr0, len0, addHeapObject(variables));
15
+ return takeObject(ret);
16
+ }
17
+
18
+ /**
19
+ * Return metadata for all built-in functions as a JS array.
20
+ *
21
+ * Each entry: `{ name, category, syntax, description }`.
22
+ * @returns {any}
23
+ */
24
+ export function list_functions() {
25
+ const ret = wasm.list_functions();
26
+ return takeObject(ret);
27
+ }
28
+
29
+ /**
30
+ * Validate a formula string without evaluating it.
31
+ *
32
+ * Returns `{ valid: true }` on success or `{ valid: false, error: "..." }` on failure.
33
+ * @param {string} formula
34
+ * @returns {any}
35
+ */
36
+ export function validate(formula) {
37
+ const ptr0 = passStringToWasm0(formula, wasm.__wbindgen_export, wasm.__wbindgen_export2);
38
+ const len0 = WASM_VECTOR_LEN;
39
+ const ret = wasm.validate(ptr0, len0);
40
+ return takeObject(ret);
41
+ }
42
+ export function __wbg_Error_960c155d3d49e4c2(arg0, arg1) {
43
+ const ret = Error(getStringFromWasm0(arg0, arg1));
44
+ return addHeapObject(ret);
45
+ }
46
+ export function __wbg___wbindgen_bigint_get_as_i64_3d3aba5d616c6a51(arg0, arg1) {
47
+ const v = getObject(arg1);
48
+ const ret = typeof(v) === 'bigint' ? v : undefined;
49
+ getDataViewMemory0().setBigInt64(arg0 + 8 * 1, isLikeNone(ret) ? BigInt(0) : ret, true);
50
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
51
+ }
52
+ export function __wbg___wbindgen_boolean_get_6ea149f0a8dcc5ff(arg0) {
53
+ const v = getObject(arg0);
54
+ const ret = typeof(v) === 'boolean' ? v : undefined;
55
+ return isLikeNone(ret) ? 0xFFFFFF : ret ? 1 : 0;
56
+ }
57
+ export function __wbg___wbindgen_debug_string_ab4b34d23d6778bd(arg0, arg1) {
58
+ const ret = debugString(getObject(arg1));
59
+ const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
60
+ const len1 = WASM_VECTOR_LEN;
61
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
62
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
63
+ }
64
+ export function __wbg___wbindgen_in_a5d8b22e52b24dd1(arg0, arg1) {
65
+ const ret = getObject(arg0) in getObject(arg1);
66
+ return ret;
67
+ }
68
+ export function __wbg___wbindgen_is_bigint_ec25c7f91b4d9e93(arg0) {
69
+ const ret = typeof(getObject(arg0)) === 'bigint';
70
+ return ret;
71
+ }
72
+ export function __wbg___wbindgen_is_function_3baa9db1a987f47d(arg0) {
73
+ const ret = typeof(getObject(arg0)) === 'function';
74
+ return ret;
75
+ }
76
+ export function __wbg___wbindgen_is_object_63322ec0cd6ea4ef(arg0) {
77
+ const val = getObject(arg0);
78
+ const ret = typeof(val) === 'object' && val !== null;
79
+ return ret;
80
+ }
81
+ export function __wbg___wbindgen_is_string_6df3bf7ef1164ed3(arg0) {
82
+ const ret = typeof(getObject(arg0)) === 'string';
83
+ return ret;
84
+ }
85
+ export function __wbg___wbindgen_jsval_eq_d3465d8a07697228(arg0, arg1) {
86
+ const ret = getObject(arg0) === getObject(arg1);
87
+ return ret;
88
+ }
89
+ export function __wbg___wbindgen_jsval_loose_eq_cac3565e89b4134c(arg0, arg1) {
90
+ const ret = getObject(arg0) == getObject(arg1);
91
+ return ret;
92
+ }
93
+ export function __wbg___wbindgen_number_get_c7f42aed0525c451(arg0, arg1) {
94
+ const obj = getObject(arg1);
95
+ const ret = typeof(obj) === 'number' ? obj : undefined;
96
+ getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
97
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
98
+ }
99
+ export function __wbg___wbindgen_string_get_7ed5322991caaec5(arg0, arg1) {
100
+ const obj = getObject(arg1);
101
+ const ret = typeof(obj) === 'string' ? obj : undefined;
102
+ var ptr1 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_export, wasm.__wbindgen_export2);
103
+ var len1 = WASM_VECTOR_LEN;
104
+ getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
105
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
106
+ }
107
+ export function __wbg___wbindgen_throw_6b64449b9b9ed33c(arg0, arg1) {
108
+ throw new Error(getStringFromWasm0(arg0, arg1));
109
+ }
110
+ export function __wbg_call_14b169f759b26747() { return handleError(function (arg0, arg1) {
111
+ const ret = getObject(arg0).call(getObject(arg1));
112
+ return addHeapObject(ret);
113
+ }, arguments); }
114
+ export function __wbg_done_9158f7cc8751ba32(arg0) {
115
+ const ret = getObject(arg0).done;
116
+ return ret;
117
+ }
118
+ export function __wbg_entries_e0b73aa8571ddb56(arg0) {
119
+ const ret = Object.entries(getObject(arg0));
120
+ return addHeapObject(ret);
121
+ }
122
+ export function __wbg_get_1affdbdd5573b16a() { return handleError(function (arg0, arg1) {
123
+ const ret = Reflect.get(getObject(arg0), getObject(arg1));
124
+ return addHeapObject(ret);
125
+ }, arguments); }
126
+ export function __wbg_get_8360291721e2339f(arg0, arg1) {
127
+ const ret = getObject(arg0)[arg1 >>> 0];
128
+ return addHeapObject(ret);
129
+ }
130
+ export function __wbg_get_unchecked_17f53dad852b9588(arg0, arg1) {
131
+ const ret = getObject(arg0)[arg1 >>> 0];
132
+ return addHeapObject(ret);
133
+ }
134
+ export function __wbg_instanceof_ArrayBuffer_7c8433c6ed14ffe3(arg0) {
135
+ let result;
136
+ try {
137
+ result = getObject(arg0) instanceof ArrayBuffer;
138
+ } catch (_) {
139
+ result = false;
140
+ }
141
+ const ret = result;
142
+ return ret;
143
+ }
144
+ export function __wbg_instanceof_Map_1b76fd4635be43eb(arg0) {
145
+ let result;
146
+ try {
147
+ result = getObject(arg0) instanceof Map;
148
+ } catch (_) {
149
+ result = false;
150
+ }
151
+ const ret = result;
152
+ return ret;
153
+ }
154
+ export function __wbg_instanceof_Uint8Array_152ba1f289edcf3f(arg0) {
155
+ let result;
156
+ try {
157
+ result = getObject(arg0) instanceof Uint8Array;
158
+ } catch (_) {
159
+ result = false;
160
+ }
161
+ const ret = result;
162
+ return ret;
163
+ }
164
+ export function __wbg_isArray_c3109d14ffc06469(arg0) {
165
+ const ret = Array.isArray(getObject(arg0));
166
+ return ret;
167
+ }
168
+ export function __wbg_isSafeInteger_4fc213d1989d6d2a(arg0) {
169
+ const ret = Number.isSafeInteger(getObject(arg0));
170
+ return ret;
171
+ }
172
+ export function __wbg_iterator_013bc09ec998c2a7() {
173
+ const ret = Symbol.iterator;
174
+ return addHeapObject(ret);
175
+ }
176
+ export function __wbg_length_3d4ecd04bd8d22f1(arg0) {
177
+ const ret = getObject(arg0).length;
178
+ return ret;
179
+ }
180
+ export function __wbg_length_9f1775224cf1d815(arg0) {
181
+ const ret = getObject(arg0).length;
182
+ return ret;
183
+ }
184
+ export function __wbg_new_0c7403db6e782f19(arg0) {
185
+ const ret = new Uint8Array(getObject(arg0));
186
+ return addHeapObject(ret);
187
+ }
188
+ export function __wbg_new_34d45cc8e36aaead() {
189
+ const ret = new Map();
190
+ return addHeapObject(ret);
191
+ }
192
+ export function __wbg_new_682678e2f47e32bc() {
193
+ const ret = new Array();
194
+ return addHeapObject(ret);
195
+ }
196
+ export function __wbg_new_aa8d0fa9762c29bd() {
197
+ const ret = new Object();
198
+ return addHeapObject(ret);
199
+ }
200
+ export function __wbg_next_0340c4ae324393c3() { return handleError(function (arg0) {
201
+ const ret = getObject(arg0).next();
202
+ return addHeapObject(ret);
203
+ }, arguments); }
204
+ export function __wbg_next_7646edaa39458ef7(arg0) {
205
+ const ret = getObject(arg0).next;
206
+ return addHeapObject(ret);
207
+ }
208
+ export function __wbg_prototypesetcall_a6b02eb00b0f4ce2(arg0, arg1, arg2) {
209
+ Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), getObject(arg2));
210
+ }
211
+ export function __wbg_set_3bf1de9fab0cd644(arg0, arg1, arg2) {
212
+ getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
213
+ }
214
+ export function __wbg_set_6be42768c690e380(arg0, arg1, arg2) {
215
+ getObject(arg0)[takeObject(arg1)] = takeObject(arg2);
216
+ }
217
+ export function __wbg_set_fde2cec06c23692b(arg0, arg1, arg2) {
218
+ const ret = getObject(arg0).set(getObject(arg1), getObject(arg2));
219
+ return addHeapObject(ret);
220
+ }
221
+ export function __wbg_value_ee3a06f4579184fa(arg0) {
222
+ const ret = getObject(arg0).value;
223
+ return addHeapObject(ret);
224
+ }
225
+ export function __wbindgen_cast_0000000000000001(arg0) {
226
+ // Cast intrinsic for `F64 -> Externref`.
227
+ const ret = arg0;
228
+ return addHeapObject(ret);
229
+ }
230
+ export function __wbindgen_cast_0000000000000002(arg0) {
231
+ // Cast intrinsic for `I64 -> Externref`.
232
+ const ret = arg0;
233
+ return addHeapObject(ret);
234
+ }
235
+ export function __wbindgen_cast_0000000000000003(arg0, arg1) {
236
+ // Cast intrinsic for `Ref(String) -> Externref`.
237
+ const ret = getStringFromWasm0(arg0, arg1);
238
+ return addHeapObject(ret);
239
+ }
240
+ export function __wbindgen_cast_0000000000000004(arg0) {
241
+ // Cast intrinsic for `U64 -> Externref`.
242
+ const ret = BigInt.asUintN(64, arg0);
243
+ return addHeapObject(ret);
244
+ }
245
+ export function __wbindgen_object_drop_ref(arg0) {
246
+ takeObject(arg0);
247
+ }
248
+ function addHeapObject(obj) {
249
+ if (heap_next === heap.length) heap.push(heap.length + 1);
250
+ const idx = heap_next;
251
+ heap_next = heap[idx];
252
+
253
+ heap[idx] = obj;
254
+ return idx;
255
+ }
256
+
257
+ function debugString(val) {
258
+ // primitive types
259
+ const type = typeof val;
260
+ if (type == 'number' || type == 'boolean' || val == null) {
261
+ return `${val}`;
262
+ }
263
+ if (type == 'string') {
264
+ return `"${val}"`;
265
+ }
266
+ if (type == 'symbol') {
267
+ const description = val.description;
268
+ if (description == null) {
269
+ return 'Symbol';
270
+ } else {
271
+ return `Symbol(${description})`;
272
+ }
273
+ }
274
+ if (type == 'function') {
275
+ const name = val.name;
276
+ if (typeof name == 'string' && name.length > 0) {
277
+ return `Function(${name})`;
278
+ } else {
279
+ return 'Function';
280
+ }
281
+ }
282
+ // objects
283
+ if (Array.isArray(val)) {
284
+ const length = val.length;
285
+ let debug = '[';
286
+ if (length > 0) {
287
+ debug += debugString(val[0]);
288
+ }
289
+ for(let i = 1; i < length; i++) {
290
+ debug += ', ' + debugString(val[i]);
291
+ }
292
+ debug += ']';
293
+ return debug;
294
+ }
295
+ // Test for built-in
296
+ const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
297
+ let className;
298
+ if (builtInMatches && builtInMatches.length > 1) {
299
+ className = builtInMatches[1];
300
+ } else {
301
+ // Failed to match the standard '[object ClassName]'
302
+ return toString.call(val);
303
+ }
304
+ if (className == 'Object') {
305
+ // we're a user defined class or Object
306
+ // JSON.stringify avoids problems with cycles, and is generally much
307
+ // easier than looping through ownProperties of `val`.
308
+ try {
309
+ return 'Object(' + JSON.stringify(val) + ')';
310
+ } catch (_) {
311
+ return 'Object';
312
+ }
313
+ }
314
+ // errors
315
+ if (val instanceof Error) {
316
+ return `${val.name}: ${val.message}\n${val.stack}`;
317
+ }
318
+ // TODO we could test for more things here, like `Set`s and `Map`s.
319
+ return className;
320
+ }
321
+
322
+ function dropObject(idx) {
323
+ if (idx < 1028) return;
324
+ heap[idx] = heap_next;
325
+ heap_next = idx;
326
+ }
327
+
328
+ function getArrayU8FromWasm0(ptr, len) {
329
+ ptr = ptr >>> 0;
330
+ return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
331
+ }
332
+
333
+ let cachedDataViewMemory0 = null;
334
+ function getDataViewMemory0() {
335
+ if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
336
+ cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
337
+ }
338
+ return cachedDataViewMemory0;
339
+ }
340
+
341
+ function getStringFromWasm0(ptr, len) {
342
+ ptr = ptr >>> 0;
343
+ return decodeText(ptr, len);
344
+ }
345
+
346
+ let cachedUint8ArrayMemory0 = null;
347
+ function getUint8ArrayMemory0() {
348
+ if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
349
+ cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
350
+ }
351
+ return cachedUint8ArrayMemory0;
352
+ }
353
+
354
+ function getObject(idx) { return heap[idx]; }
355
+
356
+ function handleError(f, args) {
357
+ try {
358
+ return f.apply(this, args);
359
+ } catch (e) {
360
+ wasm.__wbindgen_export3(addHeapObject(e));
361
+ }
362
+ }
363
+
364
+ let heap = new Array(1024).fill(undefined);
365
+ heap.push(undefined, null, true, false);
366
+
367
+ let heap_next = heap.length;
368
+
369
+ function isLikeNone(x) {
370
+ return x === undefined || x === null;
371
+ }
372
+
373
+ function passStringToWasm0(arg, malloc, realloc) {
374
+ if (realloc === undefined) {
375
+ const buf = cachedTextEncoder.encode(arg);
376
+ const ptr = malloc(buf.length, 1) >>> 0;
377
+ getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
378
+ WASM_VECTOR_LEN = buf.length;
379
+ return ptr;
380
+ }
381
+
382
+ let len = arg.length;
383
+ let ptr = malloc(len, 1) >>> 0;
384
+
385
+ const mem = getUint8ArrayMemory0();
386
+
387
+ let offset = 0;
388
+
389
+ for (; offset < len; offset++) {
390
+ const code = arg.charCodeAt(offset);
391
+ if (code > 0x7F) break;
392
+ mem[ptr + offset] = code;
393
+ }
394
+ if (offset !== len) {
395
+ if (offset !== 0) {
396
+ arg = arg.slice(offset);
397
+ }
398
+ ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
399
+ const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
400
+ const ret = cachedTextEncoder.encodeInto(arg, view);
401
+
402
+ offset += ret.written;
403
+ ptr = realloc(ptr, len, offset, 1) >>> 0;
404
+ }
405
+
406
+ WASM_VECTOR_LEN = offset;
407
+ return ptr;
408
+ }
409
+
410
+ function takeObject(idx) {
411
+ const ret = getObject(idx);
412
+ dropObject(idx);
413
+ return ret;
414
+ }
415
+
416
+ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
417
+ cachedTextDecoder.decode();
418
+ const MAX_SAFARI_DECODE_BYTES = 2146435072;
419
+ let numBytesDecoded = 0;
420
+ function decodeText(ptr, len) {
421
+ numBytesDecoded += len;
422
+ if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
423
+ cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
424
+ cachedTextDecoder.decode();
425
+ numBytesDecoded = len;
426
+ }
427
+ return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
428
+ }
429
+
430
+ const cachedTextEncoder = new TextEncoder();
431
+
432
+ if (!('encodeInto' in cachedTextEncoder)) {
433
+ cachedTextEncoder.encodeInto = function (arg, view) {
434
+ const buf = cachedTextEncoder.encode(arg);
435
+ view.set(buf);
436
+ return {
437
+ read: arg.length,
438
+ written: buf.length
439
+ };
440
+ };
441
+ }
442
+
443
+ let WASM_VECTOR_LEN = 0;
444
+
445
+
446
+ let wasm;
447
+ export function __wbg_set_wasm(val) {
448
+ wasm = val;
449
+ }
Binary file
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@tryganit/core",
3
+ "type": "module",
4
+ "description": "Spreadsheet formula engine for the browser — Excel-compatible formula evaluator compiled to WebAssembly",
5
+ "version": "0.1.1",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/tryganit/ganit-core"
10
+ },
11
+ "files": [
12
+ "ganit_wasm_bg.wasm",
13
+ "ganit_wasm.js",
14
+ "ganit_wasm_bg.js",
15
+ "ganit_wasm.d.ts"
16
+ ],
17
+ "main": "ganit_wasm.js",
18
+ "types": "ganit_wasm.d.ts",
19
+ "sideEffects": [
20
+ "./ganit_wasm.js",
21
+ "./snippets/*"
22
+ ],
23
+ "keywords": [
24
+ "spreadsheet",
25
+ "formula",
26
+ "excel",
27
+ "wasm",
28
+ "evaluator"
29
+ ]
30
+ }