@jsb188/app 1.1.10 → 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 +57 -45
- package/src/constants/agent.ts +51 -41
- package/src/constants/app.ts +34 -13
- package/src/constants/emoji.ts +9144 -8936
- package/src/constants/other.ts +1 -5
- package/src/constants/user.ts +16 -25
- package/src/constants/wsActions.ts +27 -0
- package/src/i18n/account.ts +129 -107
- package/src/i18n/agent.ts +331 -73
- 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 +107 -91
- package/src/i18n/auth.ts +117 -102
- package/src/i18n/billing.ts +5 -92
- package/src/i18n/c.ts +907 -0
- package/src/i18n/campaign.ts +919 -0
- package/src/i18n/country.ts +248 -248
- package/src/i18n/datetime.ts +32 -26
- package/src/i18n/editor.ts +58 -0
- package/src/i18n/error.ts +577 -192
- 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 +42 -19
- package/src/i18n/log.ts +272 -238
- package/src/i18n/memory.ts +53 -0
- package/src/i18n/message.ts +3 -4
- package/src/i18n/note.ts +19 -0
- package/src/i18n/org.ts +164 -109
- package/src/i18n/schedule.ts +209 -0
- package/src/i18n/sheet.ts +333 -4
- package/src/i18n/sheet_formula.ts +165 -0
- package/src/i18n/workflow.ts +109 -23
- package/src/main.ts +68 -64
- package/src/platform.ts +90 -49
- package/src/types/ai.d.ts +52 -23
- package/src/types/app.d.ts +95 -44
- package/src/types/auth.d.ts +66 -40
- package/src/types/console.d.ts +3 -0
- package/src/types/other.d.ts +13 -6
- package/src/types.d.ts +12 -14
- package/src/utils/account.ts +64 -58
- package/src/utils/api.ts +326 -77
- 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 +49 -18
- package/src/utils/csv.ts +19 -0
- package/src/utils/datetime.ts +445 -117
- package/src/utils/deferredSave.ts +89 -0
- package/src/utils/email.ts +50 -0
- package/src/utils/emoji.ts +107 -84
- 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 +141 -121
- 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 +85 -38
- package/src/utils/object.ts +227 -30
- 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 +205 -208
- package/src/utils/string.ts +426 -463
- package/src/utils/timeZone.ts +309 -851
- package/src/utils/url_client.ts +145 -72
- package/tsconfig.json +10 -10
- package/src/constants/billing.ts +0 -146
- package/src/i18n/form.ts +0 -376
- package/src/i18n/product.ts +0 -76
- package/src/types/form.d.ts +0 -97
- package/src/utils/billing.ts +0 -134
package/src/utils/string.ts
CHANGED
|
@@ -1,38 +1,39 @@
|
|
|
1
|
-
import i18n from
|
|
2
|
-
import type { AddressObj } from
|
|
1
|
+
import i18n from '../i18n/index.ts';
|
|
2
|
+
import type { AddressObj } from '../types/other.d.ts';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Combine list of class names
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
8
|
export function cn(...classNames: (string | boolean | undefined | null)[]) {
|
|
9
|
-
|
|
9
|
+
return classNames.filter(Boolean).join(' ');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Escape user-controlled text before inserting it into an HTML string.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export function escapeHTML(value: string): string {
|
|
17
|
+
return value.replace(/[&<>"']/g, (character) => `&#${character.charCodeAt(0)};`);
|
|
10
18
|
}
|
|
11
19
|
|
|
12
20
|
/**
|
|
13
21
|
* Shorten text to space with length limit
|
|
14
22
|
*/
|
|
15
23
|
|
|
16
|
-
export function shortenToSpace(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
): string {
|
|
21
|
-
if (!str || str.length < len) {
|
|
22
|
-
return str;
|
|
23
|
-
}
|
|
24
|
+
export function shortenToSpace(str: string, len: number = 100, ellipsis: string = '...'): string {
|
|
25
|
+
if (!str || str.length < len) {
|
|
26
|
+
return str;
|
|
27
|
+
}
|
|
24
28
|
|
|
25
|
-
|
|
26
|
-
|
|
29
|
+
const trimmed = str.substring(0, len);
|
|
30
|
+
const spaceIndex = trimmed.lastIndexOf(' ');
|
|
27
31
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
if (spaceIndex < 0) {
|
|
33
|
+
return trimmed.substring(0, len) + ellipsis;
|
|
34
|
+
}
|
|
31
35
|
|
|
32
|
-
|
|
33
|
-
trimmed.substring(0, Math.min(trimmed.length, spaceIndex)) +
|
|
34
|
-
ellipsis
|
|
35
|
-
);
|
|
36
|
+
return trimmed.substring(0, Math.min(trimmed.length, spaceIndex)) + ellipsis;
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
/**
|
|
@@ -40,29 +41,24 @@ export function shortenToSpace(
|
|
|
40
41
|
*/
|
|
41
42
|
|
|
42
43
|
export function guessFirstName(str: string, len: number = 40): string {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
?.trim() || "";
|
|
46
|
-
const spaceIx = shortened.indexOf(" ");
|
|
44
|
+
let shortened = str?.replace(/^(a|an|the|da|de|that|there|this|than|then)\b/i, '')?.trim() || '';
|
|
45
|
+
const spaceIx = shortened.indexOf(' ');
|
|
47
46
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
if (spaceIx >= 0) {
|
|
48
|
+
shortened = shortened.substring(0, spaceIx + 1);
|
|
49
|
+
}
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
if (len < shortened.length) {
|
|
52
|
+
shortened = shortened.substring(0, len);
|
|
53
|
+
}
|
|
55
54
|
|
|
56
|
-
|
|
57
|
-
/\b(a|an|the|da|de|that|there|this|than|then)$/i,
|
|
58
|
-
"",
|
|
59
|
-
);
|
|
55
|
+
const articleReplacedEnd = shortened.replace(/\b(a|an|the|da|de|that|there|this|than|then)$/i, '');
|
|
60
56
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
57
|
+
if (articleReplacedEnd.length < 5) {
|
|
58
|
+
return shortened.trim();
|
|
59
|
+
}
|
|
64
60
|
|
|
65
|
-
|
|
61
|
+
return articleReplacedEnd.trim();
|
|
66
62
|
}
|
|
67
63
|
|
|
68
64
|
/**
|
|
@@ -70,24 +66,22 @@ export function guessFirstName(str: string, len: number = 40): string {
|
|
|
70
66
|
*/
|
|
71
67
|
|
|
72
68
|
export function addIndefiniteArticle(word: string) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return `${article} ${word}`;
|
|
69
|
+
const raw = (word || '').trim().toLowerCase();
|
|
70
|
+
|
|
71
|
+
let article;
|
|
72
|
+
if (!raw) {
|
|
73
|
+
article = 'a';
|
|
74
|
+
} else if (/^(honest|honor|hour|heir)\b/.test(raw)) {
|
|
75
|
+
// Common silent-h words use "an"
|
|
76
|
+
article = 'an';
|
|
77
|
+
} else if (/^(uni([^nmd]|$)|use|user|euro|one|once|ubiquit|ufo|ukulele)\b/.test(raw)) {
|
|
78
|
+
// Common "you"/"w" sound starts use "a"
|
|
79
|
+
article = 'a';
|
|
80
|
+
} else {
|
|
81
|
+
article = /^[aeiou]/.test(raw) ? 'an' : 'a';
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return `${article} ${word}`;
|
|
91
85
|
}
|
|
92
86
|
|
|
93
87
|
/**
|
|
@@ -95,10 +89,10 @@ export function addIndefiniteArticle(word: string) {
|
|
|
95
89
|
*/
|
|
96
90
|
|
|
97
91
|
export function isEmail(str: string) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
92
|
+
// return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str);
|
|
93
|
+
const emailRegex =
|
|
94
|
+
/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
|
|
95
|
+
return !!str && emailRegex.test(str.trim());
|
|
102
96
|
}
|
|
103
97
|
|
|
104
98
|
/**
|
|
@@ -107,25 +101,22 @@ export function isEmail(str: string) {
|
|
|
107
101
|
*/
|
|
108
102
|
|
|
109
103
|
export function convertToInternationalPhone(str: string) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
// If it doesn't start with a country code, assume it's already in international format
|
|
128
|
-
return "+" + digits;
|
|
104
|
+
// Check if string has a character that isn't one of [(, ), +, -, " ", 0-9]
|
|
105
|
+
if (/[^0-9+\-\s().]/.test(str)) {
|
|
106
|
+
// Invalid
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const digits = str.replace(/[^0-9]/g, '');
|
|
111
|
+
if (!digits || digits.startsWith('0')) {
|
|
112
|
+
// Invalid
|
|
113
|
+
return null;
|
|
114
|
+
} else if (!digits.startsWith('1') && digits.length === 10) {
|
|
115
|
+
return '+1' + digits;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// If it doesn't start with a country code, assume it's already in international format
|
|
119
|
+
return '+' + digits;
|
|
129
120
|
}
|
|
130
121
|
|
|
131
122
|
/**
|
|
@@ -134,18 +125,15 @@ export function convertToInternationalPhone(str: string) {
|
|
|
134
125
|
* NOTE: This will convert phone-like strings to international format
|
|
135
126
|
*/
|
|
136
127
|
|
|
137
|
-
export function isPhone(
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
)
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
return /^\+[0-9]{7,15}$/.test(phoneNumber);
|
|
128
|
+
export function isPhone(str: string, doNotConvertInternational: boolean = false) {
|
|
129
|
+
let phoneNumber: string | null = str.trim();
|
|
130
|
+
if (!doNotConvertInternational) {
|
|
131
|
+
phoneNumber = convertToInternationalPhone(phoneNumber);
|
|
132
|
+
if (!phoneNumber) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return /^\+[0-9]{7,15}$/.test(phoneNumber);
|
|
149
137
|
}
|
|
150
138
|
|
|
151
139
|
/**
|
|
@@ -158,22 +146,21 @@ export function isPhone(
|
|
|
158
146
|
*/
|
|
159
147
|
|
|
160
148
|
export function passwordStrength(str: string, minLength: number = 8) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
};
|
|
149
|
+
const isLongEnough = str.length >= minLength;
|
|
150
|
+
const hasLowercase = /[a-z]/.test(str);
|
|
151
|
+
const hasUppercase = /[A-Z]/.test(str);
|
|
152
|
+
const hasDigit = /\d/.test(str);
|
|
153
|
+
const hasSpecial = /\W/.test(str);
|
|
154
|
+
const valid = isLongEnough && hasLowercase && hasUppercase && hasDigit && hasSpecial;
|
|
155
|
+
// return /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*\W)[a-zA-Z\d\W]{8,}$/.test(str);
|
|
156
|
+
return {
|
|
157
|
+
valid,
|
|
158
|
+
isLongEnough,
|
|
159
|
+
hasLowercase,
|
|
160
|
+
hasUppercase,
|
|
161
|
+
hasDigit,
|
|
162
|
+
hasSpecial,
|
|
163
|
+
};
|
|
177
164
|
}
|
|
178
165
|
|
|
179
166
|
/**
|
|
@@ -181,7 +168,7 @@ export function passwordStrength(str: string, minLength: number = 8) {
|
|
|
181
168
|
*/
|
|
182
169
|
|
|
183
170
|
export function getTimeBasedUnique() {
|
|
184
|
-
|
|
171
|
+
return Date.now().toString(36) + Math.random().toString(36).substring(2);
|
|
185
172
|
}
|
|
186
173
|
|
|
187
174
|
/**
|
|
@@ -189,33 +176,31 @@ export function getTimeBasedUnique() {
|
|
|
189
176
|
*/
|
|
190
177
|
|
|
191
178
|
export function getAlphanumeric(str: string, space: boolean = false) {
|
|
192
|
-
|
|
179
|
+
return str.replace(space ? /[^a-z0-9\s]/gi : /[^a-z0-9]/gi, '');
|
|
193
180
|
}
|
|
194
181
|
|
|
195
182
|
/**
|
|
196
183
|
* Build normalized unique list of names from raw search inputs
|
|
197
184
|
*/
|
|
198
185
|
|
|
199
|
-
export function getSearchNames(
|
|
200
|
-
|
|
201
|
-
)
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
.map((s) => s.trim())
|
|
205
|
-
.filter(Boolean);
|
|
186
|
+
export function getSearchNames(namesList: (string | null | undefined)[] = []): string[] {
|
|
187
|
+
const parsedNames = (namesList || [])
|
|
188
|
+
.flatMap((s) => (s || '').split(/\s*(?:,|;|\n|\band\b)\s*/i))
|
|
189
|
+
.map((s) => s.trim())
|
|
190
|
+
.filter(Boolean);
|
|
206
191
|
|
|
207
|
-
|
|
208
|
-
|
|
192
|
+
const uniqueNames: string[] = [];
|
|
193
|
+
const seen = new Set<string>();
|
|
209
194
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
195
|
+
for (const name of parsedNames) {
|
|
196
|
+
const key = name.toLowerCase();
|
|
197
|
+
if (!seen.has(key)) {
|
|
198
|
+
seen.add(key);
|
|
199
|
+
uniqueNames.push(name);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
217
202
|
|
|
218
|
-
|
|
203
|
+
return uniqueNames;
|
|
219
204
|
}
|
|
220
205
|
|
|
221
206
|
/**
|
|
@@ -223,17 +208,14 @@ export function getSearchNames(
|
|
|
223
208
|
*/
|
|
224
209
|
|
|
225
210
|
export function regexFromText(text: string) {
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
211
|
+
const words = getAlphanumeric(text, true)
|
|
212
|
+
.split(/\s+/)
|
|
213
|
+
.filter((s) => s.length >= 2);
|
|
214
|
+
if (!words.length) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
232
217
|
|
|
233
|
-
|
|
234
|
-
"\\b" + (words.length === 1 ? words[0] : "(?:" + words.join("|") + ")"),
|
|
235
|
-
"gi",
|
|
236
|
-
);
|
|
218
|
+
return new RegExp('\\b' + (words.length === 1 ? words[0] : '(?:' + words.join('|') + ')'), 'gi');
|
|
237
219
|
}
|
|
238
220
|
|
|
239
221
|
/**
|
|
@@ -241,10 +223,10 @@ export function regexFromText(text: string) {
|
|
|
241
223
|
*/
|
|
242
224
|
|
|
243
225
|
export function ucFirst(str: string) {
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
226
|
+
if (!str) {
|
|
227
|
+
return '';
|
|
228
|
+
}
|
|
229
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
248
230
|
}
|
|
249
231
|
|
|
250
232
|
/**
|
|
@@ -252,47 +234,49 @@ export function ucFirst(str: string) {
|
|
|
252
234
|
*/
|
|
253
235
|
|
|
254
236
|
export function camelCaseToDash(str: string) {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
237
|
+
if (/^[A-Z0-9]+$/.test(str)) {
|
|
238
|
+
return str.toLowerCase();
|
|
239
|
+
}
|
|
240
|
+
return str.replace(/[A-Z0-9]/g, (m, i) => {
|
|
241
|
+
return `${i ? '-' : ''}${m.toLowerCase()}`;
|
|
242
|
+
});
|
|
261
243
|
}
|
|
262
244
|
|
|
263
245
|
/**
|
|
264
246
|
* Shorten text to a paragraph length
|
|
265
247
|
*/
|
|
266
248
|
|
|
267
|
-
export function shortenToParagraph(
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
249
|
+
export function shortenToParagraph(str: string, len: number, replaceDoubleLineBreaks?: boolean) {
|
|
250
|
+
let trimedToParagraph = str || '';
|
|
251
|
+
if (replaceDoubleLineBreaks) {
|
|
252
|
+
trimedToParagraph = trimedToParagraph.replace(/\n\s*\n/g, '\n');
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
trimedToParagraph = trimedToParagraph
|
|
256
|
+
.split('\n')
|
|
257
|
+
.reduce(
|
|
258
|
+
(acc, str) => {
|
|
259
|
+
if (acc.done) {
|
|
260
|
+
return acc;
|
|
261
|
+
}
|
|
262
|
+
acc.done = acc.value.length + str.length >= len;
|
|
263
|
+
acc.value = acc.done ? acc.value : acc.value + '\n' + str;
|
|
264
|
+
return acc;
|
|
265
|
+
},
|
|
266
|
+
{
|
|
267
|
+
done: false,
|
|
268
|
+
value: '',
|
|
269
|
+
},
|
|
270
|
+
)
|
|
271
|
+
.value.trim();
|
|
272
|
+
|
|
273
|
+
if (!trimedToParagraph) {
|
|
274
|
+
return shortenToSpace(str || '', len);
|
|
275
|
+
} else if (trimedToParagraph.length <= len) {
|
|
276
|
+
return trimedToParagraph;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return shortenToSpace(trimedToParagraph, len);
|
|
296
280
|
}
|
|
297
281
|
|
|
298
282
|
/**
|
|
@@ -300,14 +284,14 @@ export function shortenToParagraph(
|
|
|
300
284
|
*/
|
|
301
285
|
|
|
302
286
|
export function isInvisibleString(str: string) {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
287
|
+
if (!str?.trim()) {
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
306
290
|
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
291
|
+
// eslint-disable-next-line no-misleading-character-class
|
|
292
|
+
const invisRegex = /[ㅤ ️⠀ْٔ]/g;
|
|
293
|
+
const checkStr = str.replace(invisRegex, ' '); // .replace(/[ㅤ ️⠀]/gi, ' ');
|
|
294
|
+
return !checkStr.trim();
|
|
311
295
|
}
|
|
312
296
|
|
|
313
297
|
/**
|
|
@@ -316,32 +300,25 @@ export function isInvisibleString(str: string) {
|
|
|
316
300
|
*/
|
|
317
301
|
|
|
318
302
|
type AnyLineItem = string | undefined | false | null;
|
|
319
|
-
type BuildSingleTextConditionalLine = [
|
|
320
|
-
string[],
|
|
321
|
-
BuildSingleTextLine | BuildSingleTextLine[],
|
|
322
|
-
];
|
|
303
|
+
type BuildSingleTextConditionalLine = [string[], BuildSingleTextLine | BuildSingleTextLine[]];
|
|
323
304
|
type BuildSingleTextConditionalLineGroup = BuildSingleTextConditionalLine[];
|
|
324
305
|
interface BuildSingleTextFeatureMap {
|
|
325
|
-
|
|
306
|
+
[key: string]: BuildSingleTextLine | BuildSingleTextLine[];
|
|
326
307
|
}
|
|
327
308
|
|
|
328
309
|
export type BuildSingleTextLine =
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
310
|
+
| AnyLineItem
|
|
311
|
+
| BuildSingleTextFeatureMap
|
|
312
|
+
| BuildSingleTextConditionalLine
|
|
313
|
+
| BuildSingleTextConditionalLineGroup
|
|
314
|
+
| BuildSingleTextLine[];
|
|
333
315
|
|
|
334
316
|
/*
|
|
335
317
|
* Check whether a buildSingleText line uses the all-features conditional tuple syntax.
|
|
336
318
|
*/
|
|
337
319
|
|
|
338
|
-
function isBuildSingleTextConditionalLine(
|
|
339
|
-
|
|
340
|
-
): line is BuildSingleTextConditionalLine {
|
|
341
|
-
return Array.isArray(line) &&
|
|
342
|
-
line.length === 2 &&
|
|
343
|
-
Array.isArray(line[0]) &&
|
|
344
|
-
line[0].every((feature) => typeof feature === "string");
|
|
320
|
+
function isBuildSingleTextConditionalLine(line: BuildSingleTextLine | BuildSingleTextLine[]): line is BuildSingleTextConditionalLine {
|
|
321
|
+
return Array.isArray(line) && line.length === 2 && Array.isArray(line[0]) && line[0].every((feature) => typeof feature === 'string');
|
|
345
322
|
}
|
|
346
323
|
|
|
347
324
|
/*
|
|
@@ -349,41 +326,33 @@ function isBuildSingleTextConditionalLine(
|
|
|
349
326
|
*/
|
|
350
327
|
|
|
351
328
|
function isBuildSingleTextConditionalLineGroup(
|
|
352
|
-
|
|
329
|
+
line: BuildSingleTextLine | BuildSingleTextLine[],
|
|
353
330
|
): line is BuildSingleTextConditionalLineGroup {
|
|
354
|
-
|
|
355
|
-
line.length > 0 &&
|
|
356
|
-
line.every((lineItem) => isBuildSingleTextConditionalLine(lineItem));
|
|
331
|
+
return Array.isArray(line) && line.length > 0 && line.every((lineItem) => isBuildSingleTextConditionalLine(lineItem));
|
|
357
332
|
}
|
|
358
333
|
|
|
359
334
|
/*
|
|
360
335
|
* Check whether a buildSingleText requirement matches the enabled features.
|
|
361
336
|
*/
|
|
362
337
|
|
|
363
|
-
function doesBuildSingleTextRequirementMatch(
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
) {
|
|
367
|
-
const isNegatedRequirement = requirement.startsWith("!");
|
|
368
|
-
const feature = isNegatedRequirement ? requirement.slice(1) : requirement;
|
|
338
|
+
function doesBuildSingleTextRequirementMatch(requirement: string, features: string[]) {
|
|
339
|
+
const isNegatedRequirement = requirement.startsWith('!');
|
|
340
|
+
const feature = isNegatedRequirement ? requirement.slice(1) : requirement;
|
|
369
341
|
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
342
|
+
if (!feature) {
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
373
345
|
|
|
374
|
-
|
|
375
|
-
|
|
346
|
+
const hasFeature = features.includes(feature);
|
|
347
|
+
return isNegatedRequirement ? !hasFeature : hasFeature;
|
|
376
348
|
}
|
|
377
349
|
|
|
378
350
|
/*
|
|
379
351
|
* Check whether a buildSingleText conditional line matches the enabled features.
|
|
380
352
|
*/
|
|
381
353
|
|
|
382
|
-
function doesBuildSingleTextConditionalLineMatch(
|
|
383
|
-
|
|
384
|
-
features: string[],
|
|
385
|
-
) {
|
|
386
|
-
return conditionalLine[0].every((feature) => doesBuildSingleTextRequirementMatch(feature, features));
|
|
354
|
+
function doesBuildSingleTextConditionalLineMatch(conditionalLine: BuildSingleTextConditionalLine, features: string[]) {
|
|
355
|
+
return conditionalLine[0].every((feature) => doesBuildSingleTextRequirementMatch(feature, features));
|
|
387
356
|
}
|
|
388
357
|
|
|
389
358
|
/*
|
|
@@ -391,88 +360,78 @@ function doesBuildSingleTextConditionalLineMatch(
|
|
|
391
360
|
*/
|
|
392
361
|
|
|
393
362
|
function getBuildSingleTextConditionalText(
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
363
|
+
conditionalLines: BuildSingleTextLine | BuildSingleTextLine[],
|
|
364
|
+
delemiter: string,
|
|
365
|
+
features: string[],
|
|
397
366
|
) {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
)
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
return acc;
|
|
467
|
-
}
|
|
468
|
-
if (line || acc[acc.length - 1] !== "") {
|
|
469
|
-
acc.push(line);
|
|
470
|
-
}
|
|
471
|
-
return acc;
|
|
472
|
-
}, [] as string[])
|
|
473
|
-
.join(delemiter)
|
|
474
|
-
.trim()
|
|
475
|
-
.replace(/^[ \t]+(?=[A-Za-z0-9])/gm, '');
|
|
367
|
+
return buildSingleText(
|
|
368
|
+
Array.isArray(conditionalLines) && !isBuildSingleTextConditionalLine(conditionalLines) ? conditionalLines : [conditionalLines],
|
|
369
|
+
delemiter,
|
|
370
|
+
features,
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/* Build prompt text from nested sections, feature conditions, and explicit blank lines. */
|
|
375
|
+
export function buildSingleText(lines: BuildSingleTextLine[], delemiter: string = '\n', features: string[] = []) {
|
|
376
|
+
return lines
|
|
377
|
+
.reduce<string[]>((acc, line_) => {
|
|
378
|
+
const line = line_ || '';
|
|
379
|
+
if (isBuildSingleTextConditionalLineGroup(line)) {
|
|
380
|
+
const matchedConditionalLine = line.find((conditionalLine) =>
|
|
381
|
+
doesBuildSingleTextConditionalLineMatch(conditionalLine, features),
|
|
382
|
+
);
|
|
383
|
+
|
|
384
|
+
if (!matchedConditionalLine) {
|
|
385
|
+
return acc;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
const conditionalText = getBuildSingleTextConditionalText(matchedConditionalLine[1], delemiter, features);
|
|
389
|
+
|
|
390
|
+
if (conditionalText || acc[acc.length - 1] !== '') {
|
|
391
|
+
acc.push(conditionalText);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return acc;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
if (isBuildSingleTextConditionalLine(line)) {
|
|
398
|
+
const [, conditionalLines] = line;
|
|
399
|
+
|
|
400
|
+
if (doesBuildSingleTextConditionalLineMatch(line, features)) {
|
|
401
|
+
const conditionalText = getBuildSingleTextConditionalText(conditionalLines, delemiter, features);
|
|
402
|
+
|
|
403
|
+
if (conditionalText || acc[acc.length - 1] !== '') {
|
|
404
|
+
acc.push(conditionalText);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
return acc;
|
|
409
|
+
}
|
|
410
|
+
if (Array.isArray(line)) {
|
|
411
|
+
const nestedText = buildSingleText(line, delemiter, features);
|
|
412
|
+
if (nestedText || acc[acc.length - 1] !== '') {
|
|
413
|
+
acc.push(nestedText);
|
|
414
|
+
}
|
|
415
|
+
return acc;
|
|
416
|
+
}
|
|
417
|
+
if (line && typeof line === 'object') {
|
|
418
|
+
for (const key in line) {
|
|
419
|
+
const lineItem = line[key];
|
|
420
|
+
if (features.includes(key)) {
|
|
421
|
+
const nextLines = Array.isArray(lineItem) && !isBuildSingleTextConditionalLine(lineItem) ? lineItem : [lineItem];
|
|
422
|
+
acc.push(buildSingleText(nextLines, delemiter, features));
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return acc;
|
|
426
|
+
}
|
|
427
|
+
if (line || acc[acc.length - 1] !== '') {
|
|
428
|
+
acc.push(line);
|
|
429
|
+
}
|
|
430
|
+
return acc;
|
|
431
|
+
}, [])
|
|
432
|
+
.join(delemiter)
|
|
433
|
+
.trim()
|
|
434
|
+
.replace(/^[ \t]+(?=[A-Za-z0-9])/gm, '');
|
|
476
435
|
}
|
|
477
436
|
|
|
478
437
|
/**
|
|
@@ -484,15 +443,11 @@ export function buildSingleText(
|
|
|
484
443
|
* @returns Joined string with special last separator
|
|
485
444
|
*/
|
|
486
445
|
|
|
487
|
-
export function joinReadable(
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
)
|
|
492
|
-
const lastIx = texts.length - 1;
|
|
493
|
-
return texts.filter(Boolean).reduce<string>((acc, text, i) => {
|
|
494
|
-
return acc + (!acc ? "" : i === lastIx ? lastSeparator : separator) + text;
|
|
495
|
-
}, "");
|
|
446
|
+
export function joinReadable(texts: (string | null | undefined)[], separator: string = ', ', lastSeparator: string = ' and '): string {
|
|
447
|
+
const lastIx = texts.length - 1;
|
|
448
|
+
return texts.filter(Boolean).reduce<string>((acc, text, i) => {
|
|
449
|
+
return acc + (!acc ? '' : i === lastIx ? lastSeparator : separator) + text;
|
|
450
|
+
}, '');
|
|
496
451
|
}
|
|
497
452
|
|
|
498
453
|
/**
|
|
@@ -504,23 +459,19 @@ export function joinReadable(
|
|
|
504
459
|
* @returns Formatted text with optional brackets
|
|
505
460
|
*/
|
|
506
461
|
|
|
507
|
-
export function textWithBrackets(
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
} else if (!text) {
|
|
521
|
-
return bText;
|
|
522
|
-
}
|
|
523
|
-
return `${text} ${brackets[0].trimStart()}${bText}${brackets[1].trimEnd()}`;
|
|
462
|
+
export function textWithBrackets(text: string, bracketsText?: string | string[], brackets: [string, string] = ['(', ')']) {
|
|
463
|
+
let bText;
|
|
464
|
+
if (Array.isArray(bracketsText)) {
|
|
465
|
+
bText = bracketsText.filter(Boolean).join(' ').trim();
|
|
466
|
+
} else {
|
|
467
|
+
bText = bracketsText?.trim();
|
|
468
|
+
}
|
|
469
|
+
if (!bText) {
|
|
470
|
+
return text;
|
|
471
|
+
} else if (!text) {
|
|
472
|
+
return bText;
|
|
473
|
+
}
|
|
474
|
+
return `${text} ${brackets[0].trimStart()}${bText}${brackets[1].trimEnd()}`;
|
|
524
475
|
}
|
|
525
476
|
|
|
526
477
|
/**
|
|
@@ -532,11 +483,11 @@ export function textWithBrackets(
|
|
|
532
483
|
*/
|
|
533
484
|
|
|
534
485
|
export function removeGeneratedTextGarbage(str: string) {
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
486
|
+
if (!str || typeof str !== 'string') {
|
|
487
|
+
return '';
|
|
488
|
+
}
|
|
489
|
+
// Remove all line breaks and spaces
|
|
490
|
+
return str.replace(/[\n\r\s]+/g, ' ').trim();
|
|
540
491
|
}
|
|
541
492
|
|
|
542
493
|
/**
|
|
@@ -546,10 +497,10 @@ export function removeGeneratedTextGarbage(str: string) {
|
|
|
546
497
|
*/
|
|
547
498
|
|
|
548
499
|
export function escapeCSVValue(value: string): string {
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
500
|
+
if (/[",\r\n]/.test(value)) {
|
|
501
|
+
return `"${value.replace(/"/g, '""')}"`;
|
|
502
|
+
}
|
|
503
|
+
return value;
|
|
553
504
|
}
|
|
554
505
|
|
|
555
506
|
/**
|
|
@@ -557,14 +508,14 @@ export function escapeCSVValue(value: string): string {
|
|
|
557
508
|
*/
|
|
558
509
|
|
|
559
510
|
export function truncateFileName(fileName: string, maxLen = 34) {
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
511
|
+
if (fileName.length <= maxLen) {
|
|
512
|
+
return fileName;
|
|
513
|
+
}
|
|
563
514
|
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
515
|
+
const extIndex = fileName.lastIndexOf('.');
|
|
516
|
+
const ext = extIndex > 0 ? fileName.slice(extIndex) : '';
|
|
517
|
+
const headLen = Math.max(1, maxLen - ext.length - 3);
|
|
518
|
+
return fileName.slice(0, headLen) + '...' + ext;
|
|
568
519
|
}
|
|
569
520
|
|
|
570
521
|
/**
|
|
@@ -573,23 +524,16 @@ export function truncateFileName(fileName: string, maxLen = 34) {
|
|
|
573
524
|
*/
|
|
574
525
|
|
|
575
526
|
export function formatPhoneNumber(phone?: string | null) {
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
// NOTE: This does not account for non North American (+1) phone numbers
|
|
587
|
-
return `${normalizedCountryCode}${
|
|
588
|
-
normalizedCountryCode ? " " : ""
|
|
589
|
-
}(${area}) ${first}-${second}`;
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
return phone;
|
|
527
|
+
const match = phone?.match(/(\d{3})(\d{3})(\d{4})$/);
|
|
528
|
+
if (match) {
|
|
529
|
+
const [, area, first, second] = match;
|
|
530
|
+
const countryCode = phone!.substring(0, phone!.length - area.length - first.length - second.length).trim();
|
|
531
|
+
const normalizedCountryCode = countryCode ? (countryCode.startsWith('+') ? countryCode : `+${countryCode}`) : '';
|
|
532
|
+
// NOTE: This does not account for non North American (+1) phone numbers
|
|
533
|
+
return `${normalizedCountryCode}${normalizedCountryCode ? ' ' : ''}(${area}) ${first}-${second}`;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
return phone;
|
|
593
537
|
}
|
|
594
538
|
|
|
595
539
|
/**
|
|
@@ -600,50 +544,43 @@ export function formatPhoneNumber(phone?: string | null) {
|
|
|
600
544
|
* @returns An array of text chunks.
|
|
601
545
|
*/
|
|
602
546
|
|
|
603
|
-
export function splitTextIntoChunks(
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
const chunk = text.slice(start, end).trim();
|
|
642
|
-
if (chunk) chunks.push(chunk);
|
|
643
|
-
start = end;
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
return chunks;
|
|
547
|
+
export function splitTextIntoChunks(text: string, chunkSize: number, linebreakDistance: number = 50): string[] {
|
|
548
|
+
if (!text || chunkSize <= 0 || text.length < chunkSize) {
|
|
549
|
+
return [text || ''];
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
const chunks: string[] = [];
|
|
553
|
+
let start = 0;
|
|
554
|
+
|
|
555
|
+
while (start < text.length) {
|
|
556
|
+
let end = Math.min(start + chunkSize, text.length);
|
|
557
|
+
|
|
558
|
+
if (end < text.length) {
|
|
559
|
+
// Find the nearest line break around the target chunk boundary
|
|
560
|
+
const beforeBreak = text.lastIndexOf('\n', end);
|
|
561
|
+
|
|
562
|
+
let chosenBreak = -1;
|
|
563
|
+
if (beforeBreak !== -1 && end - beforeBreak <= linebreakDistance && beforeBreak > start) {
|
|
564
|
+
chosenBreak = beforeBreak + 1;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if (chosenBreak !== -1) {
|
|
568
|
+
end = chosenBreak;
|
|
569
|
+
} else {
|
|
570
|
+
// Fall back to last whitespace near the end
|
|
571
|
+
const lastSpace = text.lastIndexOf(' ', end);
|
|
572
|
+
if (lastSpace > start && end - lastSpace < 100) {
|
|
573
|
+
end = lastSpace;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
const chunk = text.slice(start, end).trim();
|
|
579
|
+
if (chunk) chunks.push(chunk);
|
|
580
|
+
start = end;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
return chunks;
|
|
647
584
|
}
|
|
648
585
|
|
|
649
586
|
/**
|
|
@@ -651,10 +588,10 @@ export function splitTextIntoChunks(
|
|
|
651
588
|
*/
|
|
652
589
|
|
|
653
590
|
export function formatReferenceNumber(idStr: string) {
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
591
|
+
if (idStr && /^\d+$/.test(idStr)) {
|
|
592
|
+
return '#' + idStr;
|
|
593
|
+
}
|
|
594
|
+
return idStr;
|
|
658
595
|
}
|
|
659
596
|
|
|
660
597
|
/**
|
|
@@ -664,18 +601,18 @@ export function formatReferenceNumber(idStr: string) {
|
|
|
664
601
|
*/
|
|
665
602
|
|
|
666
603
|
export function getNthString(num: number): string {
|
|
667
|
-
|
|
668
|
-
|
|
604
|
+
const mod10 = num % 10;
|
|
605
|
+
const mod100 = num % 100;
|
|
669
606
|
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
607
|
+
if (mod10 === 1 && mod100 !== 11) {
|
|
608
|
+
return `${num}st`;
|
|
609
|
+
} else if (mod10 === 2 && mod100 !== 12) {
|
|
610
|
+
return `${num}nd`;
|
|
611
|
+
} else if (mod10 === 3 && mod100 !== 13) {
|
|
612
|
+
return `${num}rd`;
|
|
613
|
+
} else {
|
|
614
|
+
return `${num}th`;
|
|
615
|
+
}
|
|
679
616
|
}
|
|
680
617
|
|
|
681
618
|
/**
|
|
@@ -687,47 +624,37 @@ export function getNthString(num: number): string {
|
|
|
687
624
|
*/
|
|
688
625
|
|
|
689
626
|
export function makeAddressText(
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
627
|
+
address: Partial<AddressObj>,
|
|
628
|
+
format: 'REGION_ONLY' | 'SINGLE_LINE' | 'NORMAL' = 'NORMAL',
|
|
629
|
+
useFullCountryName: boolean = false,
|
|
630
|
+
delimiter: string = ', ',
|
|
694
631
|
) {
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
// NORMAL format
|
|
722
|
-
return [
|
|
723
|
-
address.line1,
|
|
724
|
-
address.line2,
|
|
725
|
-
[
|
|
726
|
-
[address.city, address.state].filter(Boolean).join(", "),
|
|
727
|
-
address.postalCode,
|
|
728
|
-
].filter(Boolean).join(" "),
|
|
729
|
-
countryName,
|
|
730
|
-
].filter(Boolean).join("\n");
|
|
632
|
+
if (!address) {
|
|
633
|
+
return null;
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
const countryName =
|
|
637
|
+
useFullCountryName && i18n.has(`country.from_code.${address.country}`)
|
|
638
|
+
? i18n.t(`country.from_code.${address.country}`)
|
|
639
|
+
: address.country;
|
|
640
|
+
|
|
641
|
+
if (format === 'REGION_ONLY') {
|
|
642
|
+
return [address.city, address.state, countryName].filter(Boolean).join(delimiter);
|
|
643
|
+
} else if (format === 'SINGLE_LINE') {
|
|
644
|
+
return [address.line1, address.line2, address.city, address.state, address.postalCode, countryName]
|
|
645
|
+
.filter(Boolean)
|
|
646
|
+
.join(delimiter);
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
// NORMAL format
|
|
650
|
+
return [
|
|
651
|
+
address.line1,
|
|
652
|
+
address.line2,
|
|
653
|
+
[[address.city, address.state].filter(Boolean).join(', '), address.postalCode].filter(Boolean).join(' '),
|
|
654
|
+
countryName,
|
|
655
|
+
]
|
|
656
|
+
.filter(Boolean)
|
|
657
|
+
.join('\n');
|
|
731
658
|
}
|
|
732
659
|
|
|
733
660
|
/**
|
|
@@ -736,11 +663,47 @@ export function makeAddressText(
|
|
|
736
663
|
*/
|
|
737
664
|
|
|
738
665
|
export function getDisplayName(firstName?: string | null, lastName?: string | null) {
|
|
739
|
-
|
|
740
|
-
|
|
666
|
+
const fullName = buildSingleText([firstName, lastName], ' ');
|
|
667
|
+
const displayName = guessFirstName(fullName, 8) || '?';
|
|
668
|
+
|
|
669
|
+
return {
|
|
670
|
+
fullName,
|
|
671
|
+
displayName,
|
|
672
|
+
};
|
|
673
|
+
}
|
|
741
674
|
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
675
|
+
/**
|
|
676
|
+
* Hash a string with FNV-1a (32-bit) into a short stable hex token.
|
|
677
|
+
* @returns 8-character hexadecimal hash string
|
|
678
|
+
*/
|
|
679
|
+
|
|
680
|
+
export function hashStringFNV1a(value: string) {
|
|
681
|
+
let hash = 0x811c9dc5;
|
|
682
|
+
|
|
683
|
+
for (let i = 0; i < value.length; i++) {
|
|
684
|
+
hash ^= value.charCodeAt(i);
|
|
685
|
+
hash = Math.imul(hash, 0x01000193);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
return (hash >>> 0).toString(16).padStart(8, '0');
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/**
|
|
692
|
+
* Remove markdown from text, so you can send it to SMS
|
|
693
|
+
* @param text - Text to remove markdown from
|
|
694
|
+
* @returns - Text without markdown
|
|
695
|
+
*/
|
|
696
|
+
|
|
697
|
+
export function removeMarkdownForSMS(text: string) {
|
|
698
|
+
return (
|
|
699
|
+
text
|
|
700
|
+
// 4. Drop leading `#`‑style headings (handles up to ######).
|
|
701
|
+
.replace(/^[ \t]*#{1,6}[ \t]+/gm, '')
|
|
702
|
+
// 1. Bold: **text** (no line‑breaks inside)
|
|
703
|
+
.replace(/\*\*([^\n*]+?)\*\*/g, '$1')
|
|
704
|
+
// 2. Italic: *text* (single asterisks only, not part of **)
|
|
705
|
+
.replace(/(?<!\*)\*([^\n*]+?)\*(?!\*)/g, '$1')
|
|
706
|
+
// 3. Italic: _text_ (underscores)
|
|
707
|
+
.replace(/_([^\n_]+?)_/g, '$1')
|
|
708
|
+
);
|
|
746
709
|
}
|