2
0
mirror of https://github.com/iconify/iconify.git synced 2024-10-25 01:52:03 +00:00
iconify/packages/utils/src/loader/modern.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-12-11 20:36:36 +00:00
import type { IconifyJSON } from '@iconify/types';
2022-01-10 11:17:05 +00:00
import type { FullIconifyIcon } from '../icon';
2022-01-10 11:21:34 +00:00
import { iconToSVG } from '../svg/build';
import { getIconData } from '../icon-set/get-icon';
import { mergeIconProps } from './utils';
2021-12-11 20:36:36 +00:00
import createDebugger from 'debug';
2022-01-10 11:17:05 +00:00
import { defaults as DefaultIconCustomizations } from '../customisations';
import type { IconifyLoaderOptions } from './types';
2021-12-09 21:12:00 +00:00
2022-01-10 11:17:05 +00:00
const debug = createDebugger('@iconify-loader:icon');
2021-12-09 21:12:00 +00:00
2022-01-10 12:43:35 +00:00
export async function searchForIcon(
iconSet: IconifyJSON,
collection: string,
ids: string[],
options?: IconifyLoaderOptions
2022-01-10 12:43:35 +00:00
): Promise<string | undefined> {
2021-12-11 20:36:36 +00:00
let iconData: FullIconifyIcon | null;
const { customize } = options?.customizations ?? {};
2021-12-09 21:12:00 +00:00
for (const id of ids) {
2021-12-11 20:36:36 +00:00
iconData = getIconData(iconSet, id, true);
2021-12-09 21:12:00 +00:00
if (iconData) {
2021-12-11 20:36:36 +00:00
debug(`${collection}:${id}`);
2022-01-10 16:08:40 +00:00
const defaultCustomizations = { ...DefaultIconCustomizations };
const { attributes, body } = iconToSVG(
iconData,
2022-01-10 16:08:40 +00:00
typeof customize === 'function'
? customize(defaultCustomizations)
: defaultCustomizations
);
2022-01-10 12:43:35 +00:00
return await mergeIconProps(
// DON'T remove space on <svg >
`<svg >${body}</svg>`,
2022-01-10 12:43:35 +00:00
collection,
id,
options,
() => attributes
2022-01-10 16:08:40 +00:00
);
2021-12-09 21:12:00 +00:00
}
}
}