@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,89 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Debounce immutable snapshots and serialize saves, keeping only the latest pending
|
|
3
|
+
* value. A failed value remains queued for the next edit, resume, or explicit flush.
|
|
4
|
+
*/
|
|
5
|
+
export function createDeferredSaveQueue<T>(p: { delayMs: number; onError: (error: unknown) => void; onIdle?: () => void }) {
|
|
6
|
+
let pending: { value: T; save: (value: T) => Promise<void> } | null = null;
|
|
7
|
+
let inFlight = false;
|
|
8
|
+
let paused = false;
|
|
9
|
+
let ready = false;
|
|
10
|
+
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* Clear the debounce without dropping the latest unsaved snapshot.
|
|
14
|
+
*/
|
|
15
|
+
function clearTimer() {
|
|
16
|
+
if (timer !== null) clearTimeout(timer);
|
|
17
|
+
timer = null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/*
|
|
21
|
+
* Start a ready snapshot only after the previous request has fully settled.
|
|
22
|
+
*/
|
|
23
|
+
async function drain() {
|
|
24
|
+
if (inFlight || paused || !ready || !pending) return;
|
|
25
|
+
const request = pending;
|
|
26
|
+
pending = null;
|
|
27
|
+
ready = false;
|
|
28
|
+
inFlight = true;
|
|
29
|
+
try {
|
|
30
|
+
await request.save(request.value);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (!pending) {
|
|
33
|
+
pending = request;
|
|
34
|
+
ready = false;
|
|
35
|
+
}
|
|
36
|
+
p.onError(error);
|
|
37
|
+
} finally {
|
|
38
|
+
inFlight = false;
|
|
39
|
+
if (pending) void drain();
|
|
40
|
+
else p.onIdle?.();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/*
|
|
45
|
+
* Require a full quiet period after every edit or completed interaction.
|
|
46
|
+
*/
|
|
47
|
+
function schedule() {
|
|
48
|
+
clearTimer();
|
|
49
|
+
ready = false;
|
|
50
|
+
if (!pending || paused) return;
|
|
51
|
+
timer = setTimeout(() => {
|
|
52
|
+
timer = null;
|
|
53
|
+
ready = true;
|
|
54
|
+
void drain();
|
|
55
|
+
}, p.delayMs);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
/*
|
|
60
|
+
* Replace intermediate edits with the newest complete snapshot and its owner-bound saver.
|
|
61
|
+
*/
|
|
62
|
+
queue(value: T, save: (value: T) => Promise<void>) {
|
|
63
|
+
pending = { value, save };
|
|
64
|
+
schedule();
|
|
65
|
+
},
|
|
66
|
+
/*
|
|
67
|
+
* Pause during a gesture, then restart the quiet period when it ends.
|
|
68
|
+
*/
|
|
69
|
+
setPaused(value: boolean) {
|
|
70
|
+
if (paused === value) return;
|
|
71
|
+
paused = value;
|
|
72
|
+
schedule();
|
|
73
|
+
},
|
|
74
|
+
/*
|
|
75
|
+
* Flush committed work on navigation, without overlapping an earlier request.
|
|
76
|
+
*/
|
|
77
|
+
flush() {
|
|
78
|
+
clearTimer();
|
|
79
|
+
ready = true;
|
|
80
|
+
void drain();
|
|
81
|
+
},
|
|
82
|
+
/*
|
|
83
|
+
* Retain the owner while a request or an unsaved snapshot still exists.
|
|
84
|
+
*/
|
|
85
|
+
hasPending() {
|
|
86
|
+
return inFlight || pending !== null;
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { DO_NOT_REPLY_EMAIL_DOMAINS, DOMAIN_TO_INBOX, EMAIL_BASE_DOMAINS, OUTBOUND_EMAIL_DOMAINS } from '../constants/agent.ts';
|
|
2
|
+
import { APP_EMAILS } from '../constants/app.ts';
|
|
3
|
+
|
|
4
|
+
export type AgentEmailKind = 'ASSISTANT' | 'OUTBOUND' | 'SYSTEM';
|
|
5
|
+
|
|
6
|
+
/*
|
|
7
|
+
* Permit replies only from a supported receiving inbox, including the existing outbound campaign reply channel.
|
|
8
|
+
*/
|
|
9
|
+
export function isSupportedEmailReplyAddress(email?: string | null): boolean {
|
|
10
|
+
const mailbox = email?.trim().toLowerCase();
|
|
11
|
+
if (!mailbox || !/^[^\s<>@,;]+@[^\s<>@,;]+$/.test(mailbox)) return false;
|
|
12
|
+
const domain = mailbox.slice(mailbox.lastIndexOf('@') + 1);
|
|
13
|
+
return Object.hasOwn(DOMAIN_TO_INBOX, domain) || OUTBOUND_EMAIL_DOMAINS.includes(domain);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/*
|
|
17
|
+
* Return which of our own mailboxes (agent inboxes, campaign sender, or platform address) an email belongs to.
|
|
18
|
+
*/
|
|
19
|
+
export function getAgentEmailKind(email?: string | null): AgentEmailKind | null {
|
|
20
|
+
const mailbox = email?.trim().toLowerCase();
|
|
21
|
+
if (!mailbox || !/^[^\s<>@,;]+@[^\s<>@,;]+$/.test(mailbox)) return null;
|
|
22
|
+
const domain = mailbox.slice(mailbox.lastIndexOf('@') + 1);
|
|
23
|
+
if (OUTBOUND_EMAIL_DOMAINS.includes(domain)) return 'OUTBOUND';
|
|
24
|
+
if (DO_NOT_REPLY_EMAIL_DOMAINS.includes(domain) || Object.values(APP_EMAILS).includes(mailbox)) return 'SYSTEM';
|
|
25
|
+
if (DOMAIN_TO_INBOX[domain]) return DOMAIN_TO_INBOX[domain];
|
|
26
|
+
if (
|
|
27
|
+
EMAIL_BASE_DOMAINS.some((baseDomain) => {
|
|
28
|
+
const suffix = `.${baseDomain}`;
|
|
29
|
+
return domain.endsWith(suffix) && /^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(domain.slice(0, -suffix.length));
|
|
30
|
+
})
|
|
31
|
+
) {
|
|
32
|
+
return 'SYSTEM';
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/*
|
|
38
|
+
* Return a default display name only for mailboxes on our configured agent domains.
|
|
39
|
+
*/
|
|
40
|
+
export function getAgentEmailDisplayName(email?: string | null): string | null {
|
|
41
|
+
const kind = getAgentEmailKind(email);
|
|
42
|
+
return kind === 'OUTBOUND' ? 'Email campaign' : kind === 'ASSISTANT' ? 'Agent' : null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/*
|
|
46
|
+
* Display a saved mailbox name, then a known domain name, then its address.
|
|
47
|
+
*/
|
|
48
|
+
export function getEmailMailboxDisplayName(email?: string | null, name?: string | null): string | null {
|
|
49
|
+
return name?.trim() || getAgentEmailDisplayName(email) || email || null;
|
|
50
|
+
}
|
|
@@ -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
|
+
}
|