@jsb188/app 1.1.11 → 1.1.12
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/package.json +7 -4
- package/src/constants/agent.ts +39 -62
- package/src/constants/app.ts +17 -0
- package/src/constants/wsActions.ts +7 -15
- package/src/i18n/account.ts +1 -1
- package/src/i18n/agent.ts +326 -88
- package/src/i18n/agentToolBusiness.ts +196 -0
- package/src/i18n/agentToolGrid.ts +137 -0
- package/src/i18n/agentToolWorkflow.ts +101 -0
- package/src/i18n/agent_note.ts +109 -0
- package/src/i18n/app.ts +23 -17
- package/src/i18n/auth.ts +28 -30
- package/src/i18n/billing.ts +3 -85
- package/src/i18n/c.ts +907 -0
- package/src/i18n/campaign.ts +842 -140
- package/src/i18n/datetime.ts +6 -0
- package/src/i18n/editor.ts +58 -0
- package/src/i18n/error.ts +307 -168
- package/src/i18n/field_doc_editor.ts +21 -0
- package/src/i18n/field_doc_programs.ts +52 -0
- package/src/i18n/field_docs.ts +170 -8
- package/src/i18n/index.ts +21 -5
- package/src/i18n/log.ts +232 -38
- package/src/i18n/memory.ts +18 -4
- package/src/i18n/note.ts +5 -3
- package/src/i18n/org.ts +93 -44
- package/src/i18n/schedule.ts +209 -0
- package/src/i18n/sheet.ts +102 -145
- package/src/i18n/sheet_formula.ts +165 -0
- package/src/i18n/workflow.ts +48 -9
- package/src/main.ts +2 -2
- package/src/platform.ts +89 -49
- package/src/types/ai.d.ts +19 -0
- package/src/types/app.d.ts +50 -57
- package/src/types/other.d.ts +8 -1
- package/src/utils/api.ts +46 -0
- package/src/utils/clientIP.ts +213 -0
- package/src/utils/color.ts +77 -0
- package/src/utils/csv.ts +19 -0
- package/src/utils/datetime.ts +207 -31
- package/src/utils/deferredSave.ts +89 -0
- package/src/utils/email.ts +50 -0
- package/src/utils/expression.helpers.ts +470 -0
- package/src/utils/expression.test.ts +793 -0
- package/src/utils/expression.ts +1633 -239
- package/src/utils/language.ts +9 -0
- package/src/utils/logic.ts +9 -162
- package/src/utils/number.ts +30 -11
- package/src/utils/object.ts +29 -0
- package/src/utils/promise.ts +78 -7
- package/src/utils/queryReset.ts +101 -14
- package/src/utils/string.test.ts +24 -39
- package/src/utils/string.ts +73 -143
- package/src/utils/url_client.ts +83 -14
- package/src/constants/billing.ts +0 -146
- package/src/i18n/agentDoc.ts +0 -110
- package/src/i18n/form.ts +0 -496
- package/src/types/form.d.ts +0 -101
- package/src/utils/billing.ts +0 -141
|
@@ -0,0 +1,793 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Assert that a condition holds.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
function ok(condition: unknown, message?: string): asserts condition {
|
|
6
|
+
if (!condition) {
|
|
7
|
+
throw new Error(message || 'Expected condition to be truthy');
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/*
|
|
12
|
+
* Assert strict equality of two values.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
function strictEqual(actual: unknown, expected: unknown, message?: string) {
|
|
16
|
+
if (!Object.is(actual, expected)) {
|
|
17
|
+
throw new Error(
|
|
18
|
+
`${message || 'Values are not strictly equal'}\nExpected: ${String(expected)}\nReceived: ${String(actual)}`,
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/*
|
|
24
|
+
* Assert deep equality of two JSON-serializable values.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
function deepStrictEqual(actual: unknown, expected: unknown, message?: string) {
|
|
28
|
+
const actualJSON = JSON.stringify(actual);
|
|
29
|
+
const expectedJSON = JSON.stringify(expected);
|
|
30
|
+
if (actualJSON !== expectedJSON) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
`${message || 'Values are not deeply equal'}\nExpected: ${expectedJSON}\nReceived: ${actualJSON}`,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/*
|
|
38
|
+
* Assert that a function throws an error accepted by the given predicate.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
function throws(fn: () => unknown, predicate: (error: unknown) => boolean) {
|
|
42
|
+
let thrown: unknown = null;
|
|
43
|
+
let didThrow = false;
|
|
44
|
+
try {
|
|
45
|
+
fn();
|
|
46
|
+
} catch (error) {
|
|
47
|
+
didThrow = true;
|
|
48
|
+
thrown = error;
|
|
49
|
+
}
|
|
50
|
+
ok(didThrow, 'Expected function to throw');
|
|
51
|
+
ok(predicate(thrown), 'Thrown error did not satisfy the predicate');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
import {
|
|
55
|
+
compileExpression,
|
|
56
|
+
EXPRESSION_FUNCTION_NAMES,
|
|
57
|
+
EXPRESSION_FUNCTIONS,
|
|
58
|
+
EXPRESSION_MAX_DEPTH,
|
|
59
|
+
EXPRESSION_MAX_LENGTH,
|
|
60
|
+
type ExpressionValue,
|
|
61
|
+
getExpressionCollectionFieldRefs,
|
|
62
|
+
getExpressionFieldRefs,
|
|
63
|
+
parseExpression,
|
|
64
|
+
renderExpressionCheatsheet,
|
|
65
|
+
toStrictExpressionNumber,
|
|
66
|
+
} from './expression.ts';
|
|
67
|
+
import {
|
|
68
|
+
averageExpressionNumbers,
|
|
69
|
+
compareExpressionValues,
|
|
70
|
+
getExpressionDataByteLength,
|
|
71
|
+
normalizeExpressionDataValue,
|
|
72
|
+
parseDecoratedExpressionNumber,
|
|
73
|
+
sumExpressionNumbers,
|
|
74
|
+
} from './expression.helpers.ts';
|
|
75
|
+
|
|
76
|
+
const RECEIPT_ROW = {
|
|
77
|
+
date: '2026-08-05',
|
|
78
|
+
vendorName: 'Fresh Farms',
|
|
79
|
+
metadata: {
|
|
80
|
+
values: [
|
|
81
|
+
{ label: 'EBT', value: 20 },
|
|
82
|
+
{ label: 'MM', value: 25 },
|
|
83
|
+
{ label: 'mm', value: '5' },
|
|
84
|
+
{ label: 'Cash', value: 'n/a' },
|
|
85
|
+
],
|
|
86
|
+
items: [],
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Evaluate one expression against a row.
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
function run(
|
|
95
|
+
expression: string,
|
|
96
|
+
row: Record<string, unknown> = {},
|
|
97
|
+
): ExpressionValue {
|
|
98
|
+
return compileExpression(expression).evaluate(row);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Assert that parsing fails and the message contains every given fragment.
|
|
103
|
+
*/
|
|
104
|
+
|
|
105
|
+
function expectParseError(expression: string, ...fragments: string[]) {
|
|
106
|
+
throws(
|
|
107
|
+
() => parseExpression(expression),
|
|
108
|
+
(error: unknown) => {
|
|
109
|
+
ok(error instanceof Error, 'expected an Error');
|
|
110
|
+
strictEqual(error.name, 'ExpressionError');
|
|
111
|
+
for (const fragment of fragments) {
|
|
112
|
+
ok(
|
|
113
|
+
error.message.includes(fragment),
|
|
114
|
+
`expected "${error.message}" to include "${fragment}"`,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
},
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
Deno.test('registry derives the name list and exposes limits 600/10', () => {
|
|
123
|
+
strictEqual(EXPRESSION_MAX_LENGTH, 600);
|
|
124
|
+
strictEqual(EXPRESSION_MAX_DEPTH, 10);
|
|
125
|
+
deepStrictEqual(
|
|
126
|
+
[...EXPRESSION_FUNCTION_NAMES],
|
|
127
|
+
EXPRESSION_FUNCTIONS.map((spec) => spec.name),
|
|
128
|
+
);
|
|
129
|
+
strictEqual(
|
|
130
|
+
new Set(EXPRESSION_FUNCTION_NAMES).size,
|
|
131
|
+
EXPRESSION_FUNCTIONS.length,
|
|
132
|
+
);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
Deno.test('unknown function errors suggest the closest names and mention JS syntax', () => {
|
|
136
|
+
expectParseError(
|
|
137
|
+
'uppercase(name)',
|
|
138
|
+
'Unknown function "uppercase"',
|
|
139
|
+
'Did you mean upper',
|
|
140
|
+
'JavaScript syntax',
|
|
141
|
+
);
|
|
142
|
+
expectParseError('lookUpValue(x)', 'Did you mean lookup');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
Deno.test('JavaScript syntax is rejected with a helpful message', () => {
|
|
146
|
+
expectParseError(
|
|
147
|
+
'name.toUpperCase()',
|
|
148
|
+
'is not a valid function name',
|
|
149
|
+
'JavaScript syntax',
|
|
150
|
+
);
|
|
151
|
+
expectParseError('a === b', '"==="', 'JavaScript syntax');
|
|
152
|
+
expectParseError('a ?? 0', '"??"', 'JavaScript syntax');
|
|
153
|
+
expectParseError('metadata?.values', '"?."', 'JavaScript syntax');
|
|
154
|
+
expectParseError('concat("a", b)', 'Unexpected """', 'JavaScript syntax');
|
|
155
|
+
expectParseError('a && b', 'JavaScript syntax');
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
Deno.test('bracket segments cannot carry filters, quotes or commas', () => {
|
|
159
|
+
const message =
|
|
160
|
+
'Bracket segments name a column; use lookup(...) to read array elements.';
|
|
161
|
+
expectParseError("[metadata.values(label='EBT')]", message);
|
|
162
|
+
expectParseError("[label='EBT']", message);
|
|
163
|
+
expectParseError('[a,b]', message);
|
|
164
|
+
expectParseError('["Vendor"]', message);
|
|
165
|
+
strictEqual(run('[Vendor Name]', { 'Vendor Name': 'Farm' }), 'Farm');
|
|
166
|
+
strictEqual(
|
|
167
|
+
run('metadata.[Unit price] * 2', { metadata: { 'Unit price': '$3.50' } }),
|
|
168
|
+
7,
|
|
169
|
+
);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
Deno.test('unexpected characters include an excerpt around the position', () => {
|
|
173
|
+
expectParseError(
|
|
174
|
+
'total + #tax',
|
|
175
|
+
'Unexpected character "#"',
|
|
176
|
+
'near "',
|
|
177
|
+
'#tax',
|
|
178
|
+
);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
Deno.test('length and depth limits are enforced', () => {
|
|
182
|
+
expectParseError(
|
|
183
|
+
`concat(${"'a', ".repeat(150)}'b')`,
|
|
184
|
+
`longer than ${EXPRESSION_MAX_LENGTH}`,
|
|
185
|
+
);
|
|
186
|
+
expectParseError(`${'('.repeat(11)}1${')'.repeat(11)}`, 'maximum depth');
|
|
187
|
+
strictEqual(run(`${'('.repeat(10)}1${')'.repeat(10)}`), 1);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
Deno.test('arity and argument shape errors are reported at parse time', () => {
|
|
191
|
+
expectParseError('round()', 'expects 1 to 2 argument(s)');
|
|
192
|
+
expectParseError(
|
|
193
|
+
"lookup(metadata.values, label, 'EBT', 'value')",
|
|
194
|
+
'quoted element field name',
|
|
195
|
+
);
|
|
196
|
+
expectParseError(
|
|
197
|
+
"lookup('x', 'label', 'EBT', 'value')",
|
|
198
|
+
'must be a field reference to an array',
|
|
199
|
+
);
|
|
200
|
+
expectParseError("dateAdd(date, 1, 'week')", "'day', 'month' or 'year'");
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
Deno.test('operator precedence and null arithmetic', () => {
|
|
204
|
+
strictEqual(run('1 + 2 * 3'), 7);
|
|
205
|
+
strictEqual(run('(1 + 2) * 3'), 9);
|
|
206
|
+
strictEqual(run('-2 * 3 + 1'), -5);
|
|
207
|
+
strictEqual(run('1 + 2 = 3 and not false or false'), true);
|
|
208
|
+
strictEqual(run('num(price) * 2', { price: '$1,200.50' }), 2401);
|
|
209
|
+
strictEqual(run('missing + 1'), null);
|
|
210
|
+
strictEqual(run('1 / 0'), null);
|
|
211
|
+
strictEqual(run("'10' > '9'"), true);
|
|
212
|
+
strictEqual(run("'b' > 'a'"), true);
|
|
213
|
+
strictEqual(run('missing = null'), true);
|
|
214
|
+
strictEqual(run('missing != 1'), true);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
Deno.test('text functions', () => {
|
|
218
|
+
strictEqual(
|
|
219
|
+
run("replace(phone, '-', '')", { phone: '555-12-34' }),
|
|
220
|
+
'5551234',
|
|
221
|
+
);
|
|
222
|
+
strictEqual(run("replace(phone, '', 'x')", { phone: 'ab' }), 'ab');
|
|
223
|
+
strictEqual(run("replace(phone, 'A', 'x')", { phone: 'aA' }), 'ax');
|
|
224
|
+
strictEqual(run("replace(missing, '-', '')"), null);
|
|
225
|
+
strictEqual(run('len(name)', { name: 'abcd' }), 4);
|
|
226
|
+
strictEqual(run('len(missing)'), null);
|
|
227
|
+
strictEqual(run("startsWith(sku, 'org-')", { sku: 'ORG-1' }), true);
|
|
228
|
+
strictEqual(run("startsWith(missing, 'x')"), false);
|
|
229
|
+
strictEqual(run("endsWith(file, '.PDF')", { file: 'a.pdf' }), true);
|
|
230
|
+
strictEqual(run("endsWith(missing, 'x')"), false);
|
|
231
|
+
strictEqual(run("padStart(n, 5, '0')", { n: 42 }), '00042');
|
|
232
|
+
strictEqual(run('padStart(n, 3)', { n: '7' }), ' 7');
|
|
233
|
+
strictEqual(run("padStart(missing, 5, '0')"), null);
|
|
234
|
+
strictEqual(run('left(date, 7)', { date: '2026-08-05' }), '2026-08');
|
|
235
|
+
strictEqual(run('right(phone, 4)', { phone: '5551234' }), '1234');
|
|
236
|
+
strictEqual(run('right(phone, 0)', { phone: '5551234' }), '');
|
|
237
|
+
strictEqual(run('left(missing, 2)'), null);
|
|
238
|
+
strictEqual(run('right(missing, 2)'), null);
|
|
239
|
+
strictEqual(run("split(name, ' ', 1)", { name: 'Jane Q Doe' }), 'Q');
|
|
240
|
+
strictEqual(run("split(name, ' ', 5)", { name: 'Jane Q Doe' }), null);
|
|
241
|
+
strictEqual(run("split(name, ' ', -1)", { name: 'Jane Q Doe' }), null);
|
|
242
|
+
strictEqual(run("split(missing, ' ', 0)"), null);
|
|
243
|
+
strictEqual(run("concat(first, ' ', missing)", { first: 'A' }), 'A ');
|
|
244
|
+
strictEqual(run("coalesce(missing, '', 'x')"), 'x');
|
|
245
|
+
strictEqual(run("contains(notes, 'ORG')", { notes: 'organic' }), true);
|
|
246
|
+
strictEqual(run('substr(sku, 1, 2)', { sku: 'abcd' }), 'bc');
|
|
247
|
+
strictEqual(run('upper(trim(name))', { name: ' ab ' }), 'AB');
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
Deno.test('number functions', () => {
|
|
251
|
+
strictEqual(run('min(3, missing, 1, 2)'), 1);
|
|
252
|
+
strictEqual(run("max('7 lbs', 2)"), 7);
|
|
253
|
+
strictEqual(run('min(missing, null)'), null);
|
|
254
|
+
strictEqual(run('floor(2.7)'), 2);
|
|
255
|
+
strictEqual(run('ceil(2.1)'), 3);
|
|
256
|
+
strictEqual(run('floor(missing)'), null);
|
|
257
|
+
strictEqual(run('mod(7, 3)'), 1);
|
|
258
|
+
strictEqual(run('mod(-7, 3)'), -1);
|
|
259
|
+
strictEqual(run('mod(7, 0)'), null);
|
|
260
|
+
strictEqual(run('mod(missing, 2)'), null);
|
|
261
|
+
strictEqual(run('round(2.345, 2)'), 2.35);
|
|
262
|
+
strictEqual(run('abs(-4)'), 4);
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
Deno.test('date functions operate in UTC and accept ISO or YYYY-MM-DD', () => {
|
|
266
|
+
strictEqual(run('year(d)', { d: '2026-08-05' }), 2026);
|
|
267
|
+
strictEqual(run('month(d)', { d: '2026-08-05' }), 8);
|
|
268
|
+
strictEqual(run('day(d)', { d: '2026-08-05T23:59:00Z' }), 5);
|
|
269
|
+
strictEqual(run('weekday(d)', { d: '2026-08-09' }), 0); // Sunday
|
|
270
|
+
strictEqual(run('weekday(d)', { d: '2026-08-10' }), 1);
|
|
271
|
+
strictEqual(run('weekdayName(d)', { d: '2026-08-10' }), 'Monday');
|
|
272
|
+
strictEqual(run('monthName(d)', { d: '2026-08-10' }), 'August');
|
|
273
|
+
strictEqual(run('year(missing)'), null);
|
|
274
|
+
strictEqual(run("year('not a date')"), null);
|
|
275
|
+
strictEqual(run("dateAdd(d, 30, 'day')", { d: '2026-08-05' }), '2026-09-04');
|
|
276
|
+
strictEqual(run("dateAdd(d, -1, 'day')", { d: '2026-03-01' }), '2026-02-28');
|
|
277
|
+
strictEqual(run("dateAdd(d, 1, 'month')", { d: '2026-01-31' }), '2026-02-28');
|
|
278
|
+
strictEqual(run("dateAdd(d, 1, 'year')", { d: '2024-02-29' }), '2025-02-28');
|
|
279
|
+
strictEqual(
|
|
280
|
+
run("dateAdd(d, 2, 'days')", { d: '2026-08-05T10:00:00Z' }),
|
|
281
|
+
'2026-08-07',
|
|
282
|
+
);
|
|
283
|
+
strictEqual(run("dateAdd(missing, 1, 'day')"), null);
|
|
284
|
+
strictEqual(run("dateAdd(d, missing, 'day')", { d: '2026-08-05' }), null);
|
|
285
|
+
strictEqual(
|
|
286
|
+
run("dateDiff(a, b, 'day')", { a: '2026-08-01', b: '2026-08-31' }),
|
|
287
|
+
30,
|
|
288
|
+
);
|
|
289
|
+
strictEqual(
|
|
290
|
+
run("dateDiff(a, b, 'day')", { a: '2026-08-31', b: '2026-08-01' }),
|
|
291
|
+
-30,
|
|
292
|
+
);
|
|
293
|
+
strictEqual(
|
|
294
|
+
run("dateDiff(a, b, 'day')", {
|
|
295
|
+
a: '2026-08-01T23:00:00Z',
|
|
296
|
+
b: '2026-08-02T01:00:00Z',
|
|
297
|
+
}),
|
|
298
|
+
1,
|
|
299
|
+
);
|
|
300
|
+
strictEqual(
|
|
301
|
+
run("dateDiff(a, b, 'month')", { a: '2026-01-31', b: '2026-03-30' }),
|
|
302
|
+
1,
|
|
303
|
+
);
|
|
304
|
+
strictEqual(
|
|
305
|
+
run("dateDiff(a, b, 'year')", { a: '2020-06-15', b: '2026-06-14' }),
|
|
306
|
+
5,
|
|
307
|
+
);
|
|
308
|
+
strictEqual(run("dateDiff(missing, b, 'day')", { b: '2026-08-02' }), null);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
Deno.test('dateFormat tokens', () => {
|
|
312
|
+
const row = { d: '2026-08-05T09:07:03Z' };
|
|
313
|
+
strictEqual(run('dateFormat(d)', row), '2026-08-05');
|
|
314
|
+
strictEqual(run("dateFormat(d, 'MM/DD/YYYY')", row), '08/05/2026');
|
|
315
|
+
strictEqual(run("dateFormat(d, 'HH:mm:ss')", row), '09:07:03');
|
|
316
|
+
strictEqual(run("dateFormat(d, 'MMM DD, YYYY')", row), 'Aug 05, 2026');
|
|
317
|
+
strictEqual(run("dateFormat(d, 'MMMM')", row), 'August');
|
|
318
|
+
strictEqual(run("dateFormat(d, 'ddd')", row), 'Wed');
|
|
319
|
+
strictEqual(
|
|
320
|
+
run("dateFormat(d, 'dddd, MMMM DD')", row),
|
|
321
|
+
'Wednesday, August 05',
|
|
322
|
+
);
|
|
323
|
+
strictEqual(
|
|
324
|
+
run("dateFormat(d, 'MMMM ss')", { d: '2026-12-01T00:00:09Z' }),
|
|
325
|
+
'December 09',
|
|
326
|
+
);
|
|
327
|
+
strictEqual(
|
|
328
|
+
run("dateFormat(d, 'HH:mm')", { d: '2026-08-05 14:30' }),
|
|
329
|
+
'14:30',
|
|
330
|
+
);
|
|
331
|
+
strictEqual(run("dateFormat(missing, 'YYYY')"), null);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
Deno.test('dateFormat converts to an IANA time zone when one is given', () => {
|
|
335
|
+
// 02:00 UTC on Aug 8 is 19:00 PDT on Aug 7
|
|
336
|
+
const row = { d: '2026-08-08T02:00:00Z', tz: 'America/Los_Angeles' };
|
|
337
|
+
strictEqual(
|
|
338
|
+
run("dateFormat(d, 'YYYY-MM-DD HH:mm', 'America/Los_Angeles')", row),
|
|
339
|
+
'2026-08-07 19:00',
|
|
340
|
+
);
|
|
341
|
+
strictEqual(run("dateFormat(d, 'dddd', tz)", row), 'Friday');
|
|
342
|
+
strictEqual(
|
|
343
|
+
run("dateFormat(d, 'YYYY-MM-DD HH:mm', 'UTC')", row),
|
|
344
|
+
'2026-08-08 02:00',
|
|
345
|
+
);
|
|
346
|
+
strictEqual(run("dateFormat(d, 'HH:mm', missing)", row), '02:00');
|
|
347
|
+
strictEqual(run("dateFormat(d, 'HH:mm', 'Asia/Tokyo')", row), '11:00');
|
|
348
|
+
strictEqual(
|
|
349
|
+
run("dateFormat(d, 'HH:mm', 'America/Los_Angeles')", {
|
|
350
|
+
d: '2026-01-15T00:30:00Z',
|
|
351
|
+
}),
|
|
352
|
+
'16:30',
|
|
353
|
+
);
|
|
354
|
+
strictEqual(run("dateFormat(d, 'HH:mm', 'Not/AZone')", row), null);
|
|
355
|
+
expectParseError("dateFormat(d, 'HH', 'UTC', 1)", 'dateFormat');
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
Deno.test('logic functions', () => {
|
|
359
|
+
strictEqual(run('isNull(missing)'), true);
|
|
360
|
+
strictEqual(run("isNull('')"), false);
|
|
361
|
+
strictEqual(run('isEmpty(missing)'), true);
|
|
362
|
+
strictEqual(run("isEmpty('')"), true);
|
|
363
|
+
strictEqual(run('isEmpty(metadata.items)', RECEIPT_ROW), true);
|
|
364
|
+
strictEqual(run('isEmpty(metadata.values)', RECEIPT_ROW), false);
|
|
365
|
+
strictEqual(run('isEmpty(0)'), false);
|
|
366
|
+
strictEqual(run("eq(status, 'PAID')", { status: ' paid ' }), true);
|
|
367
|
+
strictEqual(run('eq(missing, null)'), true);
|
|
368
|
+
strictEqual(run("eq(missing, '')"), false);
|
|
369
|
+
strictEqual(run('eq(1, 1.0)'), true);
|
|
370
|
+
strictEqual(run('between(t, 33, 41)', { t: '41' }), true);
|
|
371
|
+
strictEqual(run('between(t, 33, 41)', { t: 42 }), false);
|
|
372
|
+
strictEqual(run('between(missing, 33, 41)'), false);
|
|
373
|
+
strictEqual(run("if(metadata.items, 'yes', 'no')", RECEIPT_ROW), 'no');
|
|
374
|
+
strictEqual(run("if(metadata.values, 'yes', 'no')", RECEIPT_ROW), 'yes');
|
|
375
|
+
strictEqual(run('if(false, 1)'), null);
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
Deno.test('lookup and sumWhere on the receipt shape', () => {
|
|
379
|
+
strictEqual(
|
|
380
|
+
run("lookup(metadata.values, 'label', 'EBT', 'value')", RECEIPT_ROW),
|
|
381
|
+
20,
|
|
382
|
+
);
|
|
383
|
+
strictEqual(
|
|
384
|
+
run("lookup(metadata.values, 'label', 'ebt ', 'value')", RECEIPT_ROW),
|
|
385
|
+
20,
|
|
386
|
+
);
|
|
387
|
+
strictEqual(
|
|
388
|
+
run("lookup(metadata.values, 'label', 'MM', 'value')", RECEIPT_ROW),
|
|
389
|
+
25,
|
|
390
|
+
);
|
|
391
|
+
strictEqual(
|
|
392
|
+
run("lookup(metadata.values, 'label', 'SNAP', 'value')", RECEIPT_ROW),
|
|
393
|
+
null,
|
|
394
|
+
);
|
|
395
|
+
strictEqual(
|
|
396
|
+
run(
|
|
397
|
+
"coalesce(lookup(metadata.values, 'label', 'SNAP', 'value'), 0)",
|
|
398
|
+
RECEIPT_ROW,
|
|
399
|
+
),
|
|
400
|
+
0,
|
|
401
|
+
);
|
|
402
|
+
strictEqual(
|
|
403
|
+
run("lookup(metadata.values, 'label', 'EBT', 'value') + 1", RECEIPT_ROW),
|
|
404
|
+
21,
|
|
405
|
+
);
|
|
406
|
+
strictEqual(
|
|
407
|
+
run("sumWhere(metadata.values, 'label', 'MM', 'value')", RECEIPT_ROW),
|
|
408
|
+
30,
|
|
409
|
+
);
|
|
410
|
+
strictEqual(
|
|
411
|
+
run("sumWhere(metadata.values, 'label', 'Cash', 'value')", RECEIPT_ROW),
|
|
412
|
+
0,
|
|
413
|
+
);
|
|
414
|
+
strictEqual(
|
|
415
|
+
run("sumWhere(metadata.values, 'label', 'SNAP', 'value')", RECEIPT_ROW),
|
|
416
|
+
0,
|
|
417
|
+
);
|
|
418
|
+
strictEqual(
|
|
419
|
+
run("sumWhere(metadata.items, 'label', 'MM', 'value')", RECEIPT_ROW),
|
|
420
|
+
0,
|
|
421
|
+
);
|
|
422
|
+
strictEqual(run("lookup(missing, 'label', 'EBT', 'value')"), null);
|
|
423
|
+
strictEqual(
|
|
424
|
+
run("lookup(vendorName, 'label', 'EBT', 'value')", RECEIPT_ROW),
|
|
425
|
+
null,
|
|
426
|
+
);
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
Deno.test('other collection functions incl. null and empty-array behavior', () => {
|
|
430
|
+
strictEqual(
|
|
431
|
+
run("countWhere(metadata.values, 'label', 'mm')", RECEIPT_ROW),
|
|
432
|
+
2,
|
|
433
|
+
);
|
|
434
|
+
strictEqual(run("countWhere(metadata.items, 'label', 'mm')", RECEIPT_ROW), 0);
|
|
435
|
+
strictEqual(run('count(metadata.values)', RECEIPT_ROW), 4);
|
|
436
|
+
strictEqual(run('count(metadata.items)', RECEIPT_ROW), 0);
|
|
437
|
+
strictEqual(run('count(missing)'), 0);
|
|
438
|
+
strictEqual(run('count(vendorName)', RECEIPT_ROW), 0);
|
|
439
|
+
strictEqual(run("sumOf(metadata.values, 'value')", RECEIPT_ROW), 50);
|
|
440
|
+
strictEqual(run("sumOf(metadata.items, 'value')", RECEIPT_ROW), 0);
|
|
441
|
+
strictEqual(run("minOf(metadata.values, 'value')", RECEIPT_ROW), 5);
|
|
442
|
+
strictEqual(run("maxOf(metadata.values, 'value')", RECEIPT_ROW), 25);
|
|
443
|
+
strictEqual(run("minOf(metadata.items, 'value')", RECEIPT_ROW), null);
|
|
444
|
+
strictEqual(run("maxOf(missing, 'value')"), null);
|
|
445
|
+
strictEqual(run("first(metadata.values, 'label')", RECEIPT_ROW), 'EBT');
|
|
446
|
+
strictEqual(run("last(metadata.values, 'label')", RECEIPT_ROW), 'Cash');
|
|
447
|
+
strictEqual(run("first(metadata.items, 'label')", RECEIPT_ROW), null);
|
|
448
|
+
strictEqual(run('first(tags, null)', { tags: ['a', 'b'] }), 'a');
|
|
449
|
+
strictEqual(
|
|
450
|
+
run("joinText(metadata.values, 'label', '; ')", RECEIPT_ROW),
|
|
451
|
+
'EBT; MM; mm; Cash',
|
|
452
|
+
);
|
|
453
|
+
strictEqual(
|
|
454
|
+
run("joinText(metadata.values, 'label')", RECEIPT_ROW),
|
|
455
|
+
'EBT, MM, mm, Cash',
|
|
456
|
+
);
|
|
457
|
+
strictEqual(run("joinText(metadata.items, 'label')", RECEIPT_ROW), '');
|
|
458
|
+
strictEqual(
|
|
459
|
+
run("joinText(rows, 'name')", {
|
|
460
|
+
rows: [{ name: 'a' }, { name: null }, { name: '' }, { name: 'b' }],
|
|
461
|
+
}),
|
|
462
|
+
'a, b',
|
|
463
|
+
);
|
|
464
|
+
strictEqual(
|
|
465
|
+
run("hasAny(metadata.values, 'label', 'cash')", RECEIPT_ROW),
|
|
466
|
+
true,
|
|
467
|
+
);
|
|
468
|
+
strictEqual(
|
|
469
|
+
run("hasAny(metadata.values, 'label', 'SNAP')", RECEIPT_ROW),
|
|
470
|
+
false,
|
|
471
|
+
);
|
|
472
|
+
strictEqual(run("hasAny(missing, 'label', 'SNAP')"), false);
|
|
473
|
+
strictEqual(
|
|
474
|
+
run("lookup(order.lines, 'sku.code', 'A1', 'qty')", {
|
|
475
|
+
order: { lines: [{ sku: { code: 'A1' }, qty: 3 }] },
|
|
476
|
+
}),
|
|
477
|
+
3,
|
|
478
|
+
);
|
|
479
|
+
strictEqual(
|
|
480
|
+
run(
|
|
481
|
+
"sumOf(if(metadata.items, metadata.items, metadata.values), 'value')",
|
|
482
|
+
RECEIPT_ROW,
|
|
483
|
+
),
|
|
484
|
+
50,
|
|
485
|
+
);
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
Deno.test('arrays and objects become JSON text only where a scalar is required', () => {
|
|
489
|
+
strictEqual(run('metadata.items', RECEIPT_ROW), '[]');
|
|
490
|
+
strictEqual(run('tags', { tags: ['a'] }), '["a"]');
|
|
491
|
+
strictEqual(run('files.0.name', { files: [{ name: 'x.pdf' }] }), 'x.pdf');
|
|
492
|
+
strictEqual(run('count(coalesce(missing, metadata.values))', RECEIPT_ROW), 4);
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
Deno.test('evaluateRaw keeps arrays and objects intact', () => {
|
|
496
|
+
const compiled = compileExpression('metadata.values');
|
|
497
|
+
deepStrictEqual(
|
|
498
|
+
compiled.evaluateRaw(RECEIPT_ROW),
|
|
499
|
+
RECEIPT_ROW.metadata.values,
|
|
500
|
+
);
|
|
501
|
+
strictEqual(
|
|
502
|
+
compiled.evaluate(RECEIPT_ROW),
|
|
503
|
+
JSON.stringify(RECEIPT_ROW.metadata.values),
|
|
504
|
+
);
|
|
505
|
+
deepStrictEqual(
|
|
506
|
+
compileExpression('metadata').evaluateRaw(RECEIPT_ROW),
|
|
507
|
+
RECEIPT_ROW.metadata,
|
|
508
|
+
);
|
|
509
|
+
strictEqual(compileExpression('missing').evaluateRaw(RECEIPT_ROW), null);
|
|
510
|
+
strictEqual(
|
|
511
|
+
compileExpression('vendorName').evaluateRaw(RECEIPT_ROW),
|
|
512
|
+
'Fresh Farms',
|
|
513
|
+
);
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
Deno.test('field refs and collection field refs are extracted', () => {
|
|
517
|
+
const node = parseExpression(
|
|
518
|
+
"concat(vendorName, lookup(metadata.values, 'label', 'EBT', 'value'), sumWhere(metadata.values, 'label', 'MM', 'value'), count(missing))",
|
|
519
|
+
);
|
|
520
|
+
deepStrictEqual(getExpressionFieldRefs(node), [
|
|
521
|
+
'vendorName',
|
|
522
|
+
'metadata.values',
|
|
523
|
+
'missing',
|
|
524
|
+
]);
|
|
525
|
+
deepStrictEqual(getExpressionCollectionFieldRefs(node), [
|
|
526
|
+
{
|
|
527
|
+
functionName: 'lookup',
|
|
528
|
+
arrayPath: 'metadata.values',
|
|
529
|
+
elementPaths: ['label', 'value'],
|
|
530
|
+
},
|
|
531
|
+
{
|
|
532
|
+
functionName: 'sumWhere',
|
|
533
|
+
arrayPath: 'metadata.values',
|
|
534
|
+
elementPaths: ['label', 'value'],
|
|
535
|
+
},
|
|
536
|
+
{ functionName: 'count', arrayPath: 'missing', elementPaths: [] },
|
|
537
|
+
]);
|
|
538
|
+
deepStrictEqual(
|
|
539
|
+
getExpressionCollectionFieldRefs(parseExpression('count(coalesce(a, b))')),
|
|
540
|
+
[],
|
|
541
|
+
);
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
Deno.test('comparisons are numeric only for plain numbers; dates, times and ids compare as text', () => {
|
|
545
|
+
strictEqual(run("date >= '2026-08-15'", { date: '2026-08-01' }), false);
|
|
546
|
+
strictEqual(run("date >= '2026-08-15'", { date: '2026-08-20' }), true);
|
|
547
|
+
strictEqual(run("date = '2026-08-15'", { date: '2026-08-01' }), false);
|
|
548
|
+
strictEqual(run("'10:59' > '10:01'"), true);
|
|
549
|
+
strictEqual(run("'10:59' = '10:01'"), false);
|
|
550
|
+
strictEqual(run("'org-12' = 'acc-12'"), false);
|
|
551
|
+
strictEqual(run("'Jul 30, 2026' = 'Aug 01, 2026'"), false);
|
|
552
|
+
strictEqual(
|
|
553
|
+
run("'20' = 20"),
|
|
554
|
+
true,
|
|
555
|
+
'numeric text still compares numerically',
|
|
556
|
+
);
|
|
557
|
+
strictEqual(run("' 3.5 ' < 10"), true);
|
|
558
|
+
strictEqual(
|
|
559
|
+
run("'$1,200.50' + 1"),
|
|
560
|
+
1201.5,
|
|
561
|
+
'arithmetic keeps the lenient parser',
|
|
562
|
+
);
|
|
563
|
+
strictEqual(run("num('12 lbs') * 2"), 24, 'num() keeps the lenient parser');
|
|
564
|
+
|
|
565
|
+
strictEqual(toStrictExpressionNumber('2026-08-01'), null);
|
|
566
|
+
strictEqual(toStrictExpressionNumber('10:59'), null);
|
|
567
|
+
strictEqual(toStrictExpressionNumber('$1,200'), null);
|
|
568
|
+
strictEqual(toStrictExpressionNumber(' 1e3 '), 1000);
|
|
569
|
+
strictEqual(toStrictExpressionNumber(12.5), 12.5);
|
|
570
|
+
strictEqual(
|
|
571
|
+
toStrictExpressionNumber(true),
|
|
572
|
+
null,
|
|
573
|
+
'booleans are not numbers for comparison',
|
|
574
|
+
);
|
|
575
|
+
strictEqual(toStrictExpressionNumber(''), null);
|
|
576
|
+
strictEqual(toStrictExpressionNumber(Number.NaN), null);
|
|
577
|
+
});
|
|
578
|
+
|
|
579
|
+
Deno.test('element fields whose key contains a dot are read as exact keys', () => {
|
|
580
|
+
const row = {
|
|
581
|
+
items: [
|
|
582
|
+
{ 'Qty.': 1, 'Amt. (USD)': 5, nested: { a: 1 } },
|
|
583
|
+
{ 'Qty.': 2, 'Amt. (USD)': 9, nested: { a: 2 } },
|
|
584
|
+
],
|
|
585
|
+
};
|
|
586
|
+
strictEqual(run("lookup(items, 'Qty.', 2, 'Amt. (USD)')", row), 9);
|
|
587
|
+
strictEqual(run("sumOf(items, 'Amt. (USD)')", row), 14);
|
|
588
|
+
strictEqual(
|
|
589
|
+
run("sumOf(items, 'nested.a')", row),
|
|
590
|
+
3,
|
|
591
|
+
'dotted paths still walk nested objects',
|
|
592
|
+
);
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
Deno.test('cheatsheet mentions every registered function and the grammar', () => {
|
|
596
|
+
const cheatsheet = renderExpressionCheatsheet();
|
|
597
|
+
for (const spec of EXPRESSION_FUNCTIONS) {
|
|
598
|
+
ok(cheatsheet.includes(spec.signature), `missing ${spec.name}`);
|
|
599
|
+
ok(cheatsheet.includes(spec.example), `missing example for ${spec.name}`);
|
|
600
|
+
}
|
|
601
|
+
ok(cheatsheet.includes('[Vendor Name]'));
|
|
602
|
+
ok(cheatsheet.includes('JavaScript syntax'));
|
|
603
|
+
ok(cheatsheet.includes('null'));
|
|
604
|
+
});
|
|
605
|
+
|
|
606
|
+
Deno.test('numeric coercion preserves complete signed currency and unit amounts', () => {
|
|
607
|
+
for (const [input, expected] of [
|
|
608
|
+
['($1,200.50)', -1200.5],
|
|
609
|
+
['-$12.50', -12.5],
|
|
610
|
+
['$-12.50', -12.5],
|
|
611
|
+
['-.5 kg', -0.5],
|
|
612
|
+
['USD 1,234.50', 1234.5],
|
|
613
|
+
['1e3 kg', 1000],
|
|
614
|
+
['45%', 45],
|
|
615
|
+
['12 lbs', 12],
|
|
616
|
+
] as const) {
|
|
617
|
+
strictEqual(parseDecoratedExpressionNumber(input), expected, input);
|
|
618
|
+
}
|
|
619
|
+
for (const input of [
|
|
620
|
+
'Farm 12',
|
|
621
|
+
'2026-08-01',
|
|
622
|
+
'12:30',
|
|
623
|
+
'1,23',
|
|
624
|
+
'12 apples 20',
|
|
625
|
+
'12 apples',
|
|
626
|
+
'$12.50 approx',
|
|
627
|
+
'($-12)',
|
|
628
|
+
'0x10',
|
|
629
|
+
]) {
|
|
630
|
+
strictEqual(parseDecoratedExpressionNumber(input), null, input);
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
|
|
634
|
+
Deno.test('decimal rounding is half away from zero without float tie errors', () => {
|
|
635
|
+
strictEqual(run('round(1.005, 2)'), 1.01);
|
|
636
|
+
strictEqual(run('round(10.075, 2)'), 10.08);
|
|
637
|
+
strictEqual(run('round(-10.075, 2)'), -10.08);
|
|
638
|
+
strictEqual(run('round(-1.5)'), -2);
|
|
639
|
+
});
|
|
640
|
+
|
|
641
|
+
Deno.test('decimal collection totals retain cents and finite averages across large sums', () => {
|
|
642
|
+
strictEqual(
|
|
643
|
+
run("sumOf(items, 'amount')", {
|
|
644
|
+
items: [{ amount: 0.7 }, { amount: 0.1 }, { amount: 0.005 }],
|
|
645
|
+
}),
|
|
646
|
+
0.805,
|
|
647
|
+
);
|
|
648
|
+
strictEqual(
|
|
649
|
+
run("round(sumOf(items, 'amount'), 2)", {
|
|
650
|
+
items: [{ amount: -0.7 }, { amount: -0.1 }, { amount: -0.005 }],
|
|
651
|
+
}),
|
|
652
|
+
-0.81,
|
|
653
|
+
);
|
|
654
|
+
strictEqual(sumExpressionNumbers([1e308, 1, -1e308]), 1);
|
|
655
|
+
strictEqual(averageExpressionNumbers([1e308, 1e308]), 1e308);
|
|
656
|
+
strictEqual(averageExpressionNumbers([0.7, 0.1, 0.005]), 0.2683333333333333);
|
|
657
|
+
strictEqual(averageExpressionNumbers([]), null);
|
|
658
|
+
throws(
|
|
659
|
+
() => sumExpressionNumbers([1e308, 1e308]),
|
|
660
|
+
(error) => error instanceof RangeError,
|
|
661
|
+
);
|
|
662
|
+
});
|
|
663
|
+
|
|
664
|
+
Deno.test('binary decimal arithmetic preserves prices with bounded division and range', () => {
|
|
665
|
+
strictEqual(run('round(0.7 * 1.15, 2)'), 0.81);
|
|
666
|
+
strictEqual(run('round(-0.7 * 1.15, 2)'), -0.81);
|
|
667
|
+
strictEqual(run('round(0.7 + 0.1 + 0.005, 2)'), 0.81);
|
|
668
|
+
strictEqual(run('0.3 - 0.1'), 0.2);
|
|
669
|
+
strictEqual(run('1 / 3'), 1 / 3);
|
|
670
|
+
strictEqual(run('-1 / 3'), -1 / 3);
|
|
671
|
+
strictEqual(run('round(1.61 / 2, 2)'), 0.81);
|
|
672
|
+
strictEqual(run('1 / 0'), null);
|
|
673
|
+
strictEqual(run('tiny / factor', { tiny: 1e-300, factor: 1e-8 }), 1e-292);
|
|
674
|
+
strictEqual(run('large * factor', { large: 1e300, factor: 1e-300 }), 1);
|
|
675
|
+
strictEqual(run('large / factor', { large: 1e308, factor: 1e308 }), 1);
|
|
676
|
+
throws(
|
|
677
|
+
() => run('large * 2', { large: 1e308 }),
|
|
678
|
+
(error) => error instanceof RangeError,
|
|
679
|
+
);
|
|
680
|
+
throws(
|
|
681
|
+
() => run('tiny / 10', { tiny: Number.MIN_VALUE }),
|
|
682
|
+
(error) => error instanceof RangeError,
|
|
683
|
+
);
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
Deno.test('mixed numeric and text comparisons have a total order', () => {
|
|
687
|
+
const values = [null, '2', 10, '1a', '20a', false, 'z'];
|
|
688
|
+
for (const a of values) {
|
|
689
|
+
for (const b of values) {
|
|
690
|
+
for (const c of values) {
|
|
691
|
+
if (
|
|
692
|
+
compareExpressionValues(a, b) <= 0 &&
|
|
693
|
+
compareExpressionValues(b, c) <= 0
|
|
694
|
+
) {
|
|
695
|
+
ok(
|
|
696
|
+
compareExpressionValues(a, c) <= 0,
|
|
697
|
+
`${a}, ${b}, ${c} must be transitive`,
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
});
|
|
704
|
+
|
|
705
|
+
Deno.test('quoted keyword columns remain fields and predicates retain array truthiness', () => {
|
|
706
|
+
const row = { null: 99, true: 'stored', and: 42, items: [] };
|
|
707
|
+
strictEqual(run('[null]', row), 99);
|
|
708
|
+
strictEqual(run('[true]', row), 'stored');
|
|
709
|
+
strictEqual(run('[and]', row), 42);
|
|
710
|
+
strictEqual(compileExpression('items').evaluatePredicate(row), false);
|
|
711
|
+
strictEqual(
|
|
712
|
+
compileExpression('items').evaluatePredicate({ items: [1] }),
|
|
713
|
+
true,
|
|
714
|
+
);
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
Deno.test('conditional branches are lazy and expanding expressions are bounded', () => {
|
|
718
|
+
strictEqual(run("if(false, padStart('x', 1000000000), 'ok')"), 'ok');
|
|
719
|
+
strictEqual(run("coalesce('ok', padStart('x', 1000000000))"), 'ok');
|
|
720
|
+
for (const expression of [
|
|
721
|
+
"padStart('x', 1000000000)",
|
|
722
|
+
"replace(x, 'a', x)",
|
|
723
|
+
'concat(x, x)',
|
|
724
|
+
]) {
|
|
725
|
+
throws(
|
|
726
|
+
() => run(expression, { x: 'a'.repeat(600000) }),
|
|
727
|
+
(error) => error instanceof Error && error.message.includes('exceeds'),
|
|
728
|
+
);
|
|
729
|
+
}
|
|
730
|
+
throws(
|
|
731
|
+
() => run('x * 2', { x: 1e308 }),
|
|
732
|
+
(error) => error instanceof Error && error.message.includes('non-finite'),
|
|
733
|
+
);
|
|
734
|
+
});
|
|
735
|
+
|
|
736
|
+
Deno.test('large valid collections use reductions instead of argument spreading', () => {
|
|
737
|
+
const row = {
|
|
738
|
+
items: Array.from({ length: 150000 }, (_, index) => ({ x: index })),
|
|
739
|
+
};
|
|
740
|
+
strictEqual(run("minOf(items, 'x')", row), 0);
|
|
741
|
+
strictEqual(run("maxOf(items, 'x')", row), 149999);
|
|
742
|
+
});
|
|
743
|
+
|
|
744
|
+
Deno.test('date functions reject impossible dates and out-of-range arithmetic', () => {
|
|
745
|
+
strictEqual(run("dateFormat('2026-02-30')"), null);
|
|
746
|
+
strictEqual(run("dateFormat('2024-02-29')"), '2024-02-29');
|
|
747
|
+
strictEqual(run("dateFormat('Farm 12')"), null);
|
|
748
|
+
strictEqual(run("dateFormat('2026-01-01T24:00:00Z')"), null);
|
|
749
|
+
strictEqual(run("dateAdd('2026-01-01', 1000000000, 'day')"), null);
|
|
750
|
+
});
|
|
751
|
+
|
|
752
|
+
Deno.test('canonical data retains its meaning and ordering across JSON round trips', () => {
|
|
753
|
+
const input = {
|
|
754
|
+
z: [{ b: 2, a: 1 }, undefined],
|
|
755
|
+
omitted: undefined,
|
|
756
|
+
id: 123n,
|
|
757
|
+
date: new Date('2026-08-01T00:00:00Z'),
|
|
758
|
+
};
|
|
759
|
+
const normalized = normalizeExpressionDataValue(input);
|
|
760
|
+
deepStrictEqual(
|
|
761
|
+
normalized,
|
|
762
|
+
normalizeExpressionDataValue(JSON.parse(JSON.stringify(normalized))),
|
|
763
|
+
);
|
|
764
|
+
deepStrictEqual(normalized, {
|
|
765
|
+
date: '2026-08-01T00:00:00.000Z',
|
|
766
|
+
id: '123',
|
|
767
|
+
z: [{ a: 1, b: 2 }, null],
|
|
768
|
+
});
|
|
769
|
+
throws(
|
|
770
|
+
() => normalizeExpressionDataValue({ value: Infinity }),
|
|
771
|
+
(error) => error instanceof RangeError,
|
|
772
|
+
);
|
|
773
|
+
const recursive: Record<string, unknown> = {};
|
|
774
|
+
recursive.self = recursive;
|
|
775
|
+
throws(
|
|
776
|
+
() => normalizeExpressionDataValue(recursive),
|
|
777
|
+
(error) => error instanceof TypeError,
|
|
778
|
+
);
|
|
779
|
+
});
|
|
780
|
+
|
|
781
|
+
Deno.test('incremental JSON byte counts match escaped UTF-8 without serializing whole datasets', () => {
|
|
782
|
+
const value = {
|
|
783
|
+
ascii: 'a"\\\n\t\u0000',
|
|
784
|
+
unicode: 'café🍎\ud800',
|
|
785
|
+
items: [undefined, null, true, -0, 1e30],
|
|
786
|
+
};
|
|
787
|
+
const expected = new TextEncoder().encode(JSON.stringify(value)).byteLength;
|
|
788
|
+
strictEqual(getExpressionDataByteLength(value, expected), expected);
|
|
789
|
+
throws(
|
|
790
|
+
() => getExpressionDataByteLength(value, expected - 1),
|
|
791
|
+
(error) => error instanceof RangeError,
|
|
792
|
+
);
|
|
793
|
+
});
|