scriptable 0.0.6 → 0.1.0
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/.claude/settings.local.json +10 -0
- package/README.md +139 -0
- package/dist/binary-ops.d.ts +2 -0
- package/dist/binary-ops.js +16 -0
- package/dist/builtin-functions.d.ts +3 -0
- package/dist/builtin-functions.js +151 -0
- package/dist/environment.d.ts +16 -0
- package/dist/environment.js +111 -0
- package/dist/evaluate.d.ts +5 -0
- package/dist/evaluate.js +232 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +26 -0
- package/dist/logic-ops.d.ts +2 -0
- package/dist/logic-ops.js +5 -0
- package/package.json +17 -50
- package/src/__tests__/{break-out.js → break-out.test.ts} +3 -14
- package/src/__tests__/{builtin-functions-date.js → builtin-functions-date.test.ts} +30 -11
- package/src/__tests__/{builtin-functions.js → builtin-functions.test.ts} +2 -3
- package/src/__tests__/{expressions.js → expressions.test.ts} +2 -3
- package/src/__tests__/{functions.js → functions.test.ts} +7 -8
- package/src/__tests__/{logic.js → logic.test.ts} +2 -3
- package/src/__tests__/new-features.test.ts +158 -0
- package/src/__tests__/security.test.ts +399 -0
- package/src/__tests__/{variables.js → variables.test.ts} +3 -6
- package/src/binary-ops.ts +18 -0
- package/src/builtin-functions.ts +177 -0
- package/src/environment.ts +132 -0
- package/src/evaluate.ts +275 -0
- package/src/index.ts +42 -0
- package/src/logic-ops.ts +6 -0
- package/tsconfig.json +16 -0
- package/.npmignore +0 -1
- package/src/__tests__/index.js +0 -85
- package/src/binary-ops.js +0 -13
- package/src/builtin-functions.js +0 -58
- package/src/environment.js +0 -73
- package/src/evaluate.js +0 -174
- package/src/index.js +0 -26
- package/src/logic-ops.js +0 -6
- package/yarn.lock +0 -2632
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import runner from "../index.js";
|
|
3
|
+
|
|
4
|
+
const run = runner();
|
|
5
|
+
|
|
6
|
+
// ==========================================================================
|
|
7
|
+
// 1. Sandbox escape — prototype chain to Function constructor
|
|
8
|
+
// ==========================================================================
|
|
9
|
+
|
|
10
|
+
describe("sandbox escape: prototype chain", () => {
|
|
11
|
+
it("blocks obj.constructor", () => {
|
|
12
|
+
const code = `a = {}; a.constructor`;
|
|
13
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("blocks obj.constructor.constructor (Function access)", () => {
|
|
17
|
+
const code = `a = {}; a.constructor.constructor`;
|
|
18
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("blocks array.constructor.constructor", () => {
|
|
22
|
+
const code = `a = [1,2]; a.constructor.constructor("return this")()`;
|
|
23
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("blocks string.constructor", () => {
|
|
27
|
+
const code = `a = "hello"; a.constructor`;
|
|
28
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("blocks number.constructor", () => {
|
|
32
|
+
// Wrap in parens to avoid parser ambiguity
|
|
33
|
+
const code = `a = 42; a.constructor`;
|
|
34
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("blocks function.constructor (reaches Function)", () => {
|
|
38
|
+
const code = `
|
|
39
|
+
f = (x) => x;
|
|
40
|
+
f.constructor("return this")();
|
|
41
|
+
`;
|
|
42
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("blocks constructor via computed property", () => {
|
|
46
|
+
const code = `a = {}; a["constructor"]`;
|
|
47
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("blocks constructor via computed string concatenation", () => {
|
|
51
|
+
const code = `a = {}; a["const" + "ructor"]`;
|
|
52
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it("blocks constructor via variable indirection", () => {
|
|
56
|
+
const code = `
|
|
57
|
+
a = {};
|
|
58
|
+
key = "constructor";
|
|
59
|
+
a[key];
|
|
60
|
+
`;
|
|
61
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// ==========================================================================
|
|
66
|
+
// 2. Sandbox escape — __proto__ access
|
|
67
|
+
// ==========================================================================
|
|
68
|
+
|
|
69
|
+
describe("sandbox escape: __proto__", () => {
|
|
70
|
+
it("blocks __proto__ read", () => {
|
|
71
|
+
const code = `a = {}; a.__proto__`;
|
|
72
|
+
expect(() => run(code)).toThrow("Access to property '__proto__' is not allowed");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("blocks __proto__ computed read", () => {
|
|
76
|
+
const code = `a = {}; a["__proto__"]`;
|
|
77
|
+
expect(() => run(code)).toThrow("Access to property '__proto__' is not allowed");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("blocks __proto__ assignment", () => {
|
|
81
|
+
const code = `a = {}; a.__proto__ = {}`;
|
|
82
|
+
expect(() => run(code)).toThrow("Access to property '__proto__' is not allowed");
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// ==========================================================================
|
|
87
|
+
// 3. Sandbox escape — prototype access
|
|
88
|
+
// ==========================================================================
|
|
89
|
+
|
|
90
|
+
describe("sandbox escape: prototype", () => {
|
|
91
|
+
it("blocks .prototype access", () => {
|
|
92
|
+
const code = `
|
|
93
|
+
a = {};
|
|
94
|
+
f = a.constructor;
|
|
95
|
+
`;
|
|
96
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("blocks .prototype via computed", () => {
|
|
100
|
+
const code = `a = {}; a["prototype"]`;
|
|
101
|
+
expect(() => run(code)).toThrow("Access to property 'prototype' is not allowed");
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
// ==========================================================================
|
|
106
|
+
// 4. Prototype pollution via deep assignment
|
|
107
|
+
// ==========================================================================
|
|
108
|
+
|
|
109
|
+
describe("prototype pollution via deep path assignment", () => {
|
|
110
|
+
it("blocks constructor in deep assignment path", () => {
|
|
111
|
+
const code = `foo.constructor.prototype.polluted = "yes"`;
|
|
112
|
+
expect(() => run(code, { foo: {} })).toThrow(
|
|
113
|
+
"Access to property 'constructor' is not allowed"
|
|
114
|
+
);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("blocks __proto__ in deep assignment path", () => {
|
|
118
|
+
const code = `foo.__proto__.polluted = "yes"`;
|
|
119
|
+
expect(() => run(code, { foo: {} })).toThrow(
|
|
120
|
+
"Access to property '__proto__' is not allowed"
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("does not pollute Object.prototype even if attempted", () => {
|
|
125
|
+
// Verify no pollution leaked from prior tests
|
|
126
|
+
expect((Object.prototype as any).polluted).toBeUndefined();
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// ==========================================================================
|
|
131
|
+
// 5. Sandbox escape — legacy property descriptors
|
|
132
|
+
// ==========================================================================
|
|
133
|
+
|
|
134
|
+
describe("sandbox escape: legacy descriptor methods", () => {
|
|
135
|
+
it("blocks __defineGetter__", () => {
|
|
136
|
+
const code = `a = {}; a.__defineGetter__`;
|
|
137
|
+
expect(() => run(code)).toThrow(
|
|
138
|
+
"Access to property '__defineGetter__' is not allowed"
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("blocks __defineSetter__", () => {
|
|
143
|
+
const code = `a = {}; a.__defineSetter__`;
|
|
144
|
+
expect(() => run(code)).toThrow(
|
|
145
|
+
"Access to property '__defineSetter__' is not allowed"
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("blocks __lookupGetter__", () => {
|
|
150
|
+
const code = `a = {}; a.__lookupGetter__`;
|
|
151
|
+
expect(() => run(code)).toThrow(
|
|
152
|
+
"Access to property '__lookupGetter__' is not allowed"
|
|
153
|
+
);
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it("blocks __lookupSetter__", () => {
|
|
157
|
+
const code = `a = {}; a.__lookupSetter__`;
|
|
158
|
+
expect(() => run(code)).toThrow(
|
|
159
|
+
"Access to property '__lookupSetter__' is not allowed"
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// ==========================================================================
|
|
165
|
+
// 6. Sandbox escape — this / arguments / globals
|
|
166
|
+
// ==========================================================================
|
|
167
|
+
|
|
168
|
+
describe("sandbox escape: forbidden expressions", () => {
|
|
169
|
+
it("blocks this", () => {
|
|
170
|
+
const code = `this`;
|
|
171
|
+
expect(() => run(code)).toThrow("Cannot evaluate ThisExpression");
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("blocks arguments keyword", () => {
|
|
175
|
+
const code = `
|
|
176
|
+
function f() { return arguments; }
|
|
177
|
+
f();
|
|
178
|
+
`;
|
|
179
|
+
expect(() => run(code)).toThrow('"arguments" is not defined');
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it("cannot access process", () => {
|
|
183
|
+
const code = `process`;
|
|
184
|
+
expect(() => run(code)).toThrow('"process" is not defined');
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("cannot access require", () => {
|
|
188
|
+
const code = `require("fs")`;
|
|
189
|
+
expect(() => run(code)).toThrow("require is not a function");
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("cannot access globalThis", () => {
|
|
193
|
+
const code = `globalThis`;
|
|
194
|
+
expect(() => run(code)).toThrow('"globalThis" is not defined');
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("cannot access eval", () => {
|
|
198
|
+
const code = `eval("1+1")`;
|
|
199
|
+
expect(() => run(code)).toThrow("eval is not a function");
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it("cannot access Function", () => {
|
|
203
|
+
const code = `Function("return 1")`;
|
|
204
|
+
expect(() => run(code)).toThrow("Function is not a function");
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it("blocks import()", () => {
|
|
208
|
+
const code = `import("fs")`;
|
|
209
|
+
expect(() => run(code)).toThrow();
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it("blocks new expressions", () => {
|
|
213
|
+
const code = `new Array(10)`;
|
|
214
|
+
expect(() => run(code)).toThrow("Cannot evaluate NewExpression");
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// ==========================================================================
|
|
219
|
+
// 7. DoS — infinite recursion
|
|
220
|
+
// ==========================================================================
|
|
221
|
+
|
|
222
|
+
describe("DoS: infinite recursion", () => {
|
|
223
|
+
// V8's call stack limit catches deep recursion before our step counter
|
|
224
|
+
// (each interpreter call creates many real stack frames). Either mechanism
|
|
225
|
+
// is an acceptable guard — the important thing is execution stops.
|
|
226
|
+
|
|
227
|
+
it("stops infinite recursive function", () => {
|
|
228
|
+
const code = `
|
|
229
|
+
function f() { return f(); }
|
|
230
|
+
f();
|
|
231
|
+
`;
|
|
232
|
+
expect(() => run(code)).toThrow();
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it("stops mutual recursion", () => {
|
|
236
|
+
const code = `
|
|
237
|
+
function a() { return b(); }
|
|
238
|
+
function b() { return a(); }
|
|
239
|
+
a();
|
|
240
|
+
`;
|
|
241
|
+
expect(() => run(code)).toThrow();
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it("stops deep self-calling arrow", () => {
|
|
245
|
+
const code = `
|
|
246
|
+
f = (n) => f(n);
|
|
247
|
+
f(1);
|
|
248
|
+
`;
|
|
249
|
+
expect(() => run(code)).toThrow();
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it("step counter catches recursion with low limit", () => {
|
|
253
|
+
const tightRun = runner({ maxSteps: 50 });
|
|
254
|
+
const code = `
|
|
255
|
+
function f(n) { if (n > 0) { return f(n - 1); } return 0; }
|
|
256
|
+
f(100);
|
|
257
|
+
`;
|
|
258
|
+
expect(() => tightRun(code)).toThrow("Execution limit exceeded");
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
// ==========================================================================
|
|
263
|
+
// 8. DoS — execution limit configurability
|
|
264
|
+
// ==========================================================================
|
|
265
|
+
|
|
266
|
+
describe("DoS: execution limits", () => {
|
|
267
|
+
it("respects custom maxSteps", () => {
|
|
268
|
+
const tightRun = runner({ maxSteps: 10 });
|
|
269
|
+
// Even simple code will exceed 10 steps
|
|
270
|
+
const code = `a = 1; b = 2; c = 3; d = 4; e = 5;`;
|
|
271
|
+
expect(() => tightRun(code)).toThrow("Execution limit exceeded");
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it("allows disabling step limit with 0", () => {
|
|
275
|
+
const noLimitRun = runner({ maxSteps: 0 });
|
|
276
|
+
const code = `a = 1; b = 2; c = a + b;`;
|
|
277
|
+
const { env } = noLimitRun(code);
|
|
278
|
+
expect(env.c).toEqual(3);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("default limit allows normal-sized scripts", () => {
|
|
282
|
+
// Build a moderately complex script
|
|
283
|
+
let code = "";
|
|
284
|
+
for (let i = 0; i < 100; i++) {
|
|
285
|
+
code += `x${i} = ${i};\n`;
|
|
286
|
+
}
|
|
287
|
+
code += `result = x0 + x99;`;
|
|
288
|
+
const { env } = run(code);
|
|
289
|
+
expect(env.result).toEqual(99);
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
// ==========================================================================
|
|
294
|
+
// 9. Sandbox escape via builtin return values
|
|
295
|
+
// ==========================================================================
|
|
296
|
+
|
|
297
|
+
describe("sandbox escape: via builtin return values", () => {
|
|
298
|
+
it("blocks constructor on array returned by map", () => {
|
|
299
|
+
const code = `
|
|
300
|
+
result = map([1,2], (x) => x);
|
|
301
|
+
result.constructor;
|
|
302
|
+
`;
|
|
303
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it("blocks constructor on object returned by assign", () => {
|
|
307
|
+
const code = `
|
|
308
|
+
obj = assign({ a: 1 });
|
|
309
|
+
obj.constructor;
|
|
310
|
+
`;
|
|
311
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("blocks constructor on string returned by join", () => {
|
|
315
|
+
const code = `
|
|
316
|
+
s = join(["a", "b"], ",");
|
|
317
|
+
s.constructor;
|
|
318
|
+
`;
|
|
319
|
+
expect(() => run(code)).toThrow("Access to property 'constructor' is not allowed");
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
// ==========================================================================
|
|
324
|
+
// 10. Sandbox escape — original break-out.js tests (preserved)
|
|
325
|
+
// ==========================================================================
|
|
326
|
+
|
|
327
|
+
describe("original break-out tests (preserved)", () => {
|
|
328
|
+
it("this.constructor.constructor", () => {
|
|
329
|
+
const code = `bar = this.constructor.constructor`;
|
|
330
|
+
expect(() => run(code)).toThrow("Cannot evaluate ThisExpression");
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
it("object.constructor", () => {
|
|
334
|
+
const code = `
|
|
335
|
+
bar = { any: "thing" };
|
|
336
|
+
func = bar.constructor;
|
|
337
|
+
func2 = func("return process");
|
|
338
|
+
process = func2();
|
|
339
|
+
`;
|
|
340
|
+
expect(() => run(code)).toThrow(
|
|
341
|
+
"Access to property 'constructor' is not allowed"
|
|
342
|
+
);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it("arguments.callee.caller", () => {
|
|
346
|
+
const code = `
|
|
347
|
+
function getFunc () {
|
|
348
|
+
return arguments.callee.caller.arguments.callee.caller.arguments.callee.caller.arguments.callee.arguments.callee.caller;
|
|
349
|
+
}
|
|
350
|
+
func = getFunc();
|
|
351
|
+
func.call(func, "var fs = process.mainModule.require('fs');");
|
|
352
|
+
`;
|
|
353
|
+
expect(() => run(code)).toThrow(
|
|
354
|
+
new ReferenceError(`"arguments" is not defined (3:15)`)
|
|
355
|
+
);
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
// ==========================================================================
|
|
360
|
+
// 11. Safe property access still works
|
|
361
|
+
// ==========================================================================
|
|
362
|
+
|
|
363
|
+
describe("safe property access still works", () => {
|
|
364
|
+
it("array .length", () => {
|
|
365
|
+
const code = `[1, 2, 3].length`;
|
|
366
|
+
const { result } = run(code);
|
|
367
|
+
expect(result).toEqual(3);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it("string .length", () => {
|
|
371
|
+
const code = `a = "hello"; a.length`;
|
|
372
|
+
const { result } = run(code);
|
|
373
|
+
expect(result).toEqual(5);
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it("object property access", () => {
|
|
377
|
+
const code = `a = { foo: "bar" }; a.foo`;
|
|
378
|
+
const { result } = run(code);
|
|
379
|
+
expect(result).toEqual("bar");
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
it("nested object property access", () => {
|
|
383
|
+
const code = `a.b.c`;
|
|
384
|
+
const { result } = run(code, { a: { b: { c: 42 } } });
|
|
385
|
+
expect(result).toEqual(42);
|
|
386
|
+
});
|
|
387
|
+
|
|
388
|
+
it("computed property access with string", () => {
|
|
389
|
+
const code = `a = { foo: "bar" }; a["foo"]`;
|
|
390
|
+
const { result } = run(code);
|
|
391
|
+
expect(result).toEqual("bar");
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it("array index access", () => {
|
|
395
|
+
const code = `a = [10, 20, 30]; a[1]`;
|
|
396
|
+
const { result } = run(code);
|
|
397
|
+
expect(result).toEqual(20);
|
|
398
|
+
});
|
|
399
|
+
});
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import runner from "../index.js";
|
|
2
3
|
|
|
3
4
|
const run = runner();
|
|
4
5
|
|
|
5
|
-
const DEBUG = true;
|
|
6
|
-
|
|
7
6
|
describe("define global vars", () => {
|
|
8
7
|
it(`foo`, () => {
|
|
9
8
|
const code = `foo`;
|
|
@@ -22,9 +21,7 @@ describe("define global vars", () => {
|
|
|
22
21
|
it("const foo", () => {
|
|
23
22
|
const code = `const foo;`;
|
|
24
23
|
const vars = {};
|
|
25
|
-
expect(() => run(code, vars)).toThrow(
|
|
26
|
-
new SyntaxError("Unexpected token (1:9)")
|
|
27
|
-
);
|
|
24
|
+
expect(() => run(code, vars)).toThrow(SyntaxError);
|
|
28
25
|
});
|
|
29
26
|
|
|
30
27
|
it(`foo = "bar"`, () => {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const binaryOps: Record<string, (l: unknown, r: unknown) => unknown> = {
|
|
2
|
+
"+": (l: any, r: any) => l + r,
|
|
3
|
+
"-": (l: any, r: any) => l - r,
|
|
4
|
+
"*": (l: any, r: any) => l * r,
|
|
5
|
+
"/": (l: any, r: any) => l / r,
|
|
6
|
+
"%": (l: any, r: any) => l % r,
|
|
7
|
+
|
|
8
|
+
">": (l: any, r: any) => l > r,
|
|
9
|
+
"<": (l: any, r: any) => l < r,
|
|
10
|
+
">=": (l: any, r: any) => l >= r,
|
|
11
|
+
"<=": (l: any, r: any) => l <= r,
|
|
12
|
+
"==": (l: any, r: any) => l === r,
|
|
13
|
+
"!=": (l: any, r: any) => l !== r,
|
|
14
|
+
"===": (l: any, r: any) => l === r,
|
|
15
|
+
"!==": (l: any, r: any) => l !== r,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export default binaryOps;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
export type FunctionMap = Record<string, (...args: any[]) => any>;
|
|
2
|
+
|
|
3
|
+
// ---------------------------------------------------------------------------
|
|
4
|
+
// Date helpers (Intl-based, no moment dependency)
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
function getDateParts(date: Date, tz: string) {
|
|
8
|
+
const fmt = (opts: Intl.DateTimeFormatOptions) =>
|
|
9
|
+
new Intl.DateTimeFormat("en-US", { timeZone: tz, ...opts });
|
|
10
|
+
|
|
11
|
+
const year = parseInt(fmt({ year: "numeric" }).format(date));
|
|
12
|
+
const month = parseInt(fmt({ month: "numeric" }).format(date));
|
|
13
|
+
const day = parseInt(fmt({ day: "numeric" }).format(date));
|
|
14
|
+
|
|
15
|
+
const h24Parts = fmt({
|
|
16
|
+
hour: "numeric",
|
|
17
|
+
minute: "numeric",
|
|
18
|
+
second: "numeric",
|
|
19
|
+
hourCycle: "h23",
|
|
20
|
+
}).formatToParts(date);
|
|
21
|
+
|
|
22
|
+
const part = (type: string) =>
|
|
23
|
+
h24Parts.find((p) => p.type === type)?.value || "0";
|
|
24
|
+
const hour24 = parseInt(part("hour"));
|
|
25
|
+
const minute = parseInt(part("minute"));
|
|
26
|
+
const second = parseInt(part("second"));
|
|
27
|
+
|
|
28
|
+
const hour12 = hour24 === 0 ? 12 : hour24 > 12 ? hour24 - 12 : hour24;
|
|
29
|
+
const ampm = hour24 >= 12 ? "pm" : "am";
|
|
30
|
+
|
|
31
|
+
const monthShort = fmt({ month: "short" }).format(date);
|
|
32
|
+
const monthLong = fmt({ month: "long" }).format(date);
|
|
33
|
+
|
|
34
|
+
const tzAbbr =
|
|
35
|
+
fmt({ timeZoneName: "short" })
|
|
36
|
+
.formatToParts(date)
|
|
37
|
+
.find((p) => p.type === "timeZoneName")?.value || "";
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
year, month, day,
|
|
41
|
+
hour12, hour24, minute, second,
|
|
42
|
+
ampm, monthShort, monthLong, tzAbbr,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const FORMAT_TOKENS =
|
|
47
|
+
/YYYY|YY|MMMM|MMM|MM|M|DD|D|HH|H|hh|h|mm|m|ss|s|a|A|z/g;
|
|
48
|
+
|
|
49
|
+
function applyFormat(parts: ReturnType<typeof getDateParts>, format: string) {
|
|
50
|
+
return format.replace(FORMAT_TOKENS, (token) => {
|
|
51
|
+
switch (token) {
|
|
52
|
+
case "YYYY": return String(parts.year);
|
|
53
|
+
case "YY": return String(parts.year).slice(-2);
|
|
54
|
+
case "MMMM": return parts.monthLong;
|
|
55
|
+
case "MMM": return parts.monthShort;
|
|
56
|
+
case "MM": return String(parts.month).padStart(2, "0");
|
|
57
|
+
case "M": return String(parts.month);
|
|
58
|
+
case "DD": return String(parts.day).padStart(2, "0");
|
|
59
|
+
case "D": return String(parts.day);
|
|
60
|
+
case "HH": return String(parts.hour24).padStart(2, "0");
|
|
61
|
+
case "H": return String(parts.hour24);
|
|
62
|
+
case "hh": return String(parts.hour12).padStart(2, "0");
|
|
63
|
+
case "h": return String(parts.hour12);
|
|
64
|
+
case "mm": return String(parts.minute).padStart(2, "0");
|
|
65
|
+
case "m": return String(parts.minute);
|
|
66
|
+
case "ss": return String(parts.second).padStart(2, "0");
|
|
67
|
+
case "s": return String(parts.second);
|
|
68
|
+
case "a": return parts.ampm;
|
|
69
|
+
case "A": return parts.ampm.toUpperCase();
|
|
70
|
+
case "z": return parts.tzAbbr;
|
|
71
|
+
default: return token;
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
// Number helpers
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
|
|
80
|
+
function numberFormatter(...args: any[]) {
|
|
81
|
+
return new Intl.NumberFormat(...args).format;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const functions: FunctionMap = {
|
|
85
|
+
push(...args: any[]) {
|
|
86
|
+
const [target, item] = args;
|
|
87
|
+
target.push(item);
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
shift(...args: any[]) {
|
|
91
|
+
const [target] = args;
|
|
92
|
+
return target.shift();
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
currentDate(tz?: string) {
|
|
96
|
+
return new Date();
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
formatDate(dateString: string, format: string, timezone?: string) {
|
|
100
|
+
const date = /^\d+$/.test(dateString)
|
|
101
|
+
? new Date(+dateString)
|
|
102
|
+
: new Date(dateString);
|
|
103
|
+
const tz = timezone || "UTC";
|
|
104
|
+
return applyFormat(getDateParts(date, tz), format);
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
numberFormatter,
|
|
108
|
+
|
|
109
|
+
formatDollars: numberFormatter("en-us", {
|
|
110
|
+
minimumFractionDigits: 2,
|
|
111
|
+
maximumFractionDigits: 2,
|
|
112
|
+
}),
|
|
113
|
+
|
|
114
|
+
// Array/collection operations (replaces lodash)
|
|
115
|
+
map(arr: any[], fn: (...args: any[]) => any) {
|
|
116
|
+
return arr.map(fn);
|
|
117
|
+
},
|
|
118
|
+
filter(arr: any[], fn: (...args: any[]) => any) {
|
|
119
|
+
return arr.filter(fn);
|
|
120
|
+
},
|
|
121
|
+
find(arr: any[], fn: (...args: any[]) => any) {
|
|
122
|
+
return arr.find(fn);
|
|
123
|
+
},
|
|
124
|
+
forEach(arr: any[], fn: (...args: any[]) => any) {
|
|
125
|
+
arr.forEach(fn);
|
|
126
|
+
},
|
|
127
|
+
reduce(arr: any[], fn: (...args: any[]) => any, initial?: any) {
|
|
128
|
+
return initial !== undefined ? arr.reduce(fn, initial) : arr.reduce(fn);
|
|
129
|
+
},
|
|
130
|
+
concat(arr: any[], ...items: any[]) {
|
|
131
|
+
return arr.concat(...items);
|
|
132
|
+
},
|
|
133
|
+
keys(obj: any) {
|
|
134
|
+
return Object.keys(obj);
|
|
135
|
+
},
|
|
136
|
+
values(obj: any) {
|
|
137
|
+
return Object.values(obj);
|
|
138
|
+
},
|
|
139
|
+
entries(obj: any) {
|
|
140
|
+
return Object.entries(obj);
|
|
141
|
+
},
|
|
142
|
+
assign(...args: any[]) {
|
|
143
|
+
return Object.assign({}, ...args);
|
|
144
|
+
},
|
|
145
|
+
join(arr: any[], sep: string) {
|
|
146
|
+
return arr.join(sep);
|
|
147
|
+
},
|
|
148
|
+
includes(arr: any[], item: any) {
|
|
149
|
+
return arr.includes(item);
|
|
150
|
+
},
|
|
151
|
+
slice(arr: any[], start?: number, end?: number) {
|
|
152
|
+
return arr.slice(start, end);
|
|
153
|
+
},
|
|
154
|
+
sort(arr: any[], fn?: (...args: any[]) => any) {
|
|
155
|
+
return [...arr].sort(fn);
|
|
156
|
+
},
|
|
157
|
+
reverse(arr: any[]) {
|
|
158
|
+
return [...arr].reverse();
|
|
159
|
+
},
|
|
160
|
+
flat(arr: any[], depth?: number) {
|
|
161
|
+
return arr.flat(depth);
|
|
162
|
+
},
|
|
163
|
+
flatMap(arr: any[], fn: (...args: any[]) => any) {
|
|
164
|
+
return arr.flatMap(fn);
|
|
165
|
+
},
|
|
166
|
+
some(arr: any[], fn: (...args: any[]) => any) {
|
|
167
|
+
return arr.some(fn);
|
|
168
|
+
},
|
|
169
|
+
every(arr: any[], fn: (...args: any[]) => any) {
|
|
170
|
+
return arr.every(fn);
|
|
171
|
+
},
|
|
172
|
+
indexOf(arr: any[], item: any) {
|
|
173
|
+
return arr.indexOf(item);
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export default functions;
|