@kayjs/icons 2.0.0-beta.2 → 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/src/search.ts ADDED
@@ -0,0 +1,250 @@
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 }
6
+
7
+ // An iconify set as `@iconify-json/<prefix>/icons.json` holds it, of which only the names of its icons and aliases are read.
8
+ export interface IconifyNames {
9
+ icons: Record<string, unknown>
10
+ aliases?: Record<string, unknown> | undefined
11
+ }
12
+
13
+ export interface IconPage {
14
+ limit?: number | undefined
15
+ offset?: number | undefined
16
+ }
17
+
18
+ interface Index {
19
+ names: readonly string[]
20
+ byWord: Map<string, string[]>
21
+ // Each two neighbouring words of a name joined, as `log-out` holds `logout`.
22
+ byJoined: Map<string, string[]>
23
+ wordCount: Map<string, number>
24
+ order: Map<string, number>
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>
27
+ }
28
+
29
+ const SPACES = /\s+/g
30
+ const BOUNDARY = /[^a-z0-9]/
31
+ // A word typed as given counts twice what a related word, a completion or a respelling brings.
32
+ const OWN_WEIGHT = 2
33
+ const COMPLETION_WEIGHT = 0.7
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.'
38
+
39
+ // One index per names array or set object, so a set imported once is indexed once.
40
+ const indexes = new WeakMap<readonly string[] | IconifyNames, Index>()
41
+
42
+ function indexOf(source: readonly string[] | IconifyNames): Index {
43
+ const known = indexes.get(source)
44
+
45
+ if (known)
46
+ return known
47
+
48
+ const names = isNames(source) ? source : [...Object.keys(source.icons), ...Object.keys(source.aliases ?? {})]
49
+ const byWord = new Map<string, string[]>()
50
+ const byJoined = new Map<string, string[]>()
51
+ const wordCount = new Map<string, number>()
52
+
53
+ for (const name of names) {
54
+ const ordered = nameWords(name)
55
+ const words = new Set(ordered)
56
+
57
+ wordCount.set(name, nameParts(name))
58
+
59
+ for (const word of words)
60
+ listUnder(byWord, word, name)
61
+
62
+ for (let i = 1; i < ordered.length; i++)
63
+ listUnder(byJoined, ordered[i - 1]! + ordered[i]!, name)
64
+ }
65
+
66
+ const index = { names, byWord, byJoined, wordCount, order: new Map(names.map((name, i) => [name, i])) }
67
+
68
+ indexes.set(source, index)
69
+
70
+ return index
71
+ }
72
+
73
+ function isNames(source: readonly string[] | IconifyNames): source is readonly string[] {
74
+ return Array.isArray(source)
75
+ }
76
+
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> {
104
+ if (index.byWord.has(word) || relatedWords(word).length > 0)
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]))
120
+
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)
125
+
126
+ return found
127
+ }
128
+
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 {
133
+ if (name === literal)
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)
157
+
158
+ return new Set(fewest.filter((name) => {
159
+ const own = new Set(nameWords(name))
160
+
161
+ return words.every(word => own.has(word))
162
+ }))
163
+ }
164
+
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.
166
+ export function findIcons(source: readonly string[] | IconifyNames, query: string, page: IconPage = {}): string[] {
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
+
171
+ const needle = query.trim().toLowerCase()
172
+ const index = indexOf(source)
173
+ const { names } = index
174
+ const literal = needle.replace(SPACES, '-')
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>()
180
+
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
+ }
188
+ }
189
+
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)
232
+
233
+ for (const [related, strength] of relatedWords(reading))
234
+ credit(related, weight * strength, strength >= floor ? bits : 0)
235
+ }
236
+ }
237
+
238
+ const shown = new Set(literals)
239
+ const related = [...scores]
240
+ .filter(([name]) => !shown.has(name))
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))
248
+
249
+ return [...literals, ...related, ...inside].slice(offset, offset + limit)
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
+ }
@@ -0,0 +1,84 @@
1
+ // A word this long or more is completed or respelled; shorter ones match too much.
2
+ const SHORTEST_COMPLETED = 3
3
+ const SHORTEST_RESPELLED = 4
4
+ // From this length a word may be two edits off, else one.
5
+ const TWO_EDITS_FROM = 8
6
+ export const MOST = 3
7
+
8
+ // Every known word starting with `word`, unsorted.
9
+ export function completions(word: string, vocabulary: Iterable<string>): string[] {
10
+ if (word.length < SHORTEST_COMPLETED)
11
+ return []
12
+
13
+ const found: string[] = []
14
+
15
+ for (const candidate of vocabulary) {
16
+ if (candidate.startsWith(word))
17
+ found.push(candidate)
18
+ }
19
+
20
+ return found
21
+ }
22
+
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
+ if (word.length < SHORTEST_RESPELLED)
26
+ return []
27
+
28
+ const most = word.length >= TWO_EDITS_FROM ? 2 : 1
29
+ const found: [string, number][] = []
30
+
31
+ for (const candidate of vocabulary) {
32
+ if (Math.abs(candidate.length - word.length) > most)
33
+ continue
34
+
35
+ const edits = distance(word, candidate, most)
36
+
37
+ if (edits <= most)
38
+ found.push([candidate, edits])
39
+ }
40
+
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)
47
+ }
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
+
52
+ // Damerau-Levenshtein distance (optimal string alignment), giving up past `most` once a whole row is above it.
53
+ export function distance(a: string, b: string, most = Infinity): number {
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
61
+
62
+ for (let i = 1; i <= a.length; i++) {
63
+ row[0] = i
64
+ let lowest = i
65
+
66
+ for (let j = 1; j <= b.length; j++) {
67
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1
68
+ let value = Math.min(previous[j]! + 1, row[j - 1]! + 1, previous[j - 1]! + cost)
69
+
70
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1])
71
+ value = Math.min(value, before[j - 2]! + 1)
72
+
73
+ row[j] = value
74
+ lowest = Math.min(lowest, value)
75
+ }
76
+
77
+ if (lowest > most)
78
+ return lowest
79
+
80
+ ;[before, previous, row] = [previous, row, before]
81
+ }
82
+
83
+ return previous[b.length]!
84
+ }
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 ADDED
@@ -0,0 +1,119 @@
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
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
20
+ const PERCENT = 100
21
+ const BASE = 36
22
+
23
+ function load(): PackedTable {
24
+ // A synchronous lazy load keeps findIcons synchronous.
25
+ table ??= replaced ? packTable(replaced) : require('./words.json') as PackedTable
26
+
27
+ return table
28
+ }
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
+
50
+ // Swaps the table for one built in memory, as the bench scores tables learned without the set they are tested on.
51
+ export function replaceTable(next: Record<string, string>): void {
52
+ replaced = next
53
+ table = undefined
54
+ targets = undefined
55
+ words = undefined
56
+ stems = undefined
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
74
+ }
75
+
76
+ // The words the table knows, each a word a search may be typed with.
77
+ export function tableWords(): string[] {
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()))
98
+ }
99
+
100
+ // The icon-name words related to `word`, each with its strength from 0 to 1, the strongest first.
101
+ export function relatedWords(word: string): [string, number][] {
102
+ const known = decoded.get(word)
103
+
104
+ if (known)
105
+ return known
106
+
107
+ const { lines } = load()
108
+ const line = Object.hasOwn(lines, word) ? lines[word]! : ''
109
+ const parts = line === '' ? [] : line.split(' ')
110
+ const related: [string, number][] = []
111
+
112
+ targets ??= load().targets.split(' ')
113
+ for (let i = 0; i + 1 < parts.length; i += 2)
114
+ related.push([targets[Number.parseInt(parts[i]!, BASE)]!, Number(parts[i + 1]) / PERCENT])
115
+
116
+ decoded.set(word, related)
117
+
118
+ return related
119
+ }
package/src/types.ts CHANGED
@@ -17,10 +17,9 @@ export interface IconifyIcon {
17
17
  vFlip?: boolean | undefined
18
18
  }
19
19
 
20
- export interface IconSet {
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
  }