@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.
Files changed (79) hide show
  1. package/package.json +57 -45
  2. package/src/constants/agent.ts +51 -41
  3. package/src/constants/app.ts +34 -13
  4. package/src/constants/emoji.ts +9144 -8936
  5. package/src/constants/other.ts +1 -5
  6. package/src/constants/user.ts +16 -25
  7. package/src/constants/wsActions.ts +27 -0
  8. package/src/i18n/account.ts +129 -107
  9. package/src/i18n/agent.ts +331 -73
  10. package/src/i18n/agentToolBusiness.ts +196 -0
  11. package/src/i18n/agentToolGrid.ts +137 -0
  12. package/src/i18n/agentToolWorkflow.ts +101 -0
  13. package/src/i18n/agent_note.ts +109 -0
  14. package/src/i18n/app.ts +107 -91
  15. package/src/i18n/auth.ts +117 -102
  16. package/src/i18n/billing.ts +5 -92
  17. package/src/i18n/c.ts +907 -0
  18. package/src/i18n/campaign.ts +919 -0
  19. package/src/i18n/country.ts +248 -248
  20. package/src/i18n/datetime.ts +32 -26
  21. package/src/i18n/editor.ts +58 -0
  22. package/src/i18n/error.ts +577 -192
  23. package/src/i18n/field_doc_editor.ts +21 -0
  24. package/src/i18n/field_doc_programs.ts +52 -0
  25. package/src/i18n/field_docs.ts +190 -0
  26. package/src/i18n/index.ts +42 -19
  27. package/src/i18n/log.ts +272 -238
  28. package/src/i18n/memory.ts +53 -0
  29. package/src/i18n/message.ts +3 -4
  30. package/src/i18n/note.ts +19 -0
  31. package/src/i18n/org.ts +164 -109
  32. package/src/i18n/schedule.ts +209 -0
  33. package/src/i18n/sheet.ts +333 -4
  34. package/src/i18n/sheet_formula.ts +165 -0
  35. package/src/i18n/workflow.ts +109 -23
  36. package/src/main.ts +68 -64
  37. package/src/platform.ts +90 -49
  38. package/src/types/ai.d.ts +52 -23
  39. package/src/types/app.d.ts +95 -44
  40. package/src/types/auth.d.ts +66 -40
  41. package/src/types/console.d.ts +3 -0
  42. package/src/types/other.d.ts +13 -6
  43. package/src/types.d.ts +12 -14
  44. package/src/utils/account.ts +64 -58
  45. package/src/utils/api.ts +326 -77
  46. package/src/utils/authSession.ts +24 -0
  47. package/src/utils/clientIP.ts +213 -0
  48. package/src/utils/color.ts +279 -0
  49. package/src/utils/cron.ts +49 -18
  50. package/src/utils/csv.ts +19 -0
  51. package/src/utils/datetime.ts +445 -117
  52. package/src/utils/deferredSave.ts +89 -0
  53. package/src/utils/email.ts +50 -0
  54. package/src/utils/emoji.ts +107 -84
  55. package/src/utils/expression.helpers.ts +470 -0
  56. package/src/utils/expression.test.ts +793 -0
  57. package/src/utils/expression.ts +2261 -0
  58. package/src/utils/interval.ts +80 -0
  59. package/src/utils/language.ts +9 -0
  60. package/src/utils/logic.ts +141 -121
  61. package/src/utils/managedSession.ts +34 -0
  62. package/src/utils/managedSessionBff.test.ts +60 -0
  63. package/src/utils/managedSessionBff.ts +117 -0
  64. package/src/utils/number.ts +85 -38
  65. package/src/utils/object.ts +227 -30
  66. package/src/utils/promise.ts +98 -0
  67. package/src/utils/queryReset.ts +266 -0
  68. package/src/utils/recurringTask.ts +60 -0
  69. package/src/utils/schema.ts +293 -0
  70. package/src/utils/string.test.ts +205 -208
  71. package/src/utils/string.ts +426 -463
  72. package/src/utils/timeZone.ts +309 -851
  73. package/src/utils/url_client.ts +145 -72
  74. package/tsconfig.json +10 -10
  75. package/src/constants/billing.ts +0 -146
  76. package/src/i18n/form.ts +0 -376
  77. package/src/i18n/product.ts +0 -76
  78. package/src/types/form.d.ts +0 -97
  79. package/src/utils/billing.ts +0 -134
@@ -1,38 +1,39 @@
1
- import i18n from "../i18n/index.ts";
2
- import type { AddressObj } from "../types/other.d.ts";
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
- return classNames.filter(Boolean).join(" ");
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
- str: string,
18
- len: number = 100,
19
- ellipsis: string = "...",
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
- const trimmed = str.substring(0, len);
26
- const spaceIndex = trimmed.lastIndexOf(" ");
29
+ const trimmed = str.substring(0, len);
30
+ const spaceIndex = trimmed.lastIndexOf(' ');
27
31
 
28
- if (spaceIndex < 0) {
29
- return trimmed.substring(0, len) + ellipsis;
30
- }
32
+ if (spaceIndex < 0) {
33
+ return trimmed.substring(0, len) + ellipsis;
34
+ }
31
35
 
32
- return (
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
- let shortened =
44
- str?.replace(/^(a|an|the|da|de|that|there|this|than|then)\b/i, "")
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
- if (spaceIx >= 0) {
49
- shortened = shortened.substring(0, spaceIx + 1);
50
- }
47
+ if (spaceIx >= 0) {
48
+ shortened = shortened.substring(0, spaceIx + 1);
49
+ }
51
50
 
52
- if (len < shortened.length) {
53
- shortened = shortened.substring(0, len);
54
- }
51
+ if (len < shortened.length) {
52
+ shortened = shortened.substring(0, len);
53
+ }
55
54
 
56
- const articleReplacedEnd = shortened.replace(
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
- if (articleReplacedEnd.length < 5) {
62
- return shortened.trim();
63
- }
57
+ if (articleReplacedEnd.length < 5) {
58
+ return shortened.trim();
59
+ }
64
60
 
65
- return articleReplacedEnd.trim();
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
- const raw = (word || "").trim().toLowerCase();
74
-
75
- let article;
76
- if (!raw) {
77
- article = "a";
78
- } else if (/^(honest|honor|hour|heir)\b/.test(raw)) {
79
- // Common silent-h words use "an"
80
- article = "an";
81
- } else if (
82
- /^(uni([^nmd]|$)|use|user|euro|one|once|ubiquit|ufo|ukulele)\b/.test(raw)
83
- ) {
84
- // Common "you"/"w" sound starts use "a"
85
- article = "a";
86
- } else {
87
- article = /^[aeiou]/.test(raw) ? "an" : "a";
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
- // return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str);
99
- const emailRegex =
100
- /[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;
101
- return !!str && emailRegex.test(str.trim());
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
- // Check if string has a character that isn't one of [(, ), +, -, " ", 0-9]
111
- if (/[^0-9+\-\s().]/.test(str)) {
112
- // Invalid
113
- return null;
114
- }
115
-
116
- const digits = str.replace(/[^0-9]/g, "");
117
- if (!digits || digits.startsWith("0")) {
118
- // Invalid
119
- return null;
120
- } else if (
121
- !digits.startsWith("1") &&
122
- digits.length === 10
123
- ) {
124
- return "+1" + digits;
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
- str: string,
139
- doNotConvertInternational: boolean = false,
140
- ) {
141
- let phoneNumber: string | null = str.trim();
142
- if (!doNotConvertInternational) {
143
- phoneNumber = convertToInternationalPhone(phoneNumber);
144
- if (!phoneNumber) {
145
- return false;
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
- const isLongEnough = str.length >= minLength;
162
- const hasLowercase = /[a-z]/.test(str);
163
- const hasUppercase = /[A-Z]/.test(str);
164
- const hasDigit = /\d/.test(str);
165
- const hasSpecial = /\W/.test(str);
166
- const valid = isLongEnough && hasLowercase && hasUppercase && hasDigit &&
167
- hasSpecial;
168
- // return /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*\W)[a-zA-Z\d\W]{8,}$/.test(str);
169
- return {
170
- valid,
171
- isLongEnough,
172
- hasLowercase,
173
- hasUppercase,
174
- hasDigit,
175
- hasSpecial,
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
- return Date.now().toString(36) + Math.random().toString(36).substring(2);
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
- return str.replace(space ? /[^a-z0-9\s]/gi : /[^a-z0-9]/gi, "");
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
- namesList: (string | null | undefined)[] = [],
201
- ): string[] {
202
- const parsedNames = (namesList || [])
203
- .flatMap((s) => (s || "").split(/\s*(?:,|;|\n|\band\b)\s*/i))
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
- const uniqueNames: string[] = [];
208
- const seen = new Set<string>();
192
+ const uniqueNames: string[] = [];
193
+ const seen = new Set<string>();
209
194
 
210
- for (const name of parsedNames) {
211
- const key = name.toLowerCase();
212
- if (!seen.has(key)) {
213
- seen.add(key);
214
- uniqueNames.push(name);
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
- return uniqueNames;
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
- const words = getAlphanumeric(text, true).split(/\s+/).filter((s) =>
227
- s.length >= 2
228
- );
229
- if (!words.length) {
230
- return null;
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
- return new RegExp(
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
- if (!str) {
245
- return "";
246
- }
247
- return str.charAt(0).toUpperCase() + str.slice(1);
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
- if (/^[A-Z0-9]+$/.test(str)) {
256
- return str.toLowerCase();
257
- }
258
- return str.replace(/[A-Z0-9]/g, (m, i) => {
259
- return `${i ? "-" : ""}${m.toLowerCase()}`;
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
- str: string,
269
- len: number,
270
- replaceDoubleLineBreaks?: boolean,
271
- ) {
272
- let trimedToParagraph = str || "";
273
- if (replaceDoubleLineBreaks) {
274
- trimedToParagraph = trimedToParagraph.replace(/\n\s*\n/g, "\n");
275
- }
276
-
277
- trimedToParagraph = trimedToParagraph.split("\n").reduce((acc, str) => {
278
- if (acc.done) {
279
- return acc;
280
- }
281
- acc.done = (acc.value.length + str.length) >= len;
282
- acc.value = acc.done ? acc.value : acc.value + "\n" + str;
283
- return acc;
284
- }, {
285
- done: false,
286
- value: "",
287
- }).value.trim();
288
-
289
- if (!trimedToParagraph) {
290
- return shortenToSpace(str || "", len);
291
- } else if (trimedToParagraph.length <= len) {
292
- return trimedToParagraph;
293
- }
294
-
295
- return shortenToSpace(trimedToParagraph, len);
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
- if (!str?.trim()) {
304
- return true;
305
- }
287
+ if (!str?.trim()) {
288
+ return true;
289
+ }
306
290
 
307
- // eslint-disable-next-line no-misleading-character-class
308
- const invisRegex = /[ㅤ ️⠀ْٔ]/g;
309
- const checkStr = str.replace(invisRegex, " "); // .replace(/[ㅤ ️⠀]/gi, ' ');
310
- return !checkStr.trim();
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
- [key: string]: BuildSingleTextLine | BuildSingleTextLine[];
306
+ [key: string]: BuildSingleTextLine | BuildSingleTextLine[];
326
307
  }
327
308
 
328
309
  export type BuildSingleTextLine =
329
- | AnyLineItem
330
- | BuildSingleTextFeatureMap
331
- | BuildSingleTextConditionalLine
332
- | BuildSingleTextConditionalLineGroup;
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
- line: BuildSingleTextLine | BuildSingleTextLine[],
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
- line: BuildSingleTextLine | BuildSingleTextLine[],
329
+ line: BuildSingleTextLine | BuildSingleTextLine[],
353
330
  ): line is BuildSingleTextConditionalLineGroup {
354
- return Array.isArray(line) &&
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
- requirement: string,
365
- features: string[],
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
- if (!feature) {
371
- return false;
372
- }
342
+ if (!feature) {
343
+ return false;
344
+ }
373
345
 
374
- const hasFeature = features.includes(feature);
375
- return isNegatedRequirement ? !hasFeature : hasFeature;
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
- conditionalLine: BuildSingleTextConditionalLine,
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
- conditionalLines: BuildSingleTextLine | BuildSingleTextLine[],
395
- delemiter: string,
396
- features: string[],
363
+ conditionalLines: BuildSingleTextLine | BuildSingleTextLine[],
364
+ delemiter: string,
365
+ features: string[],
397
366
  ) {
398
- return buildSingleText(
399
- Array.isArray(conditionalLines) &&
400
- !isBuildSingleTextConditionalLine(conditionalLines)
401
- ? conditionalLines
402
- : [conditionalLines],
403
- delemiter,
404
- features,
405
- );
406
- }
407
-
408
- export function buildSingleText(
409
- lines: BuildSingleTextLine[],
410
- delemiter: string = "\n",
411
- features: string[] = [],
412
- ) {
413
- return lines
414
- .reduce((acc, line_) => {
415
- const line = line_ || "";
416
- if (isBuildSingleTextConditionalLineGroup(line)) {
417
- const matchedConditionalLine = line.find((conditionalLine) =>
418
- doesBuildSingleTextConditionalLineMatch(conditionalLine, features)
419
- );
420
-
421
- if (!matchedConditionalLine) {
422
- return acc;
423
- }
424
-
425
- const conditionalText = getBuildSingleTextConditionalText(
426
- matchedConditionalLine[1],
427
- delemiter,
428
- features,
429
- );
430
-
431
- if (conditionalText || acc[acc.length - 1] !== "") {
432
- acc.push(conditionalText);
433
- }
434
-
435
- return acc;
436
- }
437
-
438
- if (isBuildSingleTextConditionalLine(line)) {
439
- const [, conditionalLines] = line;
440
-
441
- if (doesBuildSingleTextConditionalLineMatch(line, features)) {
442
- const conditionalText = getBuildSingleTextConditionalText(
443
- conditionalLines,
444
- delemiter,
445
- features,
446
- );
447
-
448
- if (conditionalText || acc[acc.length - 1] !== "") {
449
- acc.push(conditionalText);
450
- }
451
- }
452
-
453
- return acc;
454
- }
455
- if (line && typeof line === "object") {
456
- for (const key in line) {
457
- const lineItem = line[key];
458
- if (features.includes(key)) {
459
- const nextLines = Array.isArray(lineItem) &&
460
- !isBuildSingleTextConditionalLine(lineItem)
461
- ? lineItem
462
- : [lineItem];
463
- acc.push(buildSingleText(nextLines, delemiter, features));
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
- texts: (string | null | undefined)[],
489
- separator: string = ", ",
490
- lastSeparator: string = " and ",
491
- ): string {
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
- text: string,
509
- bracketsText?: string | string[],
510
- brackets: [string, string] = ["(", ")"],
511
- ) {
512
- let bText;
513
- if (Array.isArray(bracketsText)) {
514
- bText = bracketsText.filter(Boolean).join(" ").trim();
515
- } else {
516
- bText = bracketsText?.trim();
517
- }
518
- if (!bText) {
519
- return text;
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
- if (!str || typeof str !== "string") {
536
- return "";
537
- }
538
- // Remove all line breaks and spaces
539
- return str.replace(/[\n\r\s]+/g, " ").trim();
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
- if (/[",\n]/.test(value)) {
550
- return `"${value.replace(/"/g, '""')}"`;
551
- }
552
- return value;
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
- if (fileName.length <= maxLen) {
561
- return fileName;
562
- }
511
+ if (fileName.length <= maxLen) {
512
+ return fileName;
513
+ }
563
514
 
564
- const extIndex = fileName.lastIndexOf(".");
565
- const ext = extIndex > 0 ? fileName.slice(extIndex) : "";
566
- const headLen = Math.max(1, maxLen - ext.length - 3);
567
- return fileName.slice(0, headLen) + "..." + ext;
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
- const match = phone?.match(/(\d{3})(\d{3})(\d{4})$/);
577
- if (match) {
578
- const [, area, first, second] = match;
579
- const countryCode = phone!.substring(
580
- 0,
581
- phone!.length - area.length - first.length - second.length,
582
- ).trim();
583
- const normalizedCountryCode = countryCode
584
- ? countryCode.startsWith("+") ? countryCode : `+${countryCode}`
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
- text: string,
605
- chunkSize: number,
606
- linebreakDistance: number = 50,
607
- ): string[] {
608
- if (!text || chunkSize <= 0 || text.length < chunkSize) {
609
- return [text || ""];
610
- }
611
-
612
- const chunks: string[] = [];
613
- let start = 0;
614
-
615
- while (start < text.length) {
616
- let end = Math.min(start + chunkSize, text.length);
617
-
618
- if (end < text.length) {
619
- // Find the nearest line break around the target chunk boundary
620
- const beforeBreak = text.lastIndexOf("\n", end);
621
-
622
- let chosenBreak = -1;
623
- if (
624
- beforeBreak !== -1 && end - beforeBreak <= linebreakDistance &&
625
- beforeBreak > start
626
- ) {
627
- chosenBreak = beforeBreak + 1;
628
- }
629
-
630
- if (chosenBreak !== -1) {
631
- end = chosenBreak;
632
- } else {
633
- // Fall back to last whitespace near the end
634
- const lastSpace = text.lastIndexOf(" ", end);
635
- if (lastSpace > start && end - lastSpace < 100) {
636
- end = lastSpace;
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
- if (idStr && /^\d+$/.test(idStr)) {
655
- return "#" + idStr;
656
- }
657
- return idStr;
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
- const mod10 = num % 10;
668
- const mod100 = num % 100;
604
+ const mod10 = num % 10;
605
+ const mod100 = num % 100;
669
606
 
670
- if (mod10 === 1 && mod100 !== 11) {
671
- return `${num}st`;
672
- } else if (mod10 === 2 && mod100 !== 12) {
673
- return `${num}nd`;
674
- } else if (mod10 === 3 && mod100 !== 13) {
675
- return `${num}rd`;
676
- } else {
677
- return `${num}th`;
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
- address: Partial<AddressObj>,
691
- format: "REGION_ONLY" | "SINGLE_LINE" | "NORMAL" = "NORMAL",
692
- useFullCountryName: boolean = false,
693
- delimiter: string = ", ",
627
+ address: Partial<AddressObj>,
628
+ format: 'REGION_ONLY' | 'SINGLE_LINE' | 'NORMAL' = 'NORMAL',
629
+ useFullCountryName: boolean = false,
630
+ delimiter: string = ', ',
694
631
  ) {
695
- if (!address) {
696
- return null;
697
- }
698
-
699
- const countryName =
700
- useFullCountryName && i18n.has(`country.from_code.${address.country}`)
701
- ? i18n.t(`country.from_code.${address.country}`)
702
- : address.country;
703
-
704
- if (format === "REGION_ONLY") {
705
- return [
706
- address.city,
707
- address.state,
708
- countryName,
709
- ].filter(Boolean).join(delimiter);
710
- } else if (format === "SINGLE_LINE") {
711
- return [
712
- address.line1,
713
- address.line2,
714
- address.city,
715
- address.state,
716
- address.postalCode,
717
- countryName,
718
- ].filter(Boolean).join(delimiter);
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
- const fullName = buildSingleText([firstName, lastName], ' ');
740
- const displayName = guessFirstName(fullName, 8) || '?';
666
+ const fullName = buildSingleText([firstName, lastName], ' ');
667
+ const displayName = guessFirstName(fullName, 8) || '?';
668
+
669
+ return {
670
+ fullName,
671
+ displayName,
672
+ };
673
+ }
741
674
 
742
- return {
743
- fullName,
744
- displayName
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
  }