11import { computed , reactive , toRaw } from 'vue'
22import type { NuxtIconRuntimeOptions } from '../../types'
33
4- const HYPHEN = 45
4+ const collectionSetCache = new WeakMap < string [ ] , { readonly value : Set < string > } > ( )
55
6- const sortedCollectionsCache = new WeakMap < string [ ] , { readonly value : string [ ] } > ( )
7-
8- export function getSortedCollections ( collections : string [ ] | null | undefined ) : string [ ] {
6+ export function getCollectionSet ( collections : string [ ] | null | undefined ) : Set < string > | undefined {
97 if ( ! collections )
10- return [ ]
8+ return
119 const key = toRaw ( collections )
12- let sorted = sortedCollectionsCache . get ( key )
13- if ( ! sorted ) {
14- const sort = ( source : string [ ] ) => [ ...source ] . sort ( ( a , b ) => b . length - a . length )
10+ let set = collectionSetCache . get ( key )
11+ if ( ! set ) {
1512 // App config is cloned per request on the server, but reactive and updated in place on the client (HMR, `updateAppConfig`)
16- sorted = import . meta. client ? computed ( ( ) => sort ( reactive ( key ) ) ) : { value : sort ( key ) }
17- sortedCollectionsCache . set ( key , sorted )
13+ set = import . meta. client ? computed ( ( ) => new Set ( reactive ( key ) ) ) : { value : new Set ( key ) }
14+ collectionSetCache . set ( key , set )
15+ }
16+ return set . value
17+ }
18+
19+ function findCollection ( name : string , collections : Set < string > ) : string | undefined {
20+ let index = name . lastIndexOf ( '-' )
21+ while ( index > 0 ) {
22+ const collection = name . slice ( 0 , index )
23+ if ( collections . has ( collection ) )
24+ return collection
25+ index = name . lastIndexOf ( '-' , index - 1 )
1826 }
19- return sorted . value
2027}
2128
2229export function resolveName ( name : string , options : Pick < NuxtIconRuntimeOptions , 'cssSelectorPrefix' | 'aliases' | 'collections' > ) : string {
@@ -28,8 +35,8 @@ export function resolveName(name: string, options: Pick<NuxtIconRuntimeOptions,
2835 // Disambiguate collection
2936 // e.g. `simple-icons-github` -> `simple-icons:github`
3037 if ( ! resolved . includes ( ':' ) ) {
31- const collection = getSortedCollections ( options . collections )
32- . find ( c => resolved . charCodeAt ( c . length ) === HYPHEN && resolved . startsWith ( c ) )
38+ const collections = getCollectionSet ( options . collections )
39+ const collection = collections && findCollection ( resolved , collections )
3340 return collection
3441 ? collection + ':' + resolved . slice ( collection . length + 1 )
3542 : resolved
0 commit comments