Repository navigation
perf: sort icon collections once when resolving names #550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+125
−18
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d80de8d
perf: sort icon collections once when resolving names
danielroe 78ca138
fix: re-sort collections updated in place on the client
danielroe 93e421b
fix: update resolved icon names when collections change on the client
danielroe 533260a
fix: track collections through `reactive()` on the client
danielroe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { computed, reactive, toRaw } from 'vue' | ||
| import type { NuxtIconRuntimeOptions } from '../../types' | ||
|
|
||
| const HYPHEN = 45 | ||
|
|
||
| const sortedCollectionsCache = new WeakMap<string[], { readonly value: string[] }>() | ||
|
|
||
| export function getSortedCollections(collections: string[] | null | undefined): string[] { | ||
| if (!collections) | ||
| return [] | ||
| const key = toRaw(collections) | ||
| let sorted = sortedCollectionsCache.get(key) | ||
| if (!sorted) { | ||
| const sort = (source: string[]) => [...source].sort((a, b) => b.length - a.length) | ||
| // App config is cloned per request on the server, but reactive and updated in place on the client (HMR, `updateAppConfig`) | ||
| sorted = import.meta.client ? computed(() => sort(reactive(key))) : { value: sort(key) } | ||
| sortedCollectionsCache.set(key, sorted) | ||
| } | ||
| return sorted.value | ||
| } | ||
|
|
||
| export function resolveName(name: string, options: Pick<NuxtIconRuntimeOptions, 'cssSelectorPrefix' | 'aliases' | 'collections'>): string { | ||
| const bare = name.startsWith(options.cssSelectorPrefix) | ||
| ? name.slice(options.cssSelectorPrefix.length) | ||
| : name | ||
| const resolved = options.aliases?.[bare] || bare | ||
|
|
||
| // Disambiguate collection | ||
| // e.g. `simple-icons-github` -> `simple-icons:github` | ||
| if (!resolved.includes(':')) { | ||
| const collection = getSortedCollections(options.collections) | ||
| .find(c => resolved.charCodeAt(c.length) === HYPHEN && resolved.startsWith(c)) | ||
| return collection | ||
| ? collection + ':' + resolved.slice(collection.length + 1) | ||
| : resolved | ||
| } | ||
| return resolved | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { computed, reactive } from 'vue' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { getSortedCollections, resolveName } from '../src/runtime/components/resolve-name' | ||
|
|
||
| function createOptions(collections: string[] | null, aliases: Record<string, string> = {}) { | ||
| return { cssSelectorPrefix: 'i-', aliases, collections } | ||
| } | ||
|
|
||
| describe('resolveName', () => { | ||
| it('keeps names that already contain a collection separator', () => { | ||
| expect(resolveName('i-simple-icons:github', createOptions(['simple-icons']))).toBe('simple-icons:github') | ||
| }) | ||
|
|
||
| it('resolves a bare name against the longest matching collection', () => { | ||
| const options = createOptions(['simple', 'simple-icons', 'si']) | ||
| expect(resolveName('i-simple-icons-github', options)).toBe('simple-icons:github') | ||
| expect(resolveName('simple-icons-github', options)).toBe('simple-icons:github') | ||
| expect(resolveName('i-simple-foo', options)).toBe('simple:foo') | ||
| expect(resolveName('i-si-foo', options)).toBe('si:foo') | ||
| }) | ||
|
|
||
| it('requires a hyphen after the collection name', () => { | ||
| const options = createOptions(['simple', 'si']) | ||
| expect(resolveName('i-simpleicons', options)).toBe('simpleicons') | ||
| expect(resolveName('i-si', options)).toBe('si') | ||
| }) | ||
|
|
||
| it('resolves aliases before matching collections', () => { | ||
| expect(resolveName('i-github', createOptions(['simple-icons'], { github: 'simple-icons-github' }))).toBe('simple-icons:github') | ||
| }) | ||
|
|
||
| it('keeps bare names without a matching collection', () => { | ||
| expect(resolveName('i-unknown-icon', createOptions(['simple-icons']))).toBe('unknown-icon') | ||
| expect(resolveName('i-unknown-icon', createOptions(null))).toBe('unknown-icon') | ||
| }) | ||
|
|
||
| it('does not reorder the configured collections', () => { | ||
| const collections = ['simple', 'simple-icons', 'si'] | ||
| resolveName('i-simple-icons-github', createOptions(collections)) | ||
| expect(collections).toEqual(['simple', 'simple-icons', 'si']) | ||
| }) | ||
|
|
||
| it('updates computed names when reactive collections change in place', () => { | ||
| const options = reactive(createOptions(['simple', 'foo'])) | ||
| const first = computed(() => resolveName('i-simple-icons-github', options)) | ||
| const second = computed(() => resolveName('i-simple-icons-github', options)) | ||
| expect(first.value).toBe('simple:icons-github') | ||
| expect(second.value).toBe('simple:icons-github') | ||
|
|
||
| options.collections![1] = 'simple-icons' | ||
| expect(first.value).toBe('simple-icons:github') | ||
| expect(second.value).toBe('simple-icons:github') | ||
| }) | ||
|
|
||
| it('updates computed names when collections first resolved as a plain array change', () => { | ||
| const collections = ['simple', 'foo'] | ||
| expect(resolveName('i-simple-icons-github', createOptions(collections))).toBe('simple:icons-github') | ||
|
|
||
| const options = reactive(createOptions(collections)) | ||
| const name = computed(() => resolveName('i-simple-icons-github', options)) | ||
| expect(name.value).toBe('simple:icons-github') | ||
|
|
||
| options.collections![1] = 'simple-icons' | ||
| expect(name.value).toBe('simple-icons:github') | ||
| }) | ||
|
|
||
| it('updates computed names when reactive collections grow', () => { | ||
| const options = reactive(createOptions(['simple'])) | ||
| const name = computed(() => resolveName('i-simple-icons-github', options)) | ||
| expect(name.value).toBe('simple:icons-github') | ||
|
|
||
| options.collections!.push('simple-icons') | ||
| expect(name.value).toBe('simple-icons:github') | ||
| }) | ||
| }) | ||
|
|
||
| describe('getSortedCollections', () => { | ||
| it('sorts each collections array once', () => { | ||
| const collections = ['simple', 'simple-icons'] | ||
| expect(getSortedCollections(collections)).toBe(getSortedCollections(collections)) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.