-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathstring.test.ts
More file actions
248 lines (207 loc) · 8.35 KB
/
Copy pathstring.test.ts
File metadata and controls
248 lines (207 loc) · 8.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* @vitest-environment node
*/
import { describe, expect, it } from 'vitest'
import {
forEachSearchOccurrence,
formatQuotedNameList,
isVersionedType,
normalizeEmail,
projectEscapedMarkdownForSearch,
sanitizeForJsonb,
sanitizeValueForJsonb,
slugify,
stripVersionSuffix,
truncate,
} from './string.js'
describe('slugify', () => {
it('lowercases and hyphenates a display name', () => {
expect(slugify('Acme Corp')).toBe('acme-corp')
})
it('collapses each run of non-alphanumerics into a single hyphen', () => {
expect(slugify('Sim.ai <> RVTech')).toBe('sim-ai-rvtech')
})
it('drops leading and trailing separators', () => {
expect(slugify(' !!Hello World!! ')).toBe('hello-world')
})
it('returns an empty string when nothing survives', () => {
expect(slugify('***')).toBe('')
expect(slugify('')).toBe('')
})
/* ASCII-only: the class drops non-Latin text rather than transliterating it. */
it('drops characters outside the ASCII alphanumerics', () => {
expect(slugify('Café')).toBe('caf')
expect(slugify('日本語')).toBe('')
})
it('preserves digits and hyphens already in the input', () => {
expect(slugify('workspace-2024')).toBe('workspace-2024')
})
})
describe('truncate', () => {
it('appends the suffix when the string exceeds the slice length', () => {
expect(truncate('hello world', 8)).toBe('hello wo...')
})
it('uses a custom suffix when provided', () => {
expect(truncate('hello world', 8, ' …')).toBe('hello wo …')
})
it('returns the original string when within the slice length', () => {
expect(truncate('hi', 10)).toBe('hi')
})
})
describe('stripVersionSuffix', () => {
it('strips a trailing _vN suffix', () => {
expect(stripVersionSuffix('notion_search_v2')).toBe('notion_search')
expect(stripVersionSuffix('github_create_pr_v3')).toBe('github_create_pr')
})
it('strips multi-digit versions', () => {
expect(stripVersionSuffix('x_v10')).toBe('x')
})
it('leaves plain values unchanged', () => {
expect(stripVersionSuffix('plain')).toBe('plain')
})
it('does not strip a non-version trailing token', () => {
expect(stripVersionSuffix('a_version')).toBe('a_version')
})
it('only strips the single trailing suffix', () => {
expect(stripVersionSuffix('a_v2_v3')).toBe('a_v2')
})
})
describe('isVersionedType', () => {
it('returns true for trailing _vN suffixes', () => {
expect(isVersionedType('notion_search_v2')).toBe(true)
expect(isVersionedType('github_create_pr_v3')).toBe(true)
expect(isVersionedType('x_v10')).toBe(true)
expect(isVersionedType('a_v2_v3')).toBe(true)
})
it('returns false when there is no trailing version suffix', () => {
expect(isVersionedType('plain')).toBe(false)
expect(isVersionedType('a_version')).toBe(false)
expect(isVersionedType('x')).toBe(false)
})
})
describe('sanitizeForJsonb', () => {
it('replaces a lone high surrogate left by mid-character truncation', () => {
// '𝐀'.slice(0, 1) cuts the surrogate pair in half
const cut = '\uD835\uDC00'.slice(0, 1)
expect(sanitizeForJsonb(`FIFA WORLD CU${cut}`)).toBe('FIFA WORLD CU\uFFFD')
})
it('replaces a lone low surrogate', () => {
expect(sanitizeForJsonb('x\uDC00y')).toBe('x\uFFFDy')
})
it('replaces NUL characters', () => {
expect(sanitizeForJsonb('a\u0000b')).toBe('a\uFFFDb')
})
it('preserves well-formed surrogate pairs', () => {
expect(sanitizeForJsonb('𝐅𝐈𝐅𝐀 🏆')).toBe('𝐅𝐈𝐅𝐀 🏆')
})
it('handles a lone high surrogate followed by a valid pair', () => {
expect(sanitizeForJsonb('\uD835\uD835\uDC00')).toBe('\uFFFD\uD835\uDC00')
})
})
describe('sanitizeValueForJsonb', () => {
it('sanitizes strings nested in objects and arrays', () => {
const input = { outline: ['ok', 'bad\uD835'], meta: { title: 'x\u0000' } }
expect(sanitizeValueForJsonb(input)).toEqual({
outline: ['ok', 'bad\uFFFD'],
meta: { title: 'x\uFFFD' },
})
})
it('sanitizes object keys', () => {
expect(sanitizeValueForJsonb({ 'k\uDC00': 1 })).toEqual({ 'k\uFFFD': 1 })
})
it('returns the same reference when nothing needs rewriting', () => {
const input = { a: ['clean', { b: 'also clean 🏆' }], n: 3 }
expect(sanitizeValueForJsonb(input)).toBe(input)
})
it('passes primitives through unchanged', () => {
expect(sanitizeValueForJsonb(42)).toBe(42)
expect(sanitizeValueForJsonb(null)).toBe(null)
expect(sanitizeValueForJsonb(undefined)).toBe(undefined)
})
})
describe('normalizeEmail', () => {
it('trims surrounding whitespace and lowercases', () => {
expect(normalizeEmail(' USER@Example.COM ')).toBe('user@example.com')
})
it('leaves an already-normalized email unchanged', () => {
expect(normalizeEmail('user@example.com')).toBe('user@example.com')
})
})
describe('formatQuotedNameList', () => {
it('lists all names quoted when within the cap', () => {
expect(formatQuotedNameList(['A', 'B'], 3)).toBe('"A", "B"')
})
it('truncates to the cap with an overflow tail', () => {
expect(formatQuotedNameList(['A', 'B', 'C', 'D', 'E'], 3)).toBe('"A", "B", "C" and 2 more')
})
it('returns an empty string for no names', () => {
expect(formatQuotedNameList([], 3)).toBe('')
})
})
describe('projectEscapedMarkdownForSearch', () => {
it('leaves text with no escapes alone', () => {
const { text, starts } = projectEscapedMarkdownForSearch('plain text')
expect(text).toBe('plain text')
expect(starts[0]).toBe(0)
expect(starts[text.length]).toBe(10)
})
it('drops the backslash a markdown escape carries', () => {
expect(projectEscapedMarkdownForSearch('SB\\_ACTION\\_ROUTER').text).toBe('SB_ACTION_ROUTER')
expect(projectEscapedMarkdownForSearch('{{TE\\_SERET}}').text).toBe('{{TE_SERET}}')
})
it('keeps a backslash that escapes nothing markdown cares about', () => {
expect(projectEscapedMarkdownForSearch('a\\nb').text).toBe('a\\nb')
expect(projectEscapedMarkdownForSearch('trailing\\').text).toBe('trailing\\')
})
/* The span must cover the backslash, or replacing a match would leave it stranded. */
it('maps a projected range back over the escape it consumed', () => {
const source = 'x SB\\_ACTION y'
const { text, starts } = projectEscapedMarkdownForSearch(source)
const start = text.indexOf('SB_ACTION')
const end = start + 'SB_ACTION'.length
expect(source.slice(starts[start], starts[end])).toBe('SB\\_ACTION')
})
it('maps every position when escapes repeat', () => {
const source = 'a\\_b\\_c'
const { text, starts } = projectEscapedMarkdownForSearch(source)
expect(text).toBe('a_b_c')
for (let i = 0; i < text.length; i += 1) {
expect(source.slice(starts[i], starts[i + 1]).endsWith(text[i])).toBe(true)
}
expect(starts[text.length]).toBe(source.length)
})
})
describe('forEachSearchOccurrence', () => {
const spans = (text: string, query: string, caseSensitive?: boolean) => {
const found: string[] = []
forEachSearchOccurrence(
text,
query,
(start, end) => found.push(text.slice(start, end)),
caseSensitive
)
return found
}
it('visits every non-overlapping occurrence, case-insensitively by default', () => {
expect(spans('Ab ab AB', 'ab')).toEqual(['Ab', 'ab', 'AB'])
expect(spans('aaaa', 'aa')).toEqual(['aa', 'aa'])
expect(spans('Ab', 'ab', true)).toEqual([])
})
it('folds whitespace so a typed space matches a non-breaking one', () => {
expect(spans('a\u00a0b', 'a b')).toEqual(['a\u00a0b'])
})
it('visits nothing for an empty query', () => {
expect(spans('abc', '')).toEqual([])
})
it('keeps context-sensitive lowercasing in the length-preserving fallback', () => {
// '\u0130' expands, so the whole-string fast path is unavailable. Folding the rest character by
// character would lowercase the word-final '\u03a3' to '\u03c3' instead of '\u03c2', making one
// unrelated code point change how every sigma in the string matches.
expect(spans('\u0130\u03a3', '\u0130\u03c2')).toEqual(['\u0130\u03a3'])
})
it('reports bounds into the caller\u2019s own string after a length-changing lowercase', () => {
// '\u0130'.toLowerCase() is TWO characters. A plain lowercase would slide every later index by
// one, so the caller would slice the wrong span out of the string it passed in.
expect(spans('\u0130xyz target', 'target')).toEqual(['target'])
})
})