2
0
mirror of https://github.com/iconify/iconify.git synced 2024-09-20 01:09:04 +00:00

chore: extract reusable getPossibleIconNames

This commit is contained in:
Vjacheslav Trushkin 2024-01-26 20:41:11 +02:00
parent 220bb35428
commit a91922ccac
3 changed files with 21 additions and 14 deletions

View File

@ -2,6 +2,7 @@ import { AutoInstall, CustomIconLoader, ExternalPkgName } from './types';
import { loadCollectionFromFS } from './fs'; import { loadCollectionFromFS } from './fs';
import { searchForIcon } from './modern'; import { searchForIcon } from './modern';
import { warnOnce } from './warn'; import { warnOnce } from './warn';
import { getPossibleIconNames } from './utils';
/** /**
* Creates a CustomIconLoader collection from an external package collection. * Creates a CustomIconLoader collection from an external package collection.
@ -57,13 +58,11 @@ function createCustomIconLoader(
// copy/paste from ./node-loader.ts // copy/paste from ./node-loader.ts
let result: string | undefined; let result: string | undefined;
if (iconSet) { if (iconSet) {
// possible icon names result = await searchForIcon(
const ids = [ iconSet,
icon, collection,
icon.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(), getPossibleIconNames(icon)
icon.replace(/([a-z])(\d+)/g, '$1-$2'), );
];
result = await searchForIcon(iconSet, collection, ids);
} }
return result; return result;

View File

@ -3,6 +3,7 @@ import { searchForIcon } from './modern';
import { loadCollectionFromFS } from './fs'; import { loadCollectionFromFS } from './fs';
import { warnOnce } from './warn'; import { warnOnce } from './warn';
import { loadIcon } from './loader'; import { loadIcon } from './loader';
import { getPossibleIconNames } from './utils';
export const loadNodeIcon: UniversalIconLoader = async ( export const loadNodeIcon: UniversalIconLoader = async (
collection, collection,
@ -19,13 +20,12 @@ export const loadNodeIcon: UniversalIconLoader = async (
options?.autoInstall options?.autoInstall
); );
if (iconSet) { if (iconSet) {
// possible icon names result = await searchForIcon(
const ids = [ iconSet,
icon, collection,
icon.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(), getPossibleIconNames(icon),
icon.replace(/([a-z])(\d+)/g, '$1-$2'), options
]; );
result = await searchForIcon(iconSet, collection, ids, options);
} }
if (!result && options?.warn) { if (!result && options?.warn) {

View File

@ -114,3 +114,11 @@ export async function mergeIconProps(
return svg; return svg;
} }
export function getPossibleIconNames(icon: string): string[] {
return [
icon,
icon.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase(),
icon.replace(/([a-z])(\d+)/g, '$1-$2'),
];
}