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

View File

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

View File

@ -114,3 +114,11 @@ export async function mergeIconProps(
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'),
];
}