@charcuterie/ui 4.28.4 → 4.29.0
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/dist/CatalogPicker/CatalogPicker.d.ts +105 -0
- package/dist/CatalogPicker/CatalogPicker.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPicker.js +152 -0
- package/dist/CatalogPicker/CatalogPicker.js.map +1 -0
- package/dist/CatalogPicker/CatalogPickerMark.d.ts +29 -0
- package/dist/CatalogPicker/CatalogPickerMark.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPickerMark.js +35 -0
- package/dist/CatalogPicker/CatalogPickerMark.js.map +1 -0
- package/dist/CatalogPicker/CatalogPickerOption.d.ts +40 -0
- package/dist/CatalogPicker/CatalogPickerOption.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPickerOption.js +42 -0
- package/dist/CatalogPicker/CatalogPickerOption.js.map +1 -0
- package/dist/CatalogPicker/CatalogPickerPanel.d.ts +60 -0
- package/dist/CatalogPicker/CatalogPickerPanel.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPickerPanel.js +409 -0
- package/dist/CatalogPicker/CatalogPickerPanel.js.map +1 -0
- package/dist/CatalogPicker/CatalogPickerRail.d.ts +50 -0
- package/dist/CatalogPicker/CatalogPickerRail.d.ts.map +1 -0
- package/dist/CatalogPicker/CatalogPickerRail.js +114 -0
- package/dist/CatalogPicker/CatalogPickerRail.js.map +1 -0
- package/dist/CatalogPicker/catalogPickerDrawings.d.ts +10 -0
- package/dist/CatalogPicker/catalogPickerDrawings.d.ts.map +1 -0
- package/dist/CatalogPicker/catalogPickerDrawings.js +29 -0
- package/dist/CatalogPicker/catalogPickerDrawings.js.map +1 -0
- package/dist/CatalogPicker/catalogPickerView.d.ts +173 -0
- package/dist/CatalogPicker/catalogPickerView.d.ts.map +1 -0
- package/dist/CatalogPicker/catalogPickerView.js +235 -0
- package/dist/CatalogPicker/catalogPickerView.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/CatalogPicker/CatalogPicker.mdx +157 -0
- package/src/CatalogPicker/CatalogPicker.stories.tsx +218 -0
- package/src/CatalogPicker/CatalogPicker.test.tsx +1352 -0
- package/src/CatalogPicker/CatalogPicker.tsx +376 -0
- package/src/CatalogPicker/CatalogPickerMark.tsx +89 -0
- package/src/CatalogPicker/CatalogPickerOption.tsx +138 -0
- package/src/CatalogPicker/CatalogPickerPanel.tsx +780 -0
- package/src/CatalogPicker/CatalogPickerRail.tsx +214 -0
- package/src/CatalogPicker/catalogPicker.storyHelpers.tsx +276 -0
- package/src/CatalogPicker/catalogPickerDrawings.tsx +51 -0
- package/src/CatalogPicker/catalogPickerView.test.ts +346 -0
- package/src/CatalogPicker/catalogPickerView.ts +457 -0
- package/src/Dialog/Dialog.test.tsx +20 -289
- package/src/Field/Field.test.tsx +11 -405
- package/src/index.ts +6 -0
- package/src/sourceRules.test.ts +5 -1
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import type { ReactNode } from "react"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* One rail tab's subject: a provider, a vendor, an account — whatever
|
|
5
|
+
* the catalog is browsed by.
|
|
6
|
+
*/
|
|
7
|
+
export type CatalogPickerGroup = {
|
|
8
|
+
/**
|
|
9
|
+
* Draws the initials badge (when the group has no `icon`) and rings
|
|
10
|
+
* the icon. Any CSS color. It is a ring, never the text or the fill
|
|
11
|
+
* behind text, so it cannot fail the contrast gate.
|
|
12
|
+
*/
|
|
13
|
+
accentColor?: string
|
|
14
|
+
/** The app's own icon. Charcuterie ships none; without one the group gets an initials badge. */
|
|
15
|
+
icon?: ReactNode
|
|
16
|
+
/** Unique across `groups`. A later group with an `id` already used is dropped, with a console warning. */
|
|
17
|
+
id: string
|
|
18
|
+
/** The rail tab's accessible name and tooltip, and the heading over this group's search results. */
|
|
19
|
+
label: string
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type CatalogPickerItem = {
|
|
23
|
+
/** Secondary text beside the label — an account, a slug. Searched. */
|
|
24
|
+
description?: string
|
|
25
|
+
/**
|
|
26
|
+
* The group this item is browsed under, or `null` for none. An item
|
|
27
|
+
* with no group — or with a `groupId` that names no group — is
|
|
28
|
+
* listed on every tab, after the pinned-start rows, so it is never
|
|
29
|
+
* out of reach.
|
|
30
|
+
*/
|
|
31
|
+
groupId: string | null
|
|
32
|
+
/** Overrides the group's icon or badge for this row. */
|
|
33
|
+
icon?: ReactNode
|
|
34
|
+
isDisabled?: boolean
|
|
35
|
+
/** The row's name and the trigger's text. Searched. */
|
|
36
|
+
label: string
|
|
37
|
+
/**
|
|
38
|
+
* Shown at the top (`"start"`) or the bottom (`"end"`) of every rail
|
|
39
|
+
* view, and of the search results when it matches — a "Default"
|
|
40
|
+
* choice that belongs to no tab.
|
|
41
|
+
*/
|
|
42
|
+
pinned?: "end" | "start"
|
|
43
|
+
/** Unique across `items`. A later item with a `value` already used is dropped, with a console warning. */
|
|
44
|
+
value: string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A tab on the rail. Keys are prefixed so a group whose `id` is
|
|
49
|
+
* `"favorites"` cannot collide with the favorites tab.
|
|
50
|
+
*/
|
|
51
|
+
export type CatalogPickerTab =
|
|
52
|
+
| {
|
|
53
|
+
key: typeof FAVORITES_TAB_KEY
|
|
54
|
+
kind: "favorites"
|
|
55
|
+
label: string
|
|
56
|
+
}
|
|
57
|
+
| {
|
|
58
|
+
group: CatalogPickerGroup
|
|
59
|
+
key: string
|
|
60
|
+
kind: "group"
|
|
61
|
+
label: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* One run of rows. A section with a `heading` is a labelled
|
|
66
|
+
* `role="group"` inside the listbox; one without is a plain run of
|
|
67
|
+
* options (pinned rows, and a browsing tab's own rows).
|
|
68
|
+
*/
|
|
69
|
+
export type CatalogPickerSection = {
|
|
70
|
+
heading?: string
|
|
71
|
+
items: readonly CatalogPickerItem[]
|
|
72
|
+
key: string
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export const FAVORITES_TAB_KEY = "favorites:"
|
|
76
|
+
|
|
77
|
+
export const toGroupTabKey = (groupId: string) =>
|
|
78
|
+
`group:${groupId}`
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* The catalog with every repeated item `value` and group `id` dropped:
|
|
82
|
+
* **the first one wins**. A value names one row, so two rows with one
|
|
83
|
+
* value would both read as chosen, and the trigger, the Favorites tab
|
|
84
|
+
* and the highlight could each settle on a different one. The dropped
|
|
85
|
+
* values and ids are returned so the picker can warn about them.
|
|
86
|
+
*/
|
|
87
|
+
export const toUniqueCatalog = ({
|
|
88
|
+
groups,
|
|
89
|
+
items,
|
|
90
|
+
}: {
|
|
91
|
+
groups: readonly CatalogPickerGroup[]
|
|
92
|
+
items: readonly CatalogPickerItem[]
|
|
93
|
+
}) => {
|
|
94
|
+
const groupIds = new Set<string>()
|
|
95
|
+
const duplicateGroupIds: string[] = []
|
|
96
|
+
|
|
97
|
+
const uniqueGroups = groups.filter((group) => {
|
|
98
|
+
if (groupIds.has(group.id)) {
|
|
99
|
+
duplicateGroupIds.push(group.id)
|
|
100
|
+
|
|
101
|
+
return false
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
groupIds.add(group.id)
|
|
105
|
+
|
|
106
|
+
return true
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
const itemValues = new Set<string>()
|
|
110
|
+
const duplicateItemValues: string[] = []
|
|
111
|
+
|
|
112
|
+
const uniqueItems = items.filter((item) => {
|
|
113
|
+
if (itemValues.has(item.value)) {
|
|
114
|
+
duplicateItemValues.push(item.value)
|
|
115
|
+
|
|
116
|
+
return false
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
itemValues.add(item.value)
|
|
120
|
+
|
|
121
|
+
return true
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
return {
|
|
125
|
+
duplicateGroupIds,
|
|
126
|
+
duplicateItemValues,
|
|
127
|
+
groups:
|
|
128
|
+
duplicateGroupIds.length === 0
|
|
129
|
+
? groups
|
|
130
|
+
: uniqueGroups,
|
|
131
|
+
items:
|
|
132
|
+
duplicateItemValues.length === 0
|
|
133
|
+
? items
|
|
134
|
+
: uniqueItems,
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Whether a row is listed on every tab rather than under one: a pinned
|
|
140
|
+
* row, and an unpinned row with no group (or a `groupId` that names no
|
|
141
|
+
* group). Without this an ungrouped row would be on no tab at all, and
|
|
142
|
+
* reachable only by search.
|
|
143
|
+
*/
|
|
144
|
+
export const isOnEveryTab = ({
|
|
145
|
+
groupIds,
|
|
146
|
+
item,
|
|
147
|
+
}: {
|
|
148
|
+
groupIds: ReadonlySet<string>
|
|
149
|
+
item: CatalogPickerItem
|
|
150
|
+
}) =>
|
|
151
|
+
item.pinned !== undefined ||
|
|
152
|
+
item.groupId === null ||
|
|
153
|
+
!groupIds.has(item.groupId)
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The words of a query, lowercased. Whitespace separates them; an
|
|
157
|
+
* all-space query has none, and is treated as no query.
|
|
158
|
+
*/
|
|
159
|
+
export const toQueryWords = (query: string) =>
|
|
160
|
+
query
|
|
161
|
+
.toLowerCase()
|
|
162
|
+
.split(/\s+/u)
|
|
163
|
+
.filter((word) => word.length > 0)
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Substring match, case-insensitive, every word required: "opus 5"
|
|
167
|
+
* finds "Claude Opus 5.5", and so does "5 opus". The text searched is
|
|
168
|
+
* the label, the description and the group's label, so typing a
|
|
169
|
+
* provider name lists that provider's rows.
|
|
170
|
+
*/
|
|
171
|
+
export const isCatalogItemMatch = ({
|
|
172
|
+
groupLabel,
|
|
173
|
+
item,
|
|
174
|
+
words,
|
|
175
|
+
}: {
|
|
176
|
+
groupLabel: string | undefined
|
|
177
|
+
item: CatalogPickerItem
|
|
178
|
+
words: readonly string[]
|
|
179
|
+
}) => {
|
|
180
|
+
const haystack = [
|
|
181
|
+
item.label,
|
|
182
|
+
item.description ?? "",
|
|
183
|
+
groupLabel ?? "",
|
|
184
|
+
]
|
|
185
|
+
.join("\n")
|
|
186
|
+
.toLowerCase()
|
|
187
|
+
|
|
188
|
+
return words.every((word) => haystack.includes(word))
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* The favorites, resolved to items in `favorites` order, once each. A
|
|
193
|
+
* value that names no item is skipped. A pinned or ungrouped favorite
|
|
194
|
+
* is included here — it counts toward showing the Favorites tab — but
|
|
195
|
+
* the tab lists it only once, where every tab lists it.
|
|
196
|
+
*/
|
|
197
|
+
export const resolveFavoriteItems = ({
|
|
198
|
+
favorites,
|
|
199
|
+
items,
|
|
200
|
+
}: {
|
|
201
|
+
favorites: readonly string[]
|
|
202
|
+
items: readonly CatalogPickerItem[]
|
|
203
|
+
}) => {
|
|
204
|
+
const byValue = new Map<string, CatalogPickerItem>()
|
|
205
|
+
|
|
206
|
+
for (const item of items) {
|
|
207
|
+
if (!byValue.has(item.value)) {
|
|
208
|
+
byValue.set(item.value, item)
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const seen = new Set<string>()
|
|
213
|
+
|
|
214
|
+
return favorites.flatMap((value) => {
|
|
215
|
+
const item = byValue.get(value)
|
|
216
|
+
|
|
217
|
+
if (item === undefined || seen.has(value)) {
|
|
218
|
+
return []
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
seen.add(value)
|
|
222
|
+
|
|
223
|
+
return [item]
|
|
224
|
+
})
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* The rail, top to bottom: the favorites tab when any favorite
|
|
229
|
+
* resolves, then one tab per group that has a row of its own, in
|
|
230
|
+
* `groups` order.
|
|
231
|
+
*/
|
|
232
|
+
export const buildCatalogTabs = ({
|
|
233
|
+
favorites,
|
|
234
|
+
favoritesLabel,
|
|
235
|
+
groups,
|
|
236
|
+
items,
|
|
237
|
+
}: {
|
|
238
|
+
favorites: readonly string[]
|
|
239
|
+
favoritesLabel: string
|
|
240
|
+
groups: readonly CatalogPickerGroup[]
|
|
241
|
+
items: readonly CatalogPickerItem[]
|
|
242
|
+
}): CatalogPickerTab[] => {
|
|
243
|
+
const groupIdsWithRows = new Set(
|
|
244
|
+
items
|
|
245
|
+
.filter((item) => item.pinned === undefined)
|
|
246
|
+
.map((item) => item.groupId),
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
const groupTabs = groups
|
|
250
|
+
.filter((group) => groupIdsWithRows.has(group.id))
|
|
251
|
+
.map(
|
|
252
|
+
(group): CatalogPickerTab => ({
|
|
253
|
+
group,
|
|
254
|
+
key: toGroupTabKey(group.id),
|
|
255
|
+
kind: "group",
|
|
256
|
+
label: group.label,
|
|
257
|
+
}),
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
const hasFavorites =
|
|
261
|
+
resolveFavoriteItems({ favorites, items }).length > 0
|
|
262
|
+
|
|
263
|
+
return hasFavorites
|
|
264
|
+
? [
|
|
265
|
+
{
|
|
266
|
+
key: FAVORITES_TAB_KEY,
|
|
267
|
+
kind: "favorites",
|
|
268
|
+
label: favoritesLabel,
|
|
269
|
+
},
|
|
270
|
+
...groupTabs,
|
|
271
|
+
]
|
|
272
|
+
: groupTabs
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Where a fresh open lands: Favorites when the chosen value is a
|
|
277
|
+
* favorite or a row on every tab (and there are favorites), else the
|
|
278
|
+
* chosen row's group, else Favorites, else the first group.
|
|
279
|
+
* `undefined` only for an empty rail.
|
|
280
|
+
*/
|
|
281
|
+
export const pickInitialTabKey = ({
|
|
282
|
+
favorites,
|
|
283
|
+
items,
|
|
284
|
+
tabs,
|
|
285
|
+
value,
|
|
286
|
+
}: {
|
|
287
|
+
favorites: readonly string[]
|
|
288
|
+
items: readonly CatalogPickerItem[]
|
|
289
|
+
tabs: readonly CatalogPickerTab[]
|
|
290
|
+
value: string | undefined
|
|
291
|
+
}): string | undefined => {
|
|
292
|
+
const hasFavoritesTab = tabs.some(
|
|
293
|
+
(tab) => tab.kind === "favorites",
|
|
294
|
+
)
|
|
295
|
+
|
|
296
|
+
const groupIds = new Set(
|
|
297
|
+
tabs.flatMap((tab) =>
|
|
298
|
+
tab.kind === "group" ? [tab.group.id] : [],
|
|
299
|
+
),
|
|
300
|
+
)
|
|
301
|
+
|
|
302
|
+
const chosen =
|
|
303
|
+
value === undefined
|
|
304
|
+
? undefined
|
|
305
|
+
: items.find((item) => item.value === value)
|
|
306
|
+
|
|
307
|
+
if (
|
|
308
|
+
hasFavoritesTab &&
|
|
309
|
+
chosen !== undefined &&
|
|
310
|
+
(isOnEveryTab({ groupIds, item: chosen }) ||
|
|
311
|
+
favorites.includes(chosen.value))
|
|
312
|
+
) {
|
|
313
|
+
return FAVORITES_TAB_KEY
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (chosen?.groupId != null) {
|
|
317
|
+
const groupKey = toGroupTabKey(chosen.groupId)
|
|
318
|
+
|
|
319
|
+
if (tabs.some((tab) => tab.key === groupKey)) {
|
|
320
|
+
return groupKey
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
return tabs[0]?.key
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* The rows the list shows, as sections. Each row appears once.
|
|
329
|
+
*
|
|
330
|
+
* **Browsing** (empty query): the pinned-start rows, the ungrouped
|
|
331
|
+
* rows, the tab's own rows (favorites in `favorites` order, a group's
|
|
332
|
+
* rows in `items` order), the pinned-end rows. The Favorites tab's own
|
|
333
|
+
* rows leave out a favorite that is already pinned or ungrouped, since
|
|
334
|
+
* every tab lists those. An empty rail browses every unpinned row.
|
|
335
|
+
*
|
|
336
|
+
* **Searching**: matches across every item, whatever tab is current —
|
|
337
|
+
* the pinned-start matches, the ungrouped matches, one headed section
|
|
338
|
+
* per group in `groups` order, the pinned-end matches. Favorites are
|
|
339
|
+
* not their own section in search: a favorite is found under its
|
|
340
|
+
* group, once.
|
|
341
|
+
*/
|
|
342
|
+
export const buildCatalogSections = ({
|
|
343
|
+
favorites,
|
|
344
|
+
groups,
|
|
345
|
+
items,
|
|
346
|
+
query,
|
|
347
|
+
tab,
|
|
348
|
+
}: {
|
|
349
|
+
favorites: readonly string[]
|
|
350
|
+
groups: readonly CatalogPickerGroup[]
|
|
351
|
+
items: readonly CatalogPickerItem[]
|
|
352
|
+
query: string
|
|
353
|
+
tab: CatalogPickerTab | undefined
|
|
354
|
+
}): CatalogPickerSection[] => {
|
|
355
|
+
const words = toQueryWords(query)
|
|
356
|
+
|
|
357
|
+
const groupById = new Map(
|
|
358
|
+
groups.map((group) => [group.id, group] as const),
|
|
359
|
+
)
|
|
360
|
+
|
|
361
|
+
const groupIds = new Set(groupById.keys())
|
|
362
|
+
|
|
363
|
+
const groupLabelOf = (item: CatalogPickerItem) =>
|
|
364
|
+
item.groupId === null
|
|
365
|
+
? undefined
|
|
366
|
+
: groupById.get(item.groupId)?.label
|
|
367
|
+
|
|
368
|
+
const isSearching = words.length > 0
|
|
369
|
+
|
|
370
|
+
const shown = isSearching
|
|
371
|
+
? items.filter((item) =>
|
|
372
|
+
isCatalogItemMatch({
|
|
373
|
+
groupLabel: groupLabelOf(item),
|
|
374
|
+
item,
|
|
375
|
+
words,
|
|
376
|
+
}),
|
|
377
|
+
)
|
|
378
|
+
: items
|
|
379
|
+
|
|
380
|
+
const pinnedStart = shown.filter(
|
|
381
|
+
(item) => item.pinned === "start",
|
|
382
|
+
)
|
|
383
|
+
|
|
384
|
+
const pinnedEnd = shown.filter(
|
|
385
|
+
(item) => item.pinned === "end",
|
|
386
|
+
)
|
|
387
|
+
|
|
388
|
+
const unpinned = shown.filter(
|
|
389
|
+
(item) => item.pinned === undefined,
|
|
390
|
+
)
|
|
391
|
+
|
|
392
|
+
const ungrouped: CatalogPickerSection = {
|
|
393
|
+
items: unpinned.filter((item) =>
|
|
394
|
+
isOnEveryTab({ groupIds, item }),
|
|
395
|
+
),
|
|
396
|
+
key: "ungrouped",
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const middle = ((): CatalogPickerSection[] => {
|
|
400
|
+
if (isSearching) {
|
|
401
|
+
return [
|
|
402
|
+
ungrouped,
|
|
403
|
+
...groups.map(
|
|
404
|
+
(group): CatalogPickerSection => ({
|
|
405
|
+
heading: group.label,
|
|
406
|
+
items: unpinned.filter(
|
|
407
|
+
(item) => item.groupId === group.id,
|
|
408
|
+
),
|
|
409
|
+
key: toGroupTabKey(group.id),
|
|
410
|
+
}),
|
|
411
|
+
),
|
|
412
|
+
]
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
if (tab === undefined) {
|
|
416
|
+
return [{ items: unpinned, key: "all" }]
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return [
|
|
420
|
+
ungrouped,
|
|
421
|
+
{
|
|
422
|
+
items:
|
|
423
|
+
tab.kind === "favorites"
|
|
424
|
+
? resolveFavoriteItems({
|
|
425
|
+
favorites,
|
|
426
|
+
items,
|
|
427
|
+
}).filter(
|
|
428
|
+
(item) => !isOnEveryTab({ groupIds, item }),
|
|
429
|
+
)
|
|
430
|
+
: unpinned.filter(
|
|
431
|
+
(item) => item.groupId === tab.group.id,
|
|
432
|
+
),
|
|
433
|
+
key: tab.key,
|
|
434
|
+
},
|
|
435
|
+
]
|
|
436
|
+
})()
|
|
437
|
+
|
|
438
|
+
return [
|
|
439
|
+
{ items: pinnedStart, key: "pinned-start" },
|
|
440
|
+
...middle,
|
|
441
|
+
{ items: pinnedEnd, key: "pinned-end" },
|
|
442
|
+
].filter((section) => section.items.length > 0)
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* A badge's text: the first letter or digit of each of the first two
|
|
447
|
+
* words that have one, so "Northwind" is "N", "Contoso Labs" is "CL",
|
|
448
|
+
* and "Contoso (Work)" is "CW" rather than "C(".
|
|
449
|
+
*/
|
|
450
|
+
export const toInitials = (label: string) =>
|
|
451
|
+
label
|
|
452
|
+
.split(/\s+/u)
|
|
453
|
+
.map((word) => word.match(/[\p{L}\p{N}]/u)?.[0])
|
|
454
|
+
.filter((initial) => initial !== undefined)
|
|
455
|
+
.slice(0, 2)
|
|
456
|
+
.join("")
|
|
457
|
+
.toUpperCase()
|