diff --git a/packages/utils/package.json b/packages/utils/package.json index ae6e620..60bbc04 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -128,10 +128,10 @@ "require": "./lib/loader/loaders.js", "import": "./lib/loader/loaders.mjs" }, - "./lib/loader/modern": { - "require": "./lib/loader/modern.js", - "import": "./lib/loader/modern.mjs" - }, + "./lib/loader/modern": { + "require": "./lib/loader/modern.js", + "import": "./lib/loader/modern.mjs" + }, "./lib/loader/types": { "require": "./lib/loader/types.js", "import": "./lib/loader/types.mjs" @@ -163,7 +163,7 @@ "@iconify/types": "^1.0.12", "debug": "^4.3.3", "kolorist": "^1.5.0", - "local-pkg": "^0.4.0" + "local-pkg": "^0.4.0" }, "devDependencies": { "@iconify/library-builder": "^1.0.4", diff --git a/packages/utils/src/loader/custom.ts b/packages/utils/src/loader/custom.ts index a02d512..84e3ed4 100644 --- a/packages/utils/src/loader/custom.ts +++ b/packages/utils/src/loader/custom.ts @@ -1,5 +1,9 @@ import createDebugger from 'debug'; -import type { CustomIconLoader, IconCustomizations, InlineCollection } from './types'; +import type { + CustomIconLoader, + IconCustomizations, + InlineCollection, +} from './types'; import { mergeIconProps } from './utils'; const debug = createDebugger('@iconify-loader:custom'); @@ -11,7 +15,7 @@ export async function getCustomIcon( custom: CustomIconLoader | InlineCollection, collection: string, icon: string, - iconsCustomizations?: IconCustomizations, + iconsCustomizations?: IconCustomizations ): Promise { let result: string | undefined | null; @@ -26,10 +30,16 @@ export async function getCustomIcon( if (result) { if (!result.startsWith('> = {}; const isLegacyExists = isPackageExists('@iconify/json'); -export async function loadCollection(name: string, autoInstall = false): Promise { +export async function loadCollection( + name: string, + autoInstall = false +): Promise { if (!_collections[name]) { _collections[name] = task(); } @@ -43,8 +46,7 @@ export async function loadCollection(name: string, autoInstall = false): Promise if (jsonPath) { return JSON.parse(await fs.readFile(jsonPath, 'utf8')); - } - else { + } else { debugModern(`failed to load ${name}`); return undefined; } @@ -55,18 +57,24 @@ export async function searchForIcon( iconSet: IconifyJSON, collection: string, ids: string[], - iconCustomizations?: IconCustomizations, + iconCustomizations?: IconCustomizations ): Promise { let iconData: FullIconifyIcon | null; - const { customize, additionalProps = {}, iconCustomizer } = iconCustomizations || {} + const { + customize, + additionalProps = {}, + iconCustomizer, + } = iconCustomizations || {}; for (const id of ids) { iconData = getIconData(iconSet, id, true); if (iconData) { debug(`${collection}:${id}`); - const defaultCustomizations = { ...DefaultIconCustomizations } + const defaultCustomizations = { ...DefaultIconCustomizations }; const { attributes, body } = iconToSVG( iconData, - typeof customize === 'function' ? customize(defaultCustomizations) : defaultCustomizations + typeof customize === 'function' + ? customize(defaultCustomizations) + : defaultCustomizations ); return await mergeIconProps( `${body}`, @@ -74,8 +82,8 @@ export async function searchForIcon( id, additionalProps, () => attributes, - iconCustomizer, - ) + iconCustomizer + ); } } } diff --git a/packages/utils/src/loader/types.ts b/packages/utils/src/loader/types.ts index e7a68bf..5784f3d 100644 --- a/packages/utils/src/loader/types.ts +++ b/packages/utils/src/loader/types.ts @@ -9,7 +9,11 @@ export type CustomIconLoader = (name: string) => Awaitable; /** * Custom icon customizer, it will allow to customize all icons on a collection or individual icons. */ -export type IconCustomizer = (collection: string, icon: string, props: Record) => Awaitable; +export type IconCustomizer = ( + collection: string, + icon: string, + props: Record +) => Awaitable; /** * Icon customizations: will be applied to all resolved icons. @@ -29,24 +33,26 @@ export type IconCustomizations = { * @param svg The loaded `svg` * @return The transformed `svg`. */ - transform?: (svg: string) => Awaitable + transform?: (svg: string) => Awaitable; /** * Change default icon customizations values. * * @param defaultCustomizations Default icon's customizations values. * @return The modified icon's customizations values. */ - customize?: (defaultCustomizations: FullIconCustomisations) => FullIconCustomisations + customize?: ( + defaultCustomizations: FullIconCustomisations + ) => FullIconCustomisations; /** * Custom icon customizer. */ - iconCustomizer?: IconCustomizer + iconCustomizer?: IconCustomizer; /** * Additional icon properties. * * All properties without value will not be applied. */ - additionalProps?: Record + additionalProps?: Record; }; /** diff --git a/packages/utils/src/loader/utils.ts b/packages/utils/src/loader/utils.ts index 8950116..8caf7aa 100644 --- a/packages/utils/src/loader/utils.ts +++ b/packages/utils/src/loader/utils.ts @@ -21,17 +21,21 @@ export async function mergeIconProps( icon: string, additionalProps: Record, propsProvider?: () => Awaitable>, - iconCustomizer?: IconCustomizer, + iconCustomizer?: IconCustomizer ): Promise { - const props: Record = await propsProvider?.() ?? {} - await iconCustomizer?.(collection, icon, props) + const props: Record = (await propsProvider?.()) ?? {}; + await iconCustomizer?.(collection, icon, props); Object.keys(additionalProps).forEach((p) => { - const v = additionalProps[p] - if (v !== undefined && v !== null) - props[p] = v - }) - const replacement = svg.startsWith(' `${p}="${props[p]}"`).join(' ')}`) + const v = additionalProps[p]; + if (v !== undefined && v !== null) props[p] = v; + }); + const replacement = svg.startsWith(' `${p}="${props[p]}"`) + .join(' ')}` + ); } export async function tryInstallPkg(name: string): Promise { diff --git a/packages/utils/tests/get-custom-icon-test.ts b/packages/utils/tests/get-custom-icon-test.ts index 26a5576..f2ae0fe 100644 --- a/packages/utils/tests/get-custom-icon-test.ts +++ b/packages/utils/tests/get-custom-icon-test.ts @@ -12,17 +12,11 @@ describe('Testing getCustomIcon', () => { test('CustomIconLoader with transform', async () => { const svg = await fs.readFile(fixturesDir + '/circle.svg', 'utf8'); - const result = await getCustomIcon( - () => svg, - 'a', - 'b', - { - transform(icon) { - return icon.replace(' svg, 'a', 'b', { + transform(icon) { + return icon.replace(' -1).toBeTruthy(); expect(result && result.indexOf('height="1em"') > -1).toBeTruthy(); });