@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,709 @@
|
|
|
1
|
+
import i18n from '../i18n/index.ts';
|
|
2
|
+
import type { AddressObj } from '../types/other.d.ts';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Combine list of class names
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export function cn(...classNames: (string | boolean | undefined | null)[]) {
|
|
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)};`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Shorten text to space with length limit
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export function shortenToSpace(str: string, len: number = 100, ellipsis: string = '...'): string {
|
|
25
|
+
if (!str || str.length < len) {
|
|
26
|
+
return str;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const trimmed = str.substring(0, len);
|
|
30
|
+
const spaceIndex = trimmed.lastIndexOf(' ');
|
|
31
|
+
|
|
32
|
+
if (spaceIndex < 0) {
|
|
33
|
+
return trimmed.substring(0, len) + ellipsis;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return trimmed.substring(0, Math.min(trimmed.length, spaceIndex)) + ellipsis;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Shorten word to space, and remove article words
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
export function guessFirstName(str: string, len: number = 40): string {
|
|
44
|
+
let shortened = str?.replace(/^(a|an|the|da|de|that|there|this|than|then)\b/i, '')?.trim() || '';
|
|
45
|
+
const spaceIx = shortened.indexOf(' ');
|
|
46
|
+
|
|
47
|
+
if (spaceIx >= 0) {
|
|
48
|
+
shortened = shortened.substring(0, spaceIx + 1);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (len < shortened.length) {
|
|
52
|
+
shortened = shortened.substring(0, len);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const articleReplacedEnd = shortened.replace(/\b(a|an|the|da|de|that|there|this|than|then)$/i, '');
|
|
56
|
+
|
|
57
|
+
if (articleReplacedEnd.length < 5) {
|
|
58
|
+
return shortened.trim();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return articleReplacedEnd.trim();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Get indefinite article ("a" or "an") for a word
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
export function addIndefiniteArticle(word: string) {
|
|
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}`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Check if email
|
|
89
|
+
*/
|
|
90
|
+
|
|
91
|
+
export function isEmail(str: string) {
|
|
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());
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Convert various phone formats to international format
|
|
100
|
+
* @returns phone number in international format
|
|
101
|
+
*/
|
|
102
|
+
|
|
103
|
+
export function convertToInternationalPhone(str: string) {
|
|
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;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Check if phone number
|
|
124
|
+
* @returns TRUE if starts with + and has 7-15 digits
|
|
125
|
+
* NOTE: This will convert phone-like strings to international format
|
|
126
|
+
*/
|
|
127
|
+
|
|
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);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Check if password is secure enough
|
|
141
|
+
* Rules:
|
|
142
|
+
* at least 8 characters long
|
|
143
|
+
* at least one lowercase letter
|
|
144
|
+
* at least one uppercase letter
|
|
145
|
+
* at least one special character
|
|
146
|
+
*/
|
|
147
|
+
|
|
148
|
+
export function passwordStrength(str: string, minLength: number = 8) {
|
|
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
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Get time base unique string
|
|
168
|
+
*/
|
|
169
|
+
|
|
170
|
+
export function getTimeBasedUnique() {
|
|
171
|
+
return Date.now().toString(36) + Math.random().toString(36).substring(2);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Convert string to alphanumeric (or space) only
|
|
176
|
+
*/
|
|
177
|
+
|
|
178
|
+
export function getAlphanumeric(str: string, space: boolean = false) {
|
|
179
|
+
return str.replace(space ? /[^a-z0-9\s]/gi : /[^a-z0-9]/gi, '');
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Build normalized unique list of names from raw search inputs
|
|
184
|
+
*/
|
|
185
|
+
|
|
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);
|
|
191
|
+
|
|
192
|
+
const uniqueNames: string[] = [];
|
|
193
|
+
const seen = new Set<string>();
|
|
194
|
+
|
|
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
|
+
}
|
|
202
|
+
|
|
203
|
+
return uniqueNames;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Get regex patterns from a text
|
|
208
|
+
*/
|
|
209
|
+
|
|
210
|
+
export function regexFromText(text: string) {
|
|
211
|
+
const words = getAlphanumeric(text, true)
|
|
212
|
+
.split(/\s+/)
|
|
213
|
+
.filter((s) => s.length >= 2);
|
|
214
|
+
if (!words.length) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return new RegExp('\\b' + (words.length === 1 ? words[0] : '(?:' + words.join('|') + ')'), 'gi');
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Upper case first letter
|
|
223
|
+
*/
|
|
224
|
+
|
|
225
|
+
export function ucFirst(str: string) {
|
|
226
|
+
if (!str) {
|
|
227
|
+
return '';
|
|
228
|
+
}
|
|
229
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Convert cameCalsed string to dash-separated
|
|
234
|
+
*/
|
|
235
|
+
|
|
236
|
+
export function camelCaseToDash(str: string) {
|
|
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
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Shorten text to a paragraph length
|
|
247
|
+
*/
|
|
248
|
+
|
|
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);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Use this to validate if a string is empty or invisible
|
|
284
|
+
*/
|
|
285
|
+
|
|
286
|
+
export function isInvisibleString(str: string) {
|
|
287
|
+
if (!str?.trim()) {
|
|
288
|
+
return true;
|
|
289
|
+
}
|
|
290
|
+
|
|
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();
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Build a single text block with proper line breaks
|
|
299
|
+
* NOTE: Userful for building AI prompts
|
|
300
|
+
*/
|
|
301
|
+
|
|
302
|
+
type AnyLineItem = string | undefined | false | null;
|
|
303
|
+
type BuildSingleTextConditionalLine = [string[], BuildSingleTextLine | BuildSingleTextLine[]];
|
|
304
|
+
type BuildSingleTextConditionalLineGroup = BuildSingleTextConditionalLine[];
|
|
305
|
+
interface BuildSingleTextFeatureMap {
|
|
306
|
+
[key: string]: BuildSingleTextLine | BuildSingleTextLine[];
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export type BuildSingleTextLine =
|
|
310
|
+
| AnyLineItem
|
|
311
|
+
| BuildSingleTextFeatureMap
|
|
312
|
+
| BuildSingleTextConditionalLine
|
|
313
|
+
| BuildSingleTextConditionalLineGroup
|
|
314
|
+
| BuildSingleTextLine[];
|
|
315
|
+
|
|
316
|
+
/*
|
|
317
|
+
* Check whether a buildSingleText line uses the all-features conditional tuple syntax.
|
|
318
|
+
*/
|
|
319
|
+
|
|
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');
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/*
|
|
325
|
+
* Check whether a buildSingleText line uses the first-match conditional group syntax.
|
|
326
|
+
*/
|
|
327
|
+
|
|
328
|
+
function isBuildSingleTextConditionalLineGroup(
|
|
329
|
+
line: BuildSingleTextLine | BuildSingleTextLine[],
|
|
330
|
+
): line is BuildSingleTextConditionalLineGroup {
|
|
331
|
+
return Array.isArray(line) && line.length > 0 && line.every((lineItem) => isBuildSingleTextConditionalLine(lineItem));
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/*
|
|
335
|
+
* Check whether a buildSingleText requirement matches the enabled features.
|
|
336
|
+
*/
|
|
337
|
+
|
|
338
|
+
function doesBuildSingleTextRequirementMatch(requirement: string, features: string[]) {
|
|
339
|
+
const isNegatedRequirement = requirement.startsWith('!');
|
|
340
|
+
const feature = isNegatedRequirement ? requirement.slice(1) : requirement;
|
|
341
|
+
|
|
342
|
+
if (!feature) {
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const hasFeature = features.includes(feature);
|
|
347
|
+
return isNegatedRequirement ? !hasFeature : hasFeature;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/*
|
|
351
|
+
* Check whether a buildSingleText conditional line matches the enabled features.
|
|
352
|
+
*/
|
|
353
|
+
|
|
354
|
+
function doesBuildSingleTextConditionalLineMatch(conditionalLine: BuildSingleTextConditionalLine, features: string[]) {
|
|
355
|
+
return conditionalLine[0].every((feature) => doesBuildSingleTextRequirementMatch(feature, features));
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/*
|
|
359
|
+
* Build text for a conditional line payload.
|
|
360
|
+
*/
|
|
361
|
+
|
|
362
|
+
function getBuildSingleTextConditionalText(
|
|
363
|
+
conditionalLines: BuildSingleTextLine | BuildSingleTextLine[],
|
|
364
|
+
delemiter: string,
|
|
365
|
+
features: string[],
|
|
366
|
+
) {
|
|
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, '');
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Join array of texts but last word has a special separator
|
|
439
|
+
*
|
|
440
|
+
* @param texts - Array of strings to join
|
|
441
|
+
* @param separator - Separator to use between texts
|
|
442
|
+
* @param lastSeparator - Separator to use before the last text
|
|
443
|
+
* @returns Joined string with special last separator
|
|
444
|
+
*/
|
|
445
|
+
|
|
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
|
+
}, '');
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* Helper for writing text with optional brackets
|
|
455
|
+
*
|
|
456
|
+
* @param text - Text to write
|
|
457
|
+
* @param bracketsText - Text that will be written in brackets
|
|
458
|
+
* @param brackets - Brackets to use, defaults to round brackets
|
|
459
|
+
* @returns Formatted text with optional brackets
|
|
460
|
+
*/
|
|
461
|
+
|
|
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()}`;
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Remove all line breaks and spaces from String
|
|
479
|
+
* NOTE: This is designed to remove garbage string from OpenAI text generation
|
|
480
|
+
*
|
|
481
|
+
* @param str - String to clean
|
|
482
|
+
* @returns Cleaned string without line breaks and spaces
|
|
483
|
+
*/
|
|
484
|
+
|
|
485
|
+
export function removeGeneratedTextGarbage(str: string) {
|
|
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();
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Escape values not allowed in CSV column
|
|
495
|
+
* @param value String value to escape
|
|
496
|
+
* @returns Escaped string value
|
|
497
|
+
*/
|
|
498
|
+
|
|
499
|
+
export function escapeCSVValue(value: string): string {
|
|
500
|
+
if (/[",\r\n]/.test(value)) {
|
|
501
|
+
return `"${value.replace(/"/g, '""')}"`;
|
|
502
|
+
}
|
|
503
|
+
return value;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Truncate a file name while preserving extension when possible.
|
|
508
|
+
*/
|
|
509
|
+
|
|
510
|
+
export function truncateFileName(fileName: string, maxLen = 34) {
|
|
511
|
+
if (fileName.length <= maxLen) {
|
|
512
|
+
return fileName;
|
|
513
|
+
}
|
|
514
|
+
|
|
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;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Convert Intl phone format to a readable format.
|
|
523
|
+
* Example: "+1 (555) 123-4567"
|
|
524
|
+
*/
|
|
525
|
+
|
|
526
|
+
export function formatPhoneNumber(phone?: string | null) {
|
|
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;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/**
|
|
540
|
+
* Split a long text into chunks, making sure each chunk is under the chunk size character limit.
|
|
541
|
+
* @param text - The long text to split.
|
|
542
|
+
* @param chunkSize - The maximum number of characters allowed in each chunk.
|
|
543
|
+
* @param linebreakDistance - The maximum distance from the chunk boundary to look for a line break.
|
|
544
|
+
* @returns An array of text chunks.
|
|
545
|
+
*/
|
|
546
|
+
|
|
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;
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Format reference number
|
|
588
|
+
*/
|
|
589
|
+
|
|
590
|
+
export function formatReferenceNumber(idStr: string) {
|
|
591
|
+
if (idStr && /^\d+$/.test(idStr)) {
|
|
592
|
+
return '#' + idStr;
|
|
593
|
+
}
|
|
594
|
+
return idStr;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* Get "nth" string for a number
|
|
599
|
+
* @param num - The number to convert
|
|
600
|
+
* @returns The number with its ordinal suffix (e.g., "1st", "2nd", "3rd", "4th")
|
|
601
|
+
*/
|
|
602
|
+
|
|
603
|
+
export function getNthString(num: number): string {
|
|
604
|
+
const mod10 = num % 10;
|
|
605
|
+
const mod100 = num % 100;
|
|
606
|
+
|
|
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
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/**
|
|
619
|
+
* Make address text
|
|
620
|
+
* @param address - Address object
|
|
621
|
+
* @param useFullCountryName - Whether to use full country name instead of code
|
|
622
|
+
* @param regionOnly - Whether to return only region (city, state, country)
|
|
623
|
+
* @returns Formatted address string
|
|
624
|
+
*/
|
|
625
|
+
|
|
626
|
+
export function makeAddressText(
|
|
627
|
+
address: Partial<AddressObj>,
|
|
628
|
+
format: 'REGION_ONLY' | 'SINGLE_LINE' | 'NORMAL' = 'NORMAL',
|
|
629
|
+
useFullCountryName: boolean = false,
|
|
630
|
+
delimiter: string = ', ',
|
|
631
|
+
) {
|
|
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');
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* Get display name from first and last name
|
|
662
|
+
* @returns Formatted display name
|
|
663
|
+
*/
|
|
664
|
+
|
|
665
|
+
export function getDisplayName(firstName?: string | null, lastName?: string | null) {
|
|
666
|
+
const fullName = buildSingleText([firstName, lastName], ' ');
|
|
667
|
+
const displayName = guessFirstName(fullName, 8) || '?';
|
|
668
|
+
|
|
669
|
+
return {
|
|
670
|
+
fullName,
|
|
671
|
+
displayName,
|
|
672
|
+
};
|
|
673
|
+
}
|
|
674
|
+
|
|
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
|
+
);
|
|
709
|
+
}
|