From a91922ccacc4ef74cac45fab4d7c993b7b7cb9f1 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Fri, 26 Jan 2024 20:41:11 +0200 Subject: [PATCH] chore: extract reusable getPossibleIconNames --- packages/utils/src/loader/external-pkg.ts | 13 ++++++------- packages/utils/src/loader/node-loader.ts | 14 +++++++------- packages/utils/src/loader/utils.ts | 8 ++++++++ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/packages/utils/src/loader/external-pkg.ts b/packages/utils/src/loader/external-pkg.ts index 274700f..0fe1d5f 100644 --- a/packages/utils/src/loader/external-pkg.ts +++ b/packages/utils/src/loader/external-pkg.ts @@ -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; diff --git a/packages/utils/src/loader/node-loader.ts b/packages/utils/src/loader/node-loader.ts index 5ee221a..4fd9d72 100644 --- a/packages/utils/src/loader/node-loader.ts +++ b/packages/utils/src/loader/node-loader.ts @@ -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) { diff --git a/packages/utils/src/loader/utils.ts b/packages/utils/src/loader/utils.ts index a9821de..1f77092 100644 --- a/packages/utils/src/loader/utils.ts +++ b/packages/utils/src/loader/utils.ts @@ -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'), + ]; +}