@zooui/nuxt 0.2.0 → 0.3.1

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,62 @@
1
+ // Search for ZFilterSelect: a literal, case-insensitive substring match, and the label cut into the segments that
2
+ // matched and the ones that didn't, so the matches are drawn as <mark> elements rather than written into v-html.
3
+ // The query is text, never a pattern: `(`, `.` or `\` match themselves and nothing else.
4
+ //
5
+ // Case is folded one character at a time, the same way for the label and the query, so a match found in the folded
6
+ // text always maps back onto whole characters of the label, even where lowercasing changes a character's length
7
+ // ('İ' becomes 'i' and a combining dot).
8
+
9
+ /** A run of a label: matched by the search, or not. Joined in order, the segments are the label. */
10
+ export interface FilterSelectSegment {
11
+ text: string
12
+ match: boolean
13
+ }
14
+
15
+ /** The text with each character lowercased, and the span of the original each folded code unit came from. */
16
+ function fold(text: string) {
17
+ let folded = ''
18
+ const start: number[] = []
19
+ const end: number[] = []
20
+ let at = 0
21
+ for (const ch of text) {
22
+ const lower = ch.toLowerCase()
23
+ for (let k = 0; k < lower.length; k++) {
24
+ start.push(at)
25
+ end.push(at + ch.length)
26
+ }
27
+ folded += lower
28
+ at += ch.length
29
+ }
30
+ return { folded, start, end }
31
+ }
32
+
33
+ /** Whether `text` contains `query`, ignoring case. An empty query matches everything. */
34
+ export function filterSelectMatches(text: string, query: string): boolean {
35
+ if (!query) return true
36
+ return fold(text).folded.includes(fold(query).folded)
37
+ }
38
+
39
+ /** `text` cut into matched and unmatched runs for `query`, left to right, matches not overlapping. */
40
+ export function filterSelectSegments(text: string, query: string): FilterSelectSegment[] {
41
+ if (!text) return []
42
+ const q = query ? fold(query).folded : ''
43
+ if (!q) return [{ text, match: false }]
44
+ const { folded, start, end } = fold(text)
45
+ const out: FilterSelectSegment[] = []
46
+ const push = (from: number, to: number, match: boolean) => {
47
+ if (to <= from) return
48
+ const last = out.at(-1)
49
+ if (last && last.match === match) last.text += text.slice(from, to)
50
+ else out.push({ text: text.slice(from, to), match })
51
+ }
52
+ let done = 0
53
+ for (let at = folded.indexOf(q); at !== -1; at = folded.indexOf(q, at + q.length)) {
54
+ const from = Math.max(start[at]!, done)
55
+ const to = end[at + q.length - 1]!
56
+ push(done, from, false)
57
+ push(from, to, true)
58
+ done = Math.max(done, to)
59
+ }
60
+ push(done, text.length, false)
61
+ return out
62
+ }
@@ -0,0 +1,42 @@
1
+ // ZOrderList's ordering, framework-free: where a move takes an item, the array after it, and which row a typed
2
+ // prefix lands on. components/OrderList.vue is the only caller; scripts/order-list.test.mjs tests it.
3
+
4
+ /** The four moves: to the top, one up, one down, to the bottom. */
5
+ export type OrderMove = 'top' | 'up' | 'down' | 'bottom'
6
+
7
+ /** Where `move` takes the item at `index` in a list of `length`, or null when it has nowhere to go (already at that
8
+ * end, or `index` is not a row). */
9
+ export function moveTarget(index: number, length: number, move: OrderMove): number | null {
10
+ if (!Number.isInteger(index) || index < 0 || index >= length) return null
11
+ const to = move === 'top' ? 0 : move === 'up' ? index - 1 : move === 'down' ? index + 1 : length - 1
12
+ return to < 0 || to >= length || to === index ? null : to
13
+ }
14
+
15
+ /** A new array with the item at `from` taken out and put back at `to`; the items themselves are the same objects. */
16
+ export function moveItem<T>(items: readonly T[], from: number, to: number): T[] {
17
+ const next = [...items]
18
+ const [item] = next.splice(from, 1)
19
+ next.splice(to, 0, item as T)
20
+ return next
21
+ }
22
+
23
+ /**
24
+ * The row a typed prefix selects, given the row `start` it is typed on (-1 for none): the first title, searching
25
+ * forward and wrapping, that starts with `query` (case- and accent-insensitive, leading space ignored). One letter
26
+ * searches from the row after `start`, so the same letter typed over and over ("sss") cycles through the rows starting
27
+ * with it, as a native select does; a longer query includes `start`, so the row it is on stays while it still matches.
28
+ * -1 when nothing matches.
29
+ */
30
+ export function typeaheadIndex(titles: readonly string[], query: string, start: number): number {
31
+ const fold = (s: string) => s.normalize('NFD').replace(/\p{M}/gu, '').trimStart().toLocaleLowerCase()
32
+ let q = fold(query)
33
+ if (!q) return -1
34
+ if ([...q].every((c) => c === q[0])) q = q[0]!
35
+ const n = titles.length
36
+ const from = start < 0 || start >= n ? 0 : q.length > 1 ? start : start + 1
37
+ for (let k = 0; k < n; k++) {
38
+ const i = (from + k) % n
39
+ if (fold(titles[i]!).startsWith(q)) return i
40
+ }
41
+ return -1
42
+ }