@jsb188/app 1.1.1 → 1.1.2
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 +57 -42
- package/src/constants/agent.ts +74 -0
- package/src/constants/app.ts +389 -0
- package/src/constants/emoji.ts +9153 -0
- package/src/constants/other.ts +13 -0
- package/src/constants/user.ts +27 -0
- package/src/constants/wsActions.ts +49 -0
- package/src/i18n/account.ts +139 -0
- package/src/i18n/agent.ts +397 -0
- 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 +124 -0
- package/src/i18n/auth.ts +128 -0
- package/src/i18n/billing.ts +7 -0
- package/src/i18n/c.ts +907 -0
- package/src/i18n/campaign.ts +919 -0
- package/src/i18n/country.ts +249 -0
- package/src/i18n/datetime.ts +66 -0
- package/src/i18n/editor.ts +58 -0
- package/src/i18n/error.ts +579 -0
- package/src/i18n/field_doc_editor.ts +21 -0
- package/src/i18n/field_doc_programs.ts +52 -0
- package/src/i18n/field_docs.ts +190 -0
- package/src/i18n/index.ts +64 -0
- package/src/i18n/log.ts +566 -0
- package/src/i18n/memory.ts +59 -0
- package/src/i18n/message.ts +13 -0
- package/src/i18n/note.ts +19 -0
- package/src/i18n/org.ts +306 -0
- package/src/i18n/schedule.ts +209 -0
- package/src/i18n/sheet.ts +339 -0
- package/src/i18n/sheet_formula.ts +165 -0
- package/src/i18n/workflow.ts +111 -0
- package/src/main.ts +89 -0
- package/src/platform.ts +166 -0
- package/src/types/ai.d.ts +63 -0
- package/src/types/app.d.ts +127 -0
- package/src/types/auth.d.ts +94 -0
- package/src/types/console.d.ts +3 -0
- package/src/types/node-polyglot.d.ts +23 -0
- package/src/types/other.d.ts +66 -0
- package/src/types.d.ts +29 -0
- package/src/utils/account.ts +97 -0
- package/src/utils/api.ts +358 -0
- package/src/utils/authSession.ts +24 -0
- package/src/utils/clientIP.ts +213 -0
- package/src/utils/color.ts +279 -0
- package/src/utils/cron.ts +310 -0
- package/src/utils/csv.ts +19 -0
- package/src/utils/datetime.ts +1394 -0
- package/src/utils/deferredSave.ts +89 -0
- package/src/utils/email.ts +39 -0
- package/src/utils/emoji.ts +174 -0
- package/src/utils/expression.helpers.ts +470 -0
- package/src/utils/expression.test.ts +793 -0
- package/src/utils/expression.ts +2261 -0
- package/src/utils/interval.ts +80 -0
- package/src/utils/language.ts +9 -0
- package/src/utils/logic.ts +299 -0
- package/src/utils/managedSession.ts +34 -0
- package/src/utils/managedSessionBff.test.ts +60 -0
- package/src/utils/managedSessionBff.ts +117 -0
- package/src/utils/number.ts +338 -0
- package/src/utils/object.ts +916 -0
- package/src/utils/promise.ts +98 -0
- package/src/utils/queryReset.ts +266 -0
- package/src/utils/recurringTask.ts +60 -0
- package/src/utils/schema.ts +293 -0
- package/src/utils/string.test.ts +278 -0
- package/src/utils/string.ts +709 -0
- package/{utils → src/utils}/timeZone.ts +531 -852
- package/src/utils/url_client.ts +206 -0
- package/tsconfig.json +12 -0
- package/constants/app.ts +0 -47
- package/constants/billing.ts +0 -146
- package/constants/emoji.ts +0 -8945
- package/constants/journal.ts +0 -74
- package/constants/user.ts +0 -36
- package/constants/ws.ts +0 -47
- package/i18n/app.ts +0 -101
- package/i18n/auth.ts +0 -63
- package/i18n/billing.ts +0 -94
- package/i18n/datetime.ts +0 -21
- package/i18n/error.ts +0 -143
- package/i18n/form.ts +0 -74
- package/i18n/index.ts +0 -5
- package/i18n/user.ts +0 -101
- package/main.ts +0 -94
- package/platform.ts +0 -125
- package/types/ai.d.ts +0 -30
- package/types/app.d.ts +0 -48
- package/types/auth.d.ts +0 -63
- package/types/form.d.ts +0 -56
- package/types/journal.d.ts +0 -24
- package/types/organization.d.ts +0 -38
- package/types/user.d.ts +0 -52
- package/types.d.ts +0 -31
- package/utils/api.ts +0 -99
- package/utils/billing.ts +0 -134
- package/utils/datetime.ts +0 -542
- package/utils/emoji.ts +0 -151
- package/utils/journal.ts +0 -17
- package/utils/logic.ts +0 -118
- package/utils/number.ts +0 -139
- package/utils/object.ts +0 -425
- package/utils/string.ts +0 -323
- package/utils/url_client.ts +0 -138
- package/utils/user.ts +0 -273
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
const FILE_CONSTANTS = {
|
|
2
|
+
divisionPrecision: 40,
|
|
3
|
+
maxDecimalPower: 1000,
|
|
4
|
+
plainNumber: /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/,
|
|
5
|
+
decoratedNumber:
|
|
6
|
+
/^([+-])?\s*(?:[$€£¥₹₩]|(?:USD|CAD|EUR|GBP|JPY|AUD|NZD|CHF|CNY|INR|KRW|MXN)\s*)?\s*([+-])?\s*((?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?|\.\d+)([eE][+-]?\d+)?\s*(%|[A-Za-z°]+(?:[ /-][A-Za-z°]+)*)?$/,
|
|
7
|
+
numericUnits: new Set(
|
|
8
|
+
'usd cad eur gbp jpy aud nzd chf cny inr krw mxn kg g mg lb lbs oz t ton tons ml l cl dl liter liters litre litres gal gallon gallons qt quart quarts pt pint pints cup cups tsp tbsp ea each pc pcs piece pieces unit units doz dozen case cases box boxes bag bags bunch bunches crate crates pack packs mm cm m km in ft yd mi s sec second seconds min minute minutes h hr hour hours day days °c °f'.split(
|
|
9
|
+
' ',
|
|
10
|
+
),
|
|
11
|
+
),
|
|
12
|
+
isoDate:
|
|
13
|
+
/^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d{1,9}))?)?(Z|[+-]\d{2}:\d{2})?)?$/,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/*
|
|
17
|
+
* Parse only a complete decimal number, including scientific notation.
|
|
18
|
+
*/
|
|
19
|
+
export function parsePlainExpressionNumber(value: unknown): number | null {
|
|
20
|
+
if (typeof value === 'number') {
|
|
21
|
+
return Number.isFinite(value) ? value : null;
|
|
22
|
+
}
|
|
23
|
+
if (
|
|
24
|
+
typeof value !== 'string' ||
|
|
25
|
+
!FILE_CONSTANTS.plainNumber.test(value.trim())
|
|
26
|
+
) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
const number = Number(value.trim());
|
|
30
|
+
return Number.isFinite(number) ? number : null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/*
|
|
34
|
+
* Parse a complete number with optional currency, grouping, units or accounting
|
|
35
|
+
* parentheses, preserving its sign and rejecting unrelated or ambiguous text.
|
|
36
|
+
*/
|
|
37
|
+
export function parseDecoratedExpressionNumber(value: unknown): number | null {
|
|
38
|
+
if (typeof value === 'boolean') {
|
|
39
|
+
return value ? 1 : 0;
|
|
40
|
+
}
|
|
41
|
+
const plain = parsePlainExpressionNumber(value);
|
|
42
|
+
if (plain !== null || typeof value !== 'string') {
|
|
43
|
+
return plain;
|
|
44
|
+
}
|
|
45
|
+
let text = value.trim();
|
|
46
|
+
const accounting = text.startsWith('(') && text.endsWith(')');
|
|
47
|
+
if (accounting) {
|
|
48
|
+
text = text.slice(1, -1).trim();
|
|
49
|
+
}
|
|
50
|
+
const match = FILE_CONSTANTS.decoratedNumber.exec(text);
|
|
51
|
+
if (
|
|
52
|
+
!match ||
|
|
53
|
+
(match[1] && match[2]) ||
|
|
54
|
+
(accounting && (match[1] || match[2])) ||
|
|
55
|
+
(match[5] &&
|
|
56
|
+
match[5] !== '%' &&
|
|
57
|
+
!match[5]
|
|
58
|
+
.toLowerCase()
|
|
59
|
+
.split(/[ /-]/)
|
|
60
|
+
.every((unit) => FILE_CONSTANTS.numericUnits.has(unit)))
|
|
61
|
+
) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
const sign = accounting || (match[1] || match[2]) === '-' ? -1 : 1;
|
|
65
|
+
const number = sign * Number(match[3].replace(/,/g, '') + (match[4] || ''));
|
|
66
|
+
return Number.isFinite(number) ? number : null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/*
|
|
70
|
+
* Split a finite number's decimal representation into a signed integer and scale.
|
|
71
|
+
*/
|
|
72
|
+
function getExpressionDecimal(value: number) {
|
|
73
|
+
if (!Number.isFinite(value)) {
|
|
74
|
+
throw new RangeError('Cannot calculate with a non-finite number.');
|
|
75
|
+
}
|
|
76
|
+
const [mantissa, exponent = '0'] = String(value).split('e');
|
|
77
|
+
const fractionLength = mantissa.includes('.')
|
|
78
|
+
? mantissa.length - mantissa.indexOf('.') - 1
|
|
79
|
+
: 0;
|
|
80
|
+
return {
|
|
81
|
+
coefficient: BigInt(mantissa.replace('.', '')),
|
|
82
|
+
scale: fractionLength - Number(exponent),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/*
|
|
87
|
+
* Bound all intermediate decimal powers independently of untrusted expression data.
|
|
88
|
+
*/
|
|
89
|
+
function expressionDecimalPower(places: number) {
|
|
90
|
+
if (
|
|
91
|
+
!Number.isInteger(places) ||
|
|
92
|
+
places < 0 ||
|
|
93
|
+
places > FILE_CONSTANTS.maxDecimalPower
|
|
94
|
+
) {
|
|
95
|
+
throw new RangeError('Decimal arithmetic exceeds the precision budget.');
|
|
96
|
+
}
|
|
97
|
+
return 10n ** BigInt(places);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/*
|
|
101
|
+
* Accumulate decimal coefficients exactly before converting the final result to
|
|
102
|
+
* a JavaScript number, retaining cents across cancellation and decimal ties.
|
|
103
|
+
*/
|
|
104
|
+
function accumulateExpressionDecimals(values: Iterable<number>) {
|
|
105
|
+
let coefficient = 0n;
|
|
106
|
+
let scale = 0;
|
|
107
|
+
let count = 0;
|
|
108
|
+
for (const value of values) {
|
|
109
|
+
const decimal = getExpressionDecimal(value);
|
|
110
|
+
const nextScale = Math.max(scale, decimal.scale);
|
|
111
|
+
coefficient =
|
|
112
|
+
coefficient * expressionDecimalPower(nextScale - scale) +
|
|
113
|
+
decimal.coefficient * expressionDecimalPower(nextScale - decimal.scale);
|
|
114
|
+
scale = nextScale;
|
|
115
|
+
count += 1;
|
|
116
|
+
}
|
|
117
|
+
return { coefficient, scale, count };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/*
|
|
121
|
+
* Convert an exact decimal to the public finite-number representation. Reject
|
|
122
|
+
* overflow and nonzero underflow instead of silently inventing Infinity or zero.
|
|
123
|
+
*/
|
|
124
|
+
function expressionDecimalToNumber(coefficient: bigint, scale: number) {
|
|
125
|
+
const value = Number(`${coefficient}e${-scale}`);
|
|
126
|
+
if (!Number.isFinite(value)) {
|
|
127
|
+
throw new RangeError('Decimal arithmetic produced a non-finite result.');
|
|
128
|
+
}
|
|
129
|
+
if (value === 0 && coefficient !== 0n) {
|
|
130
|
+
throw new RangeError(
|
|
131
|
+
'Decimal arithmetic result is below the finite-number precision range.',
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
return value;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/*
|
|
138
|
+
* Divide exact coefficients to at least 40 significant decimal digits, rounding
|
|
139
|
+
* the last digit half away from zero before conversion to a finite JS number.
|
|
140
|
+
* Repeating decimals therefore have deterministic, bounded precision.
|
|
141
|
+
*/
|
|
142
|
+
function divideExpressionDecimals(
|
|
143
|
+
numerator: bigint,
|
|
144
|
+
denominator: bigint,
|
|
145
|
+
scale: number,
|
|
146
|
+
) {
|
|
147
|
+
const negative = numerator < 0n !== denominator < 0n;
|
|
148
|
+
const absoluteNumerator = numerator < 0n ? -numerator : numerator;
|
|
149
|
+
const absoluteDenominator = denominator < 0n ? -denominator : denominator;
|
|
150
|
+
const extraPlaces = Math.max(
|
|
151
|
+
0,
|
|
152
|
+
FILE_CONSTANTS.divisionPrecision +
|
|
153
|
+
String(absoluteDenominator).length -
|
|
154
|
+
String(absoluteNumerator).length,
|
|
155
|
+
);
|
|
156
|
+
const scaled = absoluteNumerator * expressionDecimalPower(extraPlaces);
|
|
157
|
+
const rounded =
|
|
158
|
+
scaled / absoluteDenominator +
|
|
159
|
+
((scaled % absoluteDenominator) * 2n >= absoluteDenominator ? 1n : 0n);
|
|
160
|
+
return expressionDecimalToNumber(
|
|
161
|
+
negative ? -rounded : rounded,
|
|
162
|
+
scale + extraPlaces,
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/*
|
|
167
|
+
* Sum finite numbers using their decimal representations, with zero for no input.
|
|
168
|
+
*/
|
|
169
|
+
export function sumExpressionNumbers(values: Iterable<number>) {
|
|
170
|
+
const { coefficient, scale } = accumulateExpressionDecimals(values);
|
|
171
|
+
return expressionDecimalToNumber(coefficient, scale);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/*
|
|
175
|
+
* Average decimals without overflowing an intermediate sum, using the same
|
|
176
|
+
* bounded division policy as binary expression arithmetic.
|
|
177
|
+
*/
|
|
178
|
+
export function averageExpressionNumbers(values: Iterable<number>) {
|
|
179
|
+
const { coefficient, scale, count } = accumulateExpressionDecimals(values);
|
|
180
|
+
if (!count) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
return divideExpressionDecimals(coefficient, BigInt(count), scale);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/*
|
|
187
|
+
* Calculate decimal arithmetic without binary intermediate noise. Inputs and
|
|
188
|
+
* each public result remain finite JS numbers; division by zero remains null.
|
|
189
|
+
*/
|
|
190
|
+
export function calculateExpressionDecimal(
|
|
191
|
+
left: number,
|
|
192
|
+
operator: '+' | '-' | '*' | '/',
|
|
193
|
+
right: number,
|
|
194
|
+
): number | null {
|
|
195
|
+
if (operator === '+' || operator === '-') {
|
|
196
|
+
return sumExpressionNumbers([left, operator === '-' ? -right : right]);
|
|
197
|
+
}
|
|
198
|
+
const a = getExpressionDecimal(left);
|
|
199
|
+
const b = getExpressionDecimal(right);
|
|
200
|
+
if (operator === '*') {
|
|
201
|
+
return expressionDecimalToNumber(
|
|
202
|
+
a.coefficient * b.coefficient,
|
|
203
|
+
a.scale + b.scale,
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
return b.coefficient === 0n
|
|
207
|
+
? null
|
|
208
|
+
: divideExpressionDecimals(a.coefficient, b.coefficient, a.scale - b.scale);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/*
|
|
212
|
+
* Shift a finite decimal number without introducing binary multiplication noise.
|
|
213
|
+
*/
|
|
214
|
+
function shiftExpressionDecimal(value: number, places: number) {
|
|
215
|
+
const [mantissa, exponent = '0'] = String(value).split('e');
|
|
216
|
+
return Number(`${mantissa}e${Number(exponent) + places}`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/*
|
|
220
|
+
* Round the decimal representation half away from zero at 0..8 decimal places.
|
|
221
|
+
*/
|
|
222
|
+
export function roundExpressionDecimal(value: number, digits = 0) {
|
|
223
|
+
if (!Number.isFinite(value) || !Number.isFinite(digits)) {
|
|
224
|
+
throw new RangeError('Cannot round a non-finite number.');
|
|
225
|
+
}
|
|
226
|
+
const places = Math.max(0, Math.min(8, Math.trunc(digits)));
|
|
227
|
+
const shifted = shiftExpressionDecimal(Math.abs(value), places);
|
|
228
|
+
if (!Number.isFinite(shifted)) {
|
|
229
|
+
return value;
|
|
230
|
+
}
|
|
231
|
+
return (
|
|
232
|
+
Math.sign(value) * shiftExpressionDecimal(Math.round(shifted), -places)
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/*
|
|
237
|
+
* Parse a valid ISO calendar date or timestamp; timezone-less timestamps are UTC.
|
|
238
|
+
*/
|
|
239
|
+
export function parseExpressionDate(value: unknown): Date | null {
|
|
240
|
+
if (value instanceof Date) {
|
|
241
|
+
return Number.isFinite(value.getTime()) ? new Date(value.getTime()) : null;
|
|
242
|
+
}
|
|
243
|
+
if (typeof value !== 'string') {
|
|
244
|
+
return null;
|
|
245
|
+
}
|
|
246
|
+
const text = value.trim();
|
|
247
|
+
const match = FILE_CONSTANTS.isoDate.exec(text);
|
|
248
|
+
if (!match) {
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
const [, year, month, day, hour, minute, second, , zone] = match;
|
|
252
|
+
const calendar = new Date(`${year}-${month}-${day}T00:00:00Z`);
|
|
253
|
+
if (
|
|
254
|
+
!Number.isFinite(calendar.getTime()) ||
|
|
255
|
+
calendar.getUTCFullYear() !== Number(year) ||
|
|
256
|
+
calendar.getUTCMonth() + 1 !== Number(month) ||
|
|
257
|
+
calendar.getUTCDate() !== Number(day) ||
|
|
258
|
+
Number(hour || 0) > 23 ||
|
|
259
|
+
Number(minute || 0) > 59 ||
|
|
260
|
+
Number(second || 0) > 59 ||
|
|
261
|
+
(zone &&
|
|
262
|
+
zone !== 'Z' &&
|
|
263
|
+
(Number(zone.slice(1, 3)) > 23 || Number(zone.slice(4)) > 59))
|
|
264
|
+
) {
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
const parsed = new Date(text.replace(' ', 'T') + (hour && !zone ? 'Z' : ''));
|
|
268
|
+
return Number.isFinite(parsed.getTime()) ? parsed : null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/*
|
|
272
|
+
* Canonicalize JSON data before live use or persistence so retries see identical
|
|
273
|
+
* values and key order. Reject values JSON would silently discard or alter.
|
|
274
|
+
*/
|
|
275
|
+
export function normalizeExpressionDataValue(value: unknown): unknown {
|
|
276
|
+
return normalizeExpressionDataEntry(value, new WeakSet<object>());
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/*
|
|
280
|
+
* Normalize one entry while detecting recursive object graphs.
|
|
281
|
+
*/
|
|
282
|
+
function normalizeExpressionDataEntry(
|
|
283
|
+
value: unknown,
|
|
284
|
+
ancestors: WeakSet<object>,
|
|
285
|
+
): unknown {
|
|
286
|
+
if (value === undefined || value === null) {
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
if (typeof value === 'number') {
|
|
290
|
+
if (!Number.isFinite(value)) {
|
|
291
|
+
throw new RangeError('Data contains a non-finite number.');
|
|
292
|
+
}
|
|
293
|
+
return Object.is(value, -0) ? 0 : value;
|
|
294
|
+
}
|
|
295
|
+
if (typeof value === 'string' || typeof value === 'boolean') {
|
|
296
|
+
return value;
|
|
297
|
+
}
|
|
298
|
+
if (typeof value === 'bigint') {
|
|
299
|
+
return String(value);
|
|
300
|
+
}
|
|
301
|
+
if (value instanceof Date) {
|
|
302
|
+
return value.toISOString();
|
|
303
|
+
}
|
|
304
|
+
if (typeof value !== 'object' || ancestors.has(value)) {
|
|
305
|
+
throw new TypeError('Data must be an acyclic JSON value.');
|
|
306
|
+
}
|
|
307
|
+
ancestors.add(value);
|
|
308
|
+
try {
|
|
309
|
+
if (Array.isArray(value)) {
|
|
310
|
+
return Array.from(value, (entry) =>
|
|
311
|
+
normalizeExpressionDataEntry(entry, ancestors),
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
if (
|
|
315
|
+
Object.getPrototypeOf(value) !== Object.prototype &&
|
|
316
|
+
Object.getPrototypeOf(value) !== null
|
|
317
|
+
) {
|
|
318
|
+
throw new TypeError('Data objects must be plain records.');
|
|
319
|
+
}
|
|
320
|
+
return Object.fromEntries(
|
|
321
|
+
Object.entries(value)
|
|
322
|
+
.filter(([, entry]) => entry !== undefined)
|
|
323
|
+
.sort(([left], [right]) => left.localeCompare(right))
|
|
324
|
+
.map(([key, entry]) => [
|
|
325
|
+
key,
|
|
326
|
+
normalizeExpressionDataEntry(entry, ancestors),
|
|
327
|
+
]),
|
|
328
|
+
);
|
|
329
|
+
} finally {
|
|
330
|
+
ancestors.delete(value);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/*
|
|
335
|
+
* Compare values using one total order: null, decimal numbers, then text.
|
|
336
|
+
* Numeric strings share the number order; remaining values use canonical text.
|
|
337
|
+
*/
|
|
338
|
+
export function compareExpressionValues(left: unknown, right: unknown): number {
|
|
339
|
+
if (
|
|
340
|
+
left === null ||
|
|
341
|
+
left === undefined ||
|
|
342
|
+
right === null ||
|
|
343
|
+
right === undefined
|
|
344
|
+
) {
|
|
345
|
+
return left == null ? (right == null ? 0 : -1) : 1;
|
|
346
|
+
}
|
|
347
|
+
const leftNumber = parsePlainExpressionNumber(left);
|
|
348
|
+
const rightNumber = parsePlainExpressionNumber(right);
|
|
349
|
+
if (leftNumber !== null || rightNumber !== null) {
|
|
350
|
+
return leftNumber === null
|
|
351
|
+
? 1
|
|
352
|
+
: rightNumber === null
|
|
353
|
+
? -1
|
|
354
|
+
: leftNumber < rightNumber
|
|
355
|
+
? -1
|
|
356
|
+
: leftNumber > rightNumber
|
|
357
|
+
? 1
|
|
358
|
+
: 0;
|
|
359
|
+
}
|
|
360
|
+
const leftText =
|
|
361
|
+
typeof left === 'object'
|
|
362
|
+
? JSON.stringify(normalizeExpressionDataValue(left))
|
|
363
|
+
: String(left);
|
|
364
|
+
const rightText =
|
|
365
|
+
typeof right === 'object'
|
|
366
|
+
? JSON.stringify(normalizeExpressionDataValue(right))
|
|
367
|
+
: String(right);
|
|
368
|
+
return leftText < rightText ? -1 : leftText > rightText ? 1 : 0;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/*
|
|
372
|
+
* Count the UTF-8 bytes of one JSON string without allocating its escaped copy.
|
|
373
|
+
*/
|
|
374
|
+
function getJSONStringByteLength(value: string) {
|
|
375
|
+
let bytes = 2;
|
|
376
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
377
|
+
const code = value.charCodeAt(index);
|
|
378
|
+
if (code === 34 || code === 92 || [8, 9, 10, 12, 13].includes(code)) {
|
|
379
|
+
bytes += 2;
|
|
380
|
+
} else if (code < 32) {
|
|
381
|
+
bytes += 6;
|
|
382
|
+
} else if (code < 128) {
|
|
383
|
+
bytes += 1;
|
|
384
|
+
} else if (code < 2048) {
|
|
385
|
+
bytes += 2;
|
|
386
|
+
} else if (code >= 0xd800 && code <= 0xdfff) {
|
|
387
|
+
const next = value.charCodeAt(index + 1);
|
|
388
|
+
if (code <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) {
|
|
389
|
+
bytes += 4;
|
|
390
|
+
index += 1;
|
|
391
|
+
} else {
|
|
392
|
+
bytes += 6;
|
|
393
|
+
}
|
|
394
|
+
} else {
|
|
395
|
+
bytes += 3;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return bytes;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/*
|
|
402
|
+
* Measure canonical JSON data incrementally, aborting before a serialization
|
|
403
|
+
* could exceed its budget. Shared object values may be cached between rows.
|
|
404
|
+
*/
|
|
405
|
+
export function getExpressionDataByteLength(
|
|
406
|
+
value: unknown,
|
|
407
|
+
maxBytes: number,
|
|
408
|
+
cache = new WeakMap<object, number>(),
|
|
409
|
+
): number {
|
|
410
|
+
const ancestors = new WeakSet<object>();
|
|
411
|
+
let bytes = 0;
|
|
412
|
+
/*
|
|
413
|
+
* Count one entry using JSON's separators and scalar representation.
|
|
414
|
+
*/
|
|
415
|
+
function visit(entry: unknown) {
|
|
416
|
+
const start = bytes;
|
|
417
|
+
if (entry == null) {
|
|
418
|
+
bytes += 4;
|
|
419
|
+
} else if (
|
|
420
|
+
typeof entry === 'string' ||
|
|
421
|
+
typeof entry === 'bigint' ||
|
|
422
|
+
entry instanceof Date
|
|
423
|
+
) {
|
|
424
|
+
const text = entry instanceof Date ? entry.toISOString() : String(entry);
|
|
425
|
+
if (text.length > maxBytes - bytes) {
|
|
426
|
+
throw new RangeError('Data exceeds its byte budget.');
|
|
427
|
+
}
|
|
428
|
+
bytes += getJSONStringByteLength(text);
|
|
429
|
+
} else if (
|
|
430
|
+
(typeof entry === 'number' && Number.isFinite(entry)) ||
|
|
431
|
+
typeof entry === 'boolean'
|
|
432
|
+
) {
|
|
433
|
+
bytes += String(entry).length;
|
|
434
|
+
} else if (typeof entry === 'object') {
|
|
435
|
+
if (ancestors.has(entry)) {
|
|
436
|
+
throw new TypeError('Data must be an acyclic JSON value.');
|
|
437
|
+
}
|
|
438
|
+
const cached = cache.get(entry);
|
|
439
|
+
if (cached !== undefined) {
|
|
440
|
+
bytes += cached;
|
|
441
|
+
} else {
|
|
442
|
+
ancestors.add(entry);
|
|
443
|
+
bytes += 2;
|
|
444
|
+
if (Array.isArray(entry)) {
|
|
445
|
+
for (let index = 0; index < entry.length; index += 1) {
|
|
446
|
+
bytes += index ? 1 : 0;
|
|
447
|
+
visit(entry[index]);
|
|
448
|
+
}
|
|
449
|
+
} else {
|
|
450
|
+
let count = 0;
|
|
451
|
+
for (const [key, nested] of Object.entries(entry)) {
|
|
452
|
+
if (nested !== undefined) {
|
|
453
|
+
bytes += (count++ ? 1 : 0) + getJSONStringByteLength(key) + 1;
|
|
454
|
+
visit(nested);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
ancestors.delete(entry);
|
|
459
|
+
cache.set(entry, bytes - start);
|
|
460
|
+
}
|
|
461
|
+
} else {
|
|
462
|
+
throw new TypeError('Data contains an unsupported JSON value.');
|
|
463
|
+
}
|
|
464
|
+
if (bytes > maxBytes) {
|
|
465
|
+
throw new RangeError('Data exceeds its byte budget.');
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
visit(value);
|
|
469
|
+
return bytes;
|
|
470
|
+
}
|