@kayjs/icons 2.0.0-beta.3 → 2.0.0-beta.4
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/package.json +3 -2
- package/src/Icon.tsx +13 -14
- package/src/diagnose.ts +44 -0
- package/src/find.ts +47 -0
- package/src/icon-data.ts +74 -0
- package/src/index.ts +2 -142
- package/src/name-words.ts +45 -2
- package/src/plugin.ts +166 -0
- package/src/search.ts +168 -39
- package/src/sets.ts +46 -0
- package/src/spelling.ts +25 -13
- package/src/svg-dir.ts +64 -0
- package/src/table.ts +86 -8
- package/src/types.ts +2 -3
- package/src/words.json +1 -1
package/src/search.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { nameWords } from './name-words'
|
|
2
|
-
import {
|
|
3
|
-
import { relatedWords,
|
|
1
|
+
import { byStem, nameParts, nameWords, stem } from './name-words'
|
|
2
|
+
import { completions, MOST, respellings } from './spelling'
|
|
3
|
+
import { relatedWords, tableCompletions, tableForm, tableRespellings } from './table'
|
|
4
|
+
|
|
5
|
+
export { relatedWords }
|
|
4
6
|
|
|
5
7
|
// An iconify set as `@iconify-json/<prefix>/icons.json` holds it, of which only the names of its icons and aliases are read.
|
|
6
8
|
export interface IconifyNames {
|
|
@@ -16,17 +18,23 @@ export interface IconPage {
|
|
|
16
18
|
interface Index {
|
|
17
19
|
names: readonly string[]
|
|
18
20
|
byWord: Map<string, string[]>
|
|
21
|
+
// Each two neighbouring words of a name joined, as `log-out` holds `logout`.
|
|
22
|
+
byJoined: Map<string, string[]>
|
|
19
23
|
wordCount: Map<string, number>
|
|
20
24
|
order: Map<string, number>
|
|
21
|
-
//
|
|
22
|
-
|
|
25
|
+
// Each stem of the names' words to its plain form, made on the first word the names and the table both miss.
|
|
26
|
+
stems?: Map<string, string>
|
|
23
27
|
}
|
|
24
28
|
|
|
25
29
|
const SPACES = /\s+/g
|
|
30
|
+
const BOUNDARY = /[^a-z0-9]/
|
|
26
31
|
// A word typed as given counts twice what a related word, a completion or a respelling brings.
|
|
27
32
|
const OWN_WEIGHT = 2
|
|
28
33
|
const COMPLETION_WEIGHT = 0.7
|
|
29
34
|
const RESPELLING_WEIGHT = 0.8
|
|
35
|
+
// How strongly a joined word must relate to a word for that word to answer both typed words.
|
|
36
|
+
const JOINED_ANSWERS = 0.5
|
|
37
|
+
const PAGE_REFUSAL = '@kayjs/icons: findIcons pages from a whole offset of 0 or more, by a whole limit of 1 or more: clamp the page a request asks for before searching.'
|
|
30
38
|
|
|
31
39
|
// One index per names array or set object, so a set imported once is indexed once.
|
|
32
40
|
const indexes = new WeakMap<readonly string[] | IconifyNames, Index>()
|
|
@@ -39,24 +47,23 @@ function indexOf(source: readonly string[] | IconifyNames): Index {
|
|
|
39
47
|
|
|
40
48
|
const names = isNames(source) ? source : [...Object.keys(source.icons), ...Object.keys(source.aliases ?? {})]
|
|
41
49
|
const byWord = new Map<string, string[]>()
|
|
50
|
+
const byJoined = new Map<string, string[]>()
|
|
42
51
|
const wordCount = new Map<string, number>()
|
|
43
52
|
|
|
44
53
|
for (const name of names) {
|
|
45
|
-
const
|
|
54
|
+
const ordered = nameWords(name)
|
|
55
|
+
const words = new Set(ordered)
|
|
46
56
|
|
|
47
|
-
wordCount.set(name,
|
|
57
|
+
wordCount.set(name, nameParts(name))
|
|
48
58
|
|
|
49
|
-
for (const word of words)
|
|
50
|
-
|
|
59
|
+
for (const word of words)
|
|
60
|
+
listUnder(byWord, word, name)
|
|
51
61
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
else
|
|
55
|
-
byWord.set(word, [name])
|
|
56
|
-
}
|
|
62
|
+
for (let i = 1; i < ordered.length; i++)
|
|
63
|
+
listUnder(byJoined, ordered[i - 1]! + ordered[i]!, name)
|
|
57
64
|
}
|
|
58
65
|
|
|
59
|
-
const index = { names, byWord, wordCount, order: new Map(names.map((name, i) => [name, i]))
|
|
66
|
+
const index = { names, byWord, byJoined, wordCount, order: new Map(names.map((name, i) => [name, i])) }
|
|
60
67
|
|
|
61
68
|
indexes.set(source, index)
|
|
62
69
|
|
|
@@ -67,55 +74,177 @@ function isNames(source: readonly string[] | IconifyNames): source is readonly s
|
|
|
67
74
|
return Array.isArray(source)
|
|
68
75
|
}
|
|
69
76
|
|
|
70
|
-
|
|
71
|
-
|
|
77
|
+
function listUnder(lists: Map<string, string[]>, key: string, name: string): void {
|
|
78
|
+
const list = lists.get(key)
|
|
79
|
+
|
|
80
|
+
if (list)
|
|
81
|
+
list.push(name)
|
|
82
|
+
else
|
|
83
|
+
lists.set(key, [name])
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// The bit of the query word at `at`, words past the 31st sharing the last bit a number holds.
|
|
87
|
+
function bit(at: number): number {
|
|
88
|
+
return 1 << Math.min(at, 30)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function ones(bits: number): number {
|
|
92
|
+
let count = 0
|
|
93
|
+
|
|
94
|
+
for (let rest = bits; rest !== 0; rest &= rest - 1)
|
|
95
|
+
count++
|
|
96
|
+
|
|
97
|
+
return count
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
// What a typed word stands for, with its weight: itself when the names or the table know it, else the names' or the table's form of its stem, the three most used of its completions and the three closest of its respellings, the most used first among equals.
|
|
102
|
+
// The table's side of a typed word is read once for every set; a word reached both ways keeps the larger weight.
|
|
103
|
+
function readings(word: string, index: Index): Map<string, number> {
|
|
72
104
|
if (index.byWord.has(word) || relatedWords(word).length > 0)
|
|
73
|
-
return [[word, 1]]
|
|
105
|
+
return new Map([[word, 1]])
|
|
106
|
+
|
|
107
|
+
index.stems ??= byStem(index.byWord.keys())
|
|
108
|
+
const form = index.stems.get(stem(word)) ?? tableForm(word)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
const uses = (candidate: string) => index.byWord.get(candidate)?.length ?? 0
|
|
112
|
+
// Ranking the table-only completions by the names their related words reach was benched and lost half a point of prefix recall to the shortest first.
|
|
113
|
+
const completed = [...new Set([...completions(word, index.byWord.keys()), ...tableCompletions(word)])]
|
|
114
|
+
.sort((a, b) => uses(b) - uses(a) || a.length - b.length)
|
|
115
|
+
.slice(0, MOST)
|
|
116
|
+
const respelled = [...new Map([...tableRespellings(word), ...respellings(word, index.byWord.keys())])]
|
|
117
|
+
.sort((a, b) => a[1] - b[1] || uses(b[0]) - uses(a[0]) || (a[0] < b[0] ? -1 : 1))
|
|
118
|
+
.slice(0, MOST)
|
|
119
|
+
const found = new Map(respelled.map(([candidate]) => [candidate, RESPELLING_WEIGHT]))
|
|
74
120
|
|
|
75
|
-
const
|
|
121
|
+
for (const candidate of completed)
|
|
122
|
+
found.set(candidate, Math.max(found.get(candidate) ?? 0, COMPLETION_WEIGHT))
|
|
123
|
+
if (form !== undefined)
|
|
124
|
+
found.set(form, 1)
|
|
76
125
|
|
|
77
|
-
return
|
|
78
|
-
...completions(word, index.vocabulary, known).map(found => [found, COMPLETION_WEIGHT] as [string, number]),
|
|
79
|
-
...closeWords(word, index.vocabulary).map(found => [found, RESPELLING_WEIGHT] as [string, number]),
|
|
80
|
-
]
|
|
126
|
+
return found
|
|
81
127
|
}
|
|
82
128
|
|
|
83
|
-
|
|
129
|
+
// How a name holds the query as typed: the name itself, its first words, whole words further in, every query word in any order, the start of a word, or inside a word.
|
|
130
|
+
const enum Held { Name, FirstWords, Words, EveryWord, WordStart, InsideWord }
|
|
131
|
+
|
|
132
|
+
function held(name: string, literal: string): Held | undefined {
|
|
84
133
|
if (name === literal)
|
|
85
|
-
return
|
|
134
|
+
return Held.Name
|
|
135
|
+
|
|
136
|
+
let best: Held | undefined
|
|
137
|
+
|
|
138
|
+
for (let at = name.indexOf(literal); at !== -1 && at < name.length; at = name.indexOf(literal, at + 1)) {
|
|
139
|
+
const end = at + literal.length
|
|
140
|
+
const startsWord = at === 0 || BOUNDARY.test(name[at - 1]!)
|
|
141
|
+
const endsWord = end === name.length || BOUNDARY.test(name[end]!)
|
|
142
|
+
const rank = !startsWord ? Held.InsideWord : !endsWord ? Held.WordStart : at === 0 ? Held.FirstWords : Held.Words
|
|
143
|
+
|
|
144
|
+
best = best === undefined ? rank : Math.min(best, rank)
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return best
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// The names holding each of several words whole, in any order.
|
|
151
|
+
function holdingEvery(words: string[], index: Index): Set<string> {
|
|
152
|
+
if (words.length < 2)
|
|
153
|
+
return new Set()
|
|
154
|
+
|
|
155
|
+
// The names holding the word fewest names hold, kept when their own words hold the others too.
|
|
156
|
+
const fewest = words.map(word => index.byWord.get(word) ?? []).reduce((a, b) => b.length < a.length ? b : a)
|
|
86
157
|
|
|
87
|
-
return
|
|
158
|
+
return new Set(fewest.filter((name) => {
|
|
159
|
+
const own = new Set(nameWords(name))
|
|
160
|
+
|
|
161
|
+
return words.every(word => own.has(word))
|
|
162
|
+
}))
|
|
88
163
|
}
|
|
89
164
|
|
|
90
|
-
// A page of the icon names of a set, or of a list of names, matching `query`: the names holding it first (the exact name,
|
|
165
|
+
// A page of the icon names of a set, or of a list of names, matching `query`: the names holding it as whole words first (the exact name, names starting with it, names holding it further in, names holding each of its words in any order), then names starting a word with it, then the icons a related word, a completion or a respelling names, those answering more of the query's words first, then names holding it inside a word.
|
|
91
166
|
export function findIcons(source: readonly string[] | IconifyNames, query: string, page: IconPage = {}): string[] {
|
|
92
167
|
const { limit = Infinity, offset = 0 } = page
|
|
168
|
+
if (!Number.isInteger(offset) || offset < 0 || !(limit === Infinity || (Number.isInteger(limit) && limit >= 1)))
|
|
169
|
+
throw new RangeError(PAGE_REFUSAL)
|
|
170
|
+
|
|
93
171
|
const needle = query.trim().toLowerCase()
|
|
94
172
|
const index = indexOf(source)
|
|
95
173
|
const { names } = index
|
|
96
174
|
const literal = needle.replace(SPACES, '-')
|
|
97
|
-
const
|
|
98
|
-
const
|
|
175
|
+
const typedWords = [...new Set(nameWords(needle))]
|
|
176
|
+
const every = holdingEvery(typedWords, index)
|
|
177
|
+
// The names holding the query, one list per way of holding it, each in the set's order.
|
|
178
|
+
const byHeld: string[][] = [[], [], [], [], [], []]
|
|
179
|
+
const rankOf = new Map<string, Held>()
|
|
99
180
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
181
|
+
for (const name of names) {
|
|
182
|
+
const rank = name.includes(literal) ? held(name, literal) : undefined
|
|
183
|
+
|
|
184
|
+
if (rank !== undefined) {
|
|
185
|
+
byHeld[rank]!.push(name)
|
|
186
|
+
rankOf.set(name, rank)
|
|
187
|
+
}
|
|
103
188
|
}
|
|
104
189
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
190
|
+
// A name holding every query word moves up to that rank unless it holds the query better already.
|
|
191
|
+
const moved = new Set([...every].filter(name => (rankOf.get(name) ?? Held.InsideWord + 1) > Held.EveryWord))
|
|
192
|
+
|
|
193
|
+
if (moved.size > 0) {
|
|
194
|
+
byHeld[Held.EveryWord] = [...moved]
|
|
195
|
+
byHeld[Held.WordStart] = byHeld[Held.WordStart]!.filter(name => !moved.has(name))
|
|
196
|
+
byHeld[Held.InsideWord] = byHeld[Held.InsideWord]!.filter(name => !moved.has(name))
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const literals = byHeld.slice(0, Held.InsideWord).flat()
|
|
200
|
+
// Each credited name's score, and the query words it answers as one bit each, so a word counts once however many readings reach it.
|
|
201
|
+
const scores = new Map<string, { score: number, answered: number }>()
|
|
202
|
+
|
|
203
|
+
// Weighting each word by how rare it is among the names was benched and lost the multi-word queries, as `off` is common yet is what `hide` means.
|
|
204
|
+
const creditNames = (holding: string[] | undefined, weight: number, answering: number) => {
|
|
205
|
+
for (const name of holding ?? []) {
|
|
206
|
+
const gained = weight / index.wordCount.get(name)!
|
|
207
|
+
const known = scores.get(name)
|
|
208
|
+
|
|
209
|
+
if (known === undefined) {
|
|
210
|
+
scores.set(name, { score: gained, answered: answering })
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
known.score += gained
|
|
214
|
+
known.answered |= answering
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const credit = (word: string, weight: number, answering: number) => {
|
|
219
|
+
creditNames(index.byWord.get(word), weight, answering)
|
|
220
|
+
creditNames(index.byJoined.get(word), weight, answering)
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Two neighbouring query words also stand for the word they make joined when it is known, as `sign out` for `signout`: it answers both through the names holding it and its strong relations, as `checkmark` means `check`, while a weak one, as `adduser` to `user`, only adds to the score.
|
|
224
|
+
const apart = typedWords.map((typed, i) => ({ typed, bits: bit(i), floor: 0 }))
|
|
225
|
+
const joined = typedWords.slice(1)
|
|
226
|
+
.map((word, i) => ({ typed: typedWords[i]! + word, bits: bit(i) | bit(i + 1), floor: JOINED_ANSWERS }))
|
|
227
|
+
.filter(({ typed }) => index.byWord.has(typed) || relatedWords(typed).length > 0)
|
|
228
|
+
|
|
229
|
+
for (const { typed, bits, floor } of [...apart, ...joined]) {
|
|
230
|
+
for (const [reading, weight] of readings(typed, index)) {
|
|
231
|
+
credit(reading, weight * OWN_WEIGHT, bits)
|
|
108
232
|
|
|
109
233
|
for (const [related, strength] of relatedWords(reading))
|
|
110
|
-
credit(related, weight * strength)
|
|
234
|
+
credit(related, weight * strength, strength >= floor ? bits : 0)
|
|
111
235
|
}
|
|
112
236
|
}
|
|
113
237
|
|
|
114
238
|
const shown = new Set(literals)
|
|
115
239
|
const related = [...scores]
|
|
116
240
|
.filter(([name]) => !shown.has(name))
|
|
117
|
-
.
|
|
118
|
-
.
|
|
241
|
+
.map(([name, { score, answered }]) => ({ name, score, answered: ones(answered) }))
|
|
242
|
+
.sort((a, b) => b.answered - a.answered || b.score - a.score || index.order.get(a.name)! - index.order.get(b.name)!)
|
|
243
|
+
.map(({ name }) => name)
|
|
244
|
+
|
|
245
|
+
// A name holding the query only inside a word, as `spinner` holds `pin`, comes last.
|
|
246
|
+
const shownToo = new Set(related)
|
|
247
|
+
const inside = byHeld[Held.InsideWord]!.filter(name => !shownToo.has(name))
|
|
119
248
|
|
|
120
|
-
return [...literals, ...related].slice(offset, offset + limit)
|
|
249
|
+
return [...literals, ...related, ...inside].slice(offset, offset + limit)
|
|
121
250
|
}
|
package/src/sets.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Where an app's Iconify sets are installed: `@iconify-json/<prefix>` packages or the all-in-one `@iconify/json`, in the app's node_modules or a parent's (a workspace hoists them), the nearest first.
|
|
2
|
+
|
|
3
|
+
import { existsSync } from 'node:fs'
|
|
4
|
+
import { basename, dirname, join } from 'node:path'
|
|
5
|
+
|
|
6
|
+
const PREFIX_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/
|
|
7
|
+
|
|
8
|
+
// Looked up on disk at each call rather than through Bun's resolver, which remembers a package missing at the first lookup and would never see a set installed while `kay dev` runs.
|
|
9
|
+
function nodeModulesDirs(root: string): string[] {
|
|
10
|
+
const dirs: string[] = []
|
|
11
|
+
for (let dir = root; ; dir = dirname(dir)) {
|
|
12
|
+
if (existsSync(join(dir, 'node_modules')))
|
|
13
|
+
dirs.push(join(dir, 'node_modules'))
|
|
14
|
+
if (dirname(dir) === dir)
|
|
15
|
+
return dirs
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function isSetPrefix(prefix: string): boolean {
|
|
20
|
+
return PREFIX_RE.test(prefix)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The JSON file of the set `prefix` installed for the app at `root`. */
|
|
24
|
+
export function setFile(prefix: string, root: string): string | undefined {
|
|
25
|
+
if (!isSetPrefix(prefix))
|
|
26
|
+
return undefined
|
|
27
|
+
for (const dir of nodeModulesDirs(root)) {
|
|
28
|
+
for (const file of [join(dir, '@iconify-json', prefix, 'icons.json'), join(dir, '@iconify/json/json', `${prefix}.json`)]) {
|
|
29
|
+
if (existsSync(file))
|
|
30
|
+
return file
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return undefined
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The prefix of every set installed for the app at `root`. */
|
|
37
|
+
export function installedPrefixes(root: string): string[] {
|
|
38
|
+
const found = new Set<string>()
|
|
39
|
+
for (const dir of nodeModulesDirs(root)) {
|
|
40
|
+
for (const file of new Bun.Glob('@iconify-json/*/icons.json').scanSync({ cwd: dir, followSymlinks: true }))
|
|
41
|
+
found.add(basename(dirname(file)))
|
|
42
|
+
for (const file of new Bun.Glob('@iconify/json/json/*.json').scanSync({ cwd: dir, followSymlinks: true }))
|
|
43
|
+
found.add(basename(file, '.json'))
|
|
44
|
+
}
|
|
45
|
+
return [...found].filter(isSetPrefix)
|
|
46
|
+
}
|
package/src/spelling.ts
CHANGED
|
@@ -3,10 +3,10 @@ const SHORTEST_COMPLETED = 3
|
|
|
3
3
|
const SHORTEST_RESPELLED = 4
|
|
4
4
|
// From this length a word may be two edits off, else one.
|
|
5
5
|
const TWO_EDITS_FROM = 8
|
|
6
|
-
const MOST = 3
|
|
6
|
+
export const MOST = 3
|
|
7
7
|
|
|
8
|
-
//
|
|
9
|
-
export function completions(word: string, vocabulary: Iterable<string
|
|
8
|
+
// Every known word starting with `word`, unsorted.
|
|
9
|
+
export function completions(word: string, vocabulary: Iterable<string>): string[] {
|
|
10
10
|
if (word.length < SHORTEST_COMPLETED)
|
|
11
11
|
return []
|
|
12
12
|
|
|
@@ -17,11 +17,11 @@ export function completions(word: string, vocabulary: Iterable<string>, uses: (w
|
|
|
17
17
|
found.push(candidate)
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
return found
|
|
20
|
+
return found
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
//
|
|
24
|
-
export function
|
|
23
|
+
// Every known word one edit from `word` (two for a long one), a swap of neighbour letters counting as one, with its edits, unsorted.
|
|
24
|
+
export function respellings(word: string, vocabulary: Iterable<string>): [string, number][] {
|
|
25
25
|
if (word.length < SHORTEST_RESPELLED)
|
|
26
26
|
return []
|
|
27
27
|
|
|
@@ -38,16 +38,29 @@ export function closeWords(word: string, vocabulary: Iterable<string>): string[]
|
|
|
38
38
|
found.push([candidate, edits])
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
-
return found
|
|
41
|
+
return found
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// The known words closest to `word`, as `respellings` finds them, the closest first.
|
|
45
|
+
export function closeWords(word: string, vocabulary: Iterable<string>): string[] {
|
|
46
|
+
return respellings(word, vocabulary).sort((a, b) => a[1] - b[1]).slice(0, MOST).map(([candidate]) => candidate)
|
|
42
47
|
}
|
|
43
48
|
|
|
49
|
+
// Three rows reused by every distance, as one is measured against each of some 29,000 words per typed word.
|
|
50
|
+
let rows = [new Int32Array(64), new Int32Array(64), new Int32Array(64)]
|
|
51
|
+
|
|
44
52
|
// Damerau-Levenshtein distance (optimal string alignment), giving up past `most` once a whole row is above it.
|
|
45
53
|
export function distance(a: string, b: string, most = Infinity): number {
|
|
46
|
-
|
|
47
|
-
|
|
54
|
+
if (rows[0]!.length <= b.length)
|
|
55
|
+
rows = [new Int32Array(b.length * 2), new Int32Array(b.length * 2), new Int32Array(b.length * 2)]
|
|
56
|
+
|
|
57
|
+
let [before, previous, row] = rows as [Int32Array, Int32Array, Int32Array]
|
|
58
|
+
|
|
59
|
+
for (let j = 0; j <= b.length; j++)
|
|
60
|
+
previous[j] = j
|
|
48
61
|
|
|
49
62
|
for (let i = 1; i <= a.length; i++) {
|
|
50
|
-
|
|
63
|
+
row[0] = i
|
|
51
64
|
let lowest = i
|
|
52
65
|
|
|
53
66
|
for (let j = 1; j <= b.length; j++) {
|
|
@@ -57,15 +70,14 @@ export function distance(a: string, b: string, most = Infinity): number {
|
|
|
57
70
|
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1])
|
|
58
71
|
value = Math.min(value, before[j - 2]! + 1)
|
|
59
72
|
|
|
60
|
-
row
|
|
73
|
+
row[j] = value
|
|
61
74
|
lowest = Math.min(lowest, value)
|
|
62
75
|
}
|
|
63
76
|
|
|
64
77
|
if (lowest > most)
|
|
65
78
|
return lowest
|
|
66
79
|
|
|
67
|
-
before = previous
|
|
68
|
-
previous = row
|
|
80
|
+
;[before, previous, row] = [previous, row, before]
|
|
69
81
|
}
|
|
70
82
|
|
|
71
83
|
return previous[b.length]!
|
package/src/svg-dir.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// A folder of `.svg` files read as an icon set, each file an icon named after it, the way Iconify's importDirectory does.
|
|
2
|
+
|
|
3
|
+
import type { IconifyIcon, IconSet } from './types'
|
|
4
|
+
import { existsSync } from 'node:fs'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
|
|
7
|
+
const SVG_RE = /<svg\b([^>]*)>([\s\S]*)<\/svg\s*>/i
|
|
8
|
+
const ATTR_RE = /([\w:-]+)\s*=\s*(?:"([^"]*)"|'([^']*)')/g
|
|
9
|
+
const COMMENT_RE = /<!--[\s\S]*?-->/g
|
|
10
|
+
const NUMBER_RE = /^\s*(-?[\d.]+)(?:px)?\s*$/
|
|
11
|
+
const BOX_SEPARATOR_RE = /[\s,]+/
|
|
12
|
+
// Root attributes that paint the glyph, kept on a group around it when the file drops its <svg> element.
|
|
13
|
+
const PAINT_ATTR_RE = /^(?:fill|stroke|color|opacity|clip-rule|fill-.+|stroke-.+)$/
|
|
14
|
+
|
|
15
|
+
export interface SvgDirSet {
|
|
16
|
+
set: IconSet
|
|
17
|
+
/** Why a file is not an icon, keyed by the icon name it would have. */
|
|
18
|
+
problems: Map<string, string>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function attributes(source: string): Map<string, string> {
|
|
22
|
+
const found = new Map<string, string>()
|
|
23
|
+
for (const [, name, double, single] of source.matchAll(ATTR_RE))
|
|
24
|
+
found.set(name!, double ?? single ?? '')
|
|
25
|
+
return found
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function number(value: string | undefined): number | undefined {
|
|
29
|
+
const match = value === undefined ? null : NUMBER_RE.exec(value)
|
|
30
|
+
return match === null ? undefined : Number(match[1])
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The icon drawn by an SVG file's source, or why it cannot be one. */
|
|
34
|
+
export function svgIcon(source: string): IconifyIcon | string {
|
|
35
|
+
const svg = SVG_RE.exec(source.replace(COMMENT_RE, ''))
|
|
36
|
+
if (svg === null)
|
|
37
|
+
return 'holds no <svg> element'
|
|
38
|
+
const attrs = attributes(svg[1]!)
|
|
39
|
+
const box = attrs.get('viewBox')?.trim().split(BOX_SEPARATOR_RE).map(Number)
|
|
40
|
+
const [left, top, width, height] = box?.length === 4 && box.every(Number.isFinite) ? box : [0, 0, number(attrs.get('width')), number(attrs.get('height'))]
|
|
41
|
+
if (width === undefined || height === undefined || width <= 0 || height <= 0)
|
|
42
|
+
return 'has no viewBox, nor a width and height, to size it: add viewBox="0 0 <width> <height>" to its <svg>'
|
|
43
|
+
|
|
44
|
+
const paint = [...attrs].filter(([name]) => PAINT_ATTR_RE.test(name)).map(([name, value]) => ` ${name}="${value}"`).join('')
|
|
45
|
+
const body = svg[2]!.trim()
|
|
46
|
+
return { body: paint === '' ? body : `<g${paint}>${body}</g>`, left: left!, top: top!, width, height }
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** The icons of the `.svg` files directly in `dir`, undefined when the folder does not exist. */
|
|
50
|
+
export async function readSvgDir(prefix: string, dir: string): Promise<SvgDirSet | undefined> {
|
|
51
|
+
if (!existsSync(dir))
|
|
52
|
+
return undefined
|
|
53
|
+
const set: IconSet = { prefix, icons: {} }
|
|
54
|
+
const problems = new Map<string, string>()
|
|
55
|
+
for (const file of new Bun.Glob('*.svg').scanSync({ cwd: dir })) {
|
|
56
|
+
const name = file.slice(0, -'.svg'.length)
|
|
57
|
+
const icon = svgIcon(await Bun.file(join(dir, file)).text())
|
|
58
|
+
if (typeof icon === 'string')
|
|
59
|
+
problems.set(name, icon)
|
|
60
|
+
else
|
|
61
|
+
set.icons[name] = icon
|
|
62
|
+
}
|
|
63
|
+
return { set, problems }
|
|
64
|
+
}
|
package/src/table.ts
CHANGED
|
@@ -1,24 +1,100 @@
|
|
|
1
|
-
// words.json
|
|
2
|
-
|
|
1
|
+
// words.json relates a word to icon-name words, built by build/table.ts and read on the first search: `lines` maps a word to `index percent index percent`, each index in base 36 into the words of `targets`.
|
|
2
|
+
import { byStem, stem } from './name-words'
|
|
3
|
+
import { completions, respellings } from './spelling'
|
|
4
|
+
|
|
5
|
+
export interface PackedTable {
|
|
6
|
+
targets: string
|
|
7
|
+
lines: Record<string, string>
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let table: PackedTable | undefined
|
|
11
|
+
let replaced: Record<string, string> | undefined
|
|
12
|
+
let targets: string[] | undefined
|
|
13
|
+
let words: string[] | undefined
|
|
14
|
+
let stems: Map<string, string> | undefined
|
|
3
15
|
const decoded = new Map<string, [string, number][]>()
|
|
16
|
+
// The readings of typed words, shared by every set searched; typed words are unbounded, so only the latest are kept.
|
|
17
|
+
const completed = new Map<string, string[]>()
|
|
18
|
+
const respelled = new Map<string, [string, number][]>()
|
|
19
|
+
const REMEMBERED = 1000
|
|
4
20
|
const PERCENT = 100
|
|
21
|
+
const BASE = 36
|
|
5
22
|
|
|
6
|
-
function load():
|
|
23
|
+
function load(): PackedTable {
|
|
7
24
|
// A synchronous lazy load keeps findIcons synchronous.
|
|
8
|
-
table ??= require('./words.json') as
|
|
25
|
+
table ??= replaced ? packTable(replaced) : require('./words.json') as PackedTable
|
|
9
26
|
|
|
10
27
|
return table
|
|
11
28
|
}
|
|
12
29
|
|
|
30
|
+
// Packs `word: 'related percent related percent'` lines into the shape words.json holds.
|
|
31
|
+
export function packTable(plain: Record<string, string>): PackedTable {
|
|
32
|
+
const all: string[] = []
|
|
33
|
+
const at = new Map<string, number>()
|
|
34
|
+
const lines: Record<string, string> = {}
|
|
35
|
+
|
|
36
|
+
for (const [word, line] of Object.entries(plain)) {
|
|
37
|
+
const parts = line === '' ? [] : line.split(' ')
|
|
38
|
+
|
|
39
|
+
for (let i = 0; i < parts.length; i += 2) {
|
|
40
|
+
if (!at.has(parts[i]!))
|
|
41
|
+
at.set(parts[i]!, all.push(parts[i]!) - 1)
|
|
42
|
+
parts[i] = at.get(parts[i]!)!.toString(BASE)
|
|
43
|
+
}
|
|
44
|
+
lines[word] = parts.join(' ')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return { targets: all.join(' '), lines }
|
|
48
|
+
}
|
|
49
|
+
|
|
13
50
|
// Swaps the table for one built in memory, as the bench scores tables learned without the set they are tested on.
|
|
14
51
|
export function replaceTable(next: Record<string, string>): void {
|
|
15
|
-
|
|
52
|
+
replaced = next
|
|
53
|
+
table = undefined
|
|
54
|
+
targets = undefined
|
|
55
|
+
words = undefined
|
|
56
|
+
stems = undefined
|
|
16
57
|
decoded.clear()
|
|
58
|
+
completed.clear()
|
|
59
|
+
respelled.clear()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function remember<T>(cache: Map<string, T>, word: string, read: () => T): T {
|
|
63
|
+
let found = cache.get(word)
|
|
64
|
+
|
|
65
|
+
if (found === undefined) {
|
|
66
|
+
found = read()
|
|
67
|
+
|
|
68
|
+
if (cache.size >= REMEMBERED)
|
|
69
|
+
cache.delete(cache.keys().next().value!)
|
|
70
|
+
cache.set(word, found)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return found
|
|
17
74
|
}
|
|
18
75
|
|
|
19
76
|
// The words the table knows, each a word a search may be typed with.
|
|
20
77
|
export function tableWords(): string[] {
|
|
21
|
-
|
|
78
|
+
words ??= Object.keys(load().lines)
|
|
79
|
+
|
|
80
|
+
return words
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// The table's form of `word` when the table knows another, as `search` for `searching`.
|
|
84
|
+
export function tableForm(word: string): string | undefined {
|
|
85
|
+
stems ??= byStem(tableWords())
|
|
86
|
+
|
|
87
|
+
return stems.get(stem(word))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// The table words starting with `word`, unsorted.
|
|
91
|
+
export function tableCompletions(word: string): string[] {
|
|
92
|
+
return remember(completed, word, () => completions(word, tableWords()))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The table words close to `word`, with their edits, unsorted.
|
|
96
|
+
export function tableRespellings(word: string): [string, number][] {
|
|
97
|
+
return remember(respelled, word, () => respellings(word, tableWords()))
|
|
22
98
|
}
|
|
23
99
|
|
|
24
100
|
// The icon-name words related to `word`, each with its strength from 0 to 1, the strongest first.
|
|
@@ -28,12 +104,14 @@ export function relatedWords(word: string): [string, number][] {
|
|
|
28
104
|
if (known)
|
|
29
105
|
return known
|
|
30
106
|
|
|
31
|
-
const
|
|
107
|
+
const { lines } = load()
|
|
108
|
+
const line = Object.hasOwn(lines, word) ? lines[word]! : ''
|
|
32
109
|
const parts = line === '' ? [] : line.split(' ')
|
|
33
110
|
const related: [string, number][] = []
|
|
34
111
|
|
|
112
|
+
targets ??= load().targets.split(' ')
|
|
35
113
|
for (let i = 0; i + 1 < parts.length; i += 2)
|
|
36
|
-
related.push([parts[i]!, Number(parts[i + 1]) / PERCENT])
|
|
114
|
+
related.push([targets[Number.parseInt(parts[i]!, BASE)]!, Number(parts[i + 1]) / PERCENT])
|
|
37
115
|
|
|
38
116
|
decoded.set(word, related)
|
|
39
117
|
|
package/src/types.ts
CHANGED
|
@@ -17,10 +17,9 @@ export interface IconifyIcon {
|
|
|
17
17
|
vFlip?: boolean | undefined
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
// An Iconify JSON set: its root may carry defaults for the size, offset and transforms of every icon.
|
|
21
|
+
export interface IconSet extends Omit<IconifyIcon, 'body' | 'parent'> {
|
|
21
22
|
prefix: string
|
|
22
23
|
icons: Record<string, IconifyIcon>
|
|
23
24
|
aliases?: Record<string, IconifyIcon> | undefined
|
|
24
|
-
width?: number | undefined
|
|
25
|
-
height?: number | undefined
|
|
26
25
|
}
|