@jsb188/app 1.1.7 → 1.1.9

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.
@@ -0,0 +1,281 @@
1
+ import { beforeAll, describe, expect, it } from 'vitest';
2
+
3
+ import { configI18n } from '../main.ts';
4
+ import {
5
+ addIndefiniteArticle,
6
+ buildSingleText,
7
+ camelCaseToDash,
8
+ cn,
9
+ convertToInternationalPhone,
10
+ escapeCSVValue,
11
+ formatPhoneNumber,
12
+ formatReferenceNumber,
13
+ getAlphanumeric,
14
+ getDisplayName,
15
+ getNthString,
16
+ getSearchNames,
17
+ guessFirstName,
18
+ isEmail,
19
+ isInvisibleString,
20
+ isPhone,
21
+ joinReadable,
22
+ makeAddressText,
23
+ passwordStrength,
24
+ regexFromText,
25
+ removeGeneratedTextGarbage,
26
+ shortenToParagraph,
27
+ shortenToSpace,
28
+ splitTextIntoChunks,
29
+ textWithBrackets,
30
+ truncateFileName,
31
+ ucFirst,
32
+ } from './string.ts';
33
+
34
+ beforeAll(() => {
35
+ configI18n();
36
+ });
37
+
38
+ describe('cn', () => {
39
+ it('joins truthy class names with spaces', () => {
40
+ expect(cn('card', false, 'selected', undefined, null, '')).toBe('card selected');
41
+ });
42
+ });
43
+
44
+ describe('shortenToSpace', () => {
45
+ it('shortens at the last available space before the limit', () => {
46
+ expect(shortenToSpace('alpha beta gamma', 12)).toBe('alpha beta...');
47
+ });
48
+
49
+ it('falls back to a hard cut when no spaces exist', () => {
50
+ expect(shortenToSpace('alphabetagamma', 5)).toBe('alpha...');
51
+ });
52
+ });
53
+
54
+ describe('guessFirstName', () => {
55
+ it('drops leading articles and returns the first meaningful word', () => {
56
+ expect(guessFirstName('the quick brown fox')).toBe('quick');
57
+ });
58
+
59
+ it('keeps short names instead of stripping the full word as an article suffix', () => {
60
+ expect(guessFirstName('An Lee')).toBe('Lee');
61
+ });
62
+ });
63
+
64
+ describe('addIndefiniteArticle', () => {
65
+ it('handles silent-h and consonant-sounding vowel exceptions', () => {
66
+ expect(addIndefiniteArticle('honest answer')).toBe('an honest answer');
67
+ expect(addIndefiniteArticle('user')).toBe('a user');
68
+ });
69
+ });
70
+
71
+ describe('isEmail', () => {
72
+ it('accepts trimmed valid emails and rejects malformed values', () => {
73
+ expect(isEmail(' hello@example.com ')).toBe(true);
74
+ expect(isEmail('hello@example')).toBe(false);
75
+ });
76
+ });
77
+
78
+ describe('convertToInternationalPhone and isPhone', () => {
79
+ it('normalizes North American and existing international numbers', () => {
80
+ expect(convertToInternationalPhone('(555) 123-4567')).toBe('+15551234567');
81
+ expect(convertToInternationalPhone('+44 20 1234 5678')).toBe('+442012345678');
82
+ });
83
+
84
+ it('rejects invalid phone-like input', () => {
85
+ expect(convertToInternationalPhone('05551234567')).toBeNull();
86
+ expect(convertToInternationalPhone('555-abc-1234')).toBeNull();
87
+ });
88
+
89
+ it('supports optional conversion when validating phone numbers', () => {
90
+ expect(isPhone('5551234567')).toBe(true);
91
+ expect(isPhone('5551234567', true)).toBe(false);
92
+ expect(isPhone('+442012345678', true)).toBe(true);
93
+ });
94
+ });
95
+
96
+ describe('passwordStrength', () => {
97
+ it('reports rule-by-rule password strength details', () => {
98
+ expect(passwordStrength('Abcdef1!')).toEqual({
99
+ valid: true,
100
+ isLongEnough: true,
101
+ hasLowercase: true,
102
+ hasUppercase: true,
103
+ hasDigit: true,
104
+ hasSpecial: true,
105
+ });
106
+
107
+ expect(passwordStrength('abcdefg')).toMatchObject({
108
+ valid: false,
109
+ isLongEnough: false,
110
+ hasLowercase: true,
111
+ hasUppercase: false,
112
+ hasDigit: false,
113
+ hasSpecial: false,
114
+ });
115
+ });
116
+ });
117
+
118
+ describe('getAlphanumeric and getSearchNames', () => {
119
+ it('removes punctuation while optionally preserving spaces', () => {
120
+ expect(getAlphanumeric('A-B_C!', false)).toBe('ABC');
121
+ expect(getAlphanumeric('A-B_C!', true)).toBe('ABC');
122
+ });
123
+
124
+ it('splits mixed delimiters and keeps case-insensitive unique names', () => {
125
+ expect(getSearchNames(['Alice, Bob and alice', 'Carol;\nBob', null])).toEqual([
126
+ 'Alice',
127
+ 'Bob',
128
+ 'Carol',
129
+ ]);
130
+ });
131
+ });
132
+
133
+ describe('regexFromText', () => {
134
+ it('builds a case-insensitive regex from cleaned words', () => {
135
+ const regex = regexFromText('Hello, world!');
136
+
137
+ expect(regex).not.toBeNull();
138
+ expect('say hello there'.match(regex!)).toEqual(['hello']);
139
+ expect('WORLD tour'.match(regex!)).toEqual(['WORLD']);
140
+ });
141
+
142
+ it('returns null when no searchable words remain', () => {
143
+ expect(regexFromText('a !')).toBeNull();
144
+ });
145
+ });
146
+
147
+ describe('ucFirst and camelCaseToDash', () => {
148
+ it('formats capitalization and dash-separated names', () => {
149
+ expect(ucFirst('marketday')).toBe('Marketday');
150
+ expect(camelCaseToDash('camelCase2Value')).toBe('camel-case-2-value');
151
+ expect(camelCaseToDash('API')).toBe('api');
152
+ });
153
+ });
154
+
155
+ describe('shortenToParagraph', () => {
156
+ it('uses paragraph boundaries before falling back to word trimming', () => {
157
+ const text = 'First line\nSecond line\nThird line';
158
+
159
+ expect(shortenToParagraph(text, 18)).toBe('First line');
160
+ expect(shortenToParagraph('Alpha beta gamma', 10)).toBe('Alpha...');
161
+ });
162
+ });
163
+
164
+ describe('isInvisibleString', () => {
165
+ it('detects strings made only from whitespace-like invisible characters', () => {
166
+ expect(isInvisibleString('ㅤ️⠀')).toBe(true);
167
+ expect(isInvisibleString(' visible ')).toBe(false);
168
+ });
169
+ });
170
+
171
+ describe('buildSingleText', () => {
172
+ it('supports feature maps, conditional lines, first-match groups, and blank line collapsing', () => {
173
+ expect(
174
+ buildSingleText(
175
+ [
176
+ 'Header',
177
+ '',
178
+ '',
179
+ { advanced: ['Advanced section'] },
180
+ [['beta', '!legacy'], ['Beta only']],
181
+ [
182
+ [['admin'], ['Admin choice']],
183
+ [['member'], [' Member choice']],
184
+ [['!member'], ['Fallback choice']],
185
+ ],
186
+ 'Footer',
187
+ ],
188
+ '\n',
189
+ ['advanced', 'beta', 'member'],
190
+ ),
191
+ ).toBe('Header\n\nAdvanced section\nBeta only\nMember choice\nFooter');
192
+ });
193
+ });
194
+
195
+ describe('joinReadable and textWithBrackets', () => {
196
+ it('joins human-readable lists and appends bracket text when present', () => {
197
+ expect(joinReadable(['Alpha', 'Beta', 'Gamma'])).toBe('Alpha, Beta and Gamma');
198
+ expect(textWithBrackets('Status', ['draft', 'internal'], ['[', ']'])).toBe('Status [draft internal]');
199
+ expect(textWithBrackets('', ['draft', 'internal'], ['[', ']'])).toBe('draft internal');
200
+ });
201
+ });
202
+
203
+ describe('removeGeneratedTextGarbage and escapeCSVValue', () => {
204
+ it('normalizes generated whitespace and escapes CSV-sensitive values', () => {
205
+ expect(removeGeneratedTextGarbage('Hello\n\n world\tfrom\r\nAI')).toBe('Hello world from AI');
206
+ expect(escapeCSVValue('plain')).toBe('plain');
207
+ expect(escapeCSVValue('a,"b"\nc')).toBe('"a,""b""\nc"');
208
+ });
209
+ });
210
+
211
+ describe('truncateFileName', () => {
212
+ it('preserves extensions while truncating long file names', () => {
213
+ expect(truncateFileName('very-long-document-name.pdf', 12)).toBe('very-....pdf');
214
+ expect(truncateFileName('short.txt', 20)).toBe('short.txt');
215
+ });
216
+ });
217
+
218
+ describe('formatPhoneNumber', () => {
219
+ it('formats trailing 10 digits into a readable North American phone number', () => {
220
+ expect(formatPhoneNumber('+15551234567')).toBe('+1 (555) 123-4567');
221
+ expect(formatPhoneNumber('5551234567')).toBe('(555) 123-4567');
222
+ expect(formatPhoneNumber('not-a-phone')).toBe('not-a-phone');
223
+ });
224
+ });
225
+
226
+ describe('splitTextIntoChunks', () => {
227
+ it('prefers nearby line breaks when splitting long text', () => {
228
+ expect(splitTextIntoChunks('12345\n67890\nabcde', 10, 5)).toEqual([
229
+ '12345',
230
+ '67890',
231
+ 'abcde',
232
+ ]);
233
+ });
234
+
235
+ it('falls back to whitespace boundaries when no line break is nearby', () => {
236
+ expect(splitTextIntoChunks('alpha beta gamma delta', 12, 2)).toEqual([
237
+ 'alpha beta',
238
+ 'gamma delta',
239
+ ]);
240
+ });
241
+ });
242
+
243
+ describe('formatReferenceNumber and getNthString', () => {
244
+ it('formats numeric references and ordinal suffixes', () => {
245
+ expect(formatReferenceNumber('42')).toBe('#42');
246
+ expect(formatReferenceNumber('abc')).toBe('abc');
247
+ expect(getNthString(1)).toBe('1st');
248
+ expect(getNthString(2)).toBe('2nd');
249
+ expect(getNthString(3)).toBe('3rd');
250
+ expect(getNthString(11)).toBe('11th');
251
+ expect(getNthString(23)).toBe('23rd');
252
+ });
253
+ });
254
+
255
+ describe('makeAddressText and getDisplayName', () => {
256
+ it('formats addresses in each supported display mode', () => {
257
+ const address = {
258
+ line1: '1 Main St',
259
+ line2: 'Suite 2',
260
+ city: 'Toronto',
261
+ state: 'ON',
262
+ postalCode: 'A1A 1A1',
263
+ country: 'CA',
264
+ };
265
+
266
+ expect(makeAddressText(address, 'REGION_ONLY')).toBe('Toronto, ON, CA');
267
+ expect(makeAddressText(address, 'SINGLE_LINE', true)).toBe(
268
+ '1 Main St, Suite 2, Toronto, ON, A1A 1A1, Canada',
269
+ );
270
+ expect(makeAddressText(address)).toBe(
271
+ '1 Main St\nSuite 2\nToronto, ON A1A 1A1\nCA',
272
+ );
273
+ });
274
+
275
+ it('builds a full name and short display name together', () => {
276
+ expect(getDisplayName('John', 'Smith')).toEqual({
277
+ fullName: 'John Smith',
278
+ displayName: 'John',
279
+ });
280
+ });
281
+ });