2
0
mirror of https://github.com/iconify/iconify.git synced 2024-12-13 14:13:06 +00:00

Prettier recently updated files

This commit is contained in:
Vjacheslav Trushkin 2022-01-10 18:08:40 +02:00
parent c8da6958ab
commit 4487568ba4
6 changed files with 67 additions and 46 deletions

View File

@ -128,10 +128,10 @@
"require": "./lib/loader/loaders.js", "require": "./lib/loader/loaders.js",
"import": "./lib/loader/loaders.mjs" "import": "./lib/loader/loaders.mjs"
}, },
"./lib/loader/modern": { "./lib/loader/modern": {
"require": "./lib/loader/modern.js", "require": "./lib/loader/modern.js",
"import": "./lib/loader/modern.mjs" "import": "./lib/loader/modern.mjs"
}, },
"./lib/loader/types": { "./lib/loader/types": {
"require": "./lib/loader/types.js", "require": "./lib/loader/types.js",
"import": "./lib/loader/types.mjs" "import": "./lib/loader/types.mjs"
@ -163,7 +163,7 @@
"@iconify/types": "^1.0.12", "@iconify/types": "^1.0.12",
"debug": "^4.3.3", "debug": "^4.3.3",
"kolorist": "^1.5.0", "kolorist": "^1.5.0",
"local-pkg": "^0.4.0" "local-pkg": "^0.4.0"
}, },
"devDependencies": { "devDependencies": {
"@iconify/library-builder": "^1.0.4", "@iconify/library-builder": "^1.0.4",

View File

@ -1,5 +1,9 @@
import createDebugger from 'debug'; import createDebugger from 'debug';
import type { CustomIconLoader, IconCustomizations, InlineCollection } from './types'; import type {
CustomIconLoader,
IconCustomizations,
InlineCollection,
} from './types';
import { mergeIconProps } from './utils'; import { mergeIconProps } from './utils';
const debug = createDebugger('@iconify-loader:custom'); const debug = createDebugger('@iconify-loader:custom');
@ -11,7 +15,7 @@ export async function getCustomIcon(
custom: CustomIconLoader | InlineCollection, custom: CustomIconLoader | InlineCollection,
collection: string, collection: string,
icon: string, icon: string,
iconsCustomizations?: IconCustomizations, iconsCustomizations?: IconCustomizations
): Promise<string | undefined> { ): Promise<string | undefined> {
let result: string | undefined | null; let result: string | undefined | null;
@ -26,10 +30,16 @@ export async function getCustomIcon(
if (result) { if (result) {
if (!result.startsWith('<svg ')) { if (!result.startsWith('<svg ')) {
console.warn(`Custom icon "${icon}" in "${collection}" is not a valid SVG`) console.warn(
return result `Custom icon "${icon}" in "${collection}" is not a valid SVG`
);
return result;
} }
const { transform, additionalProps = {}, iconCustomizer } = iconsCustomizations || {} const {
transform,
additionalProps = {},
iconCustomizer,
} = iconsCustomizations || {};
return await mergeIconProps( return await mergeIconProps(
transform ? await transform(result) : result, transform ? await transform(result) : result,
collection, collection,
@ -37,7 +47,6 @@ export async function getCustomIcon(
additionalProps, additionalProps,
undefined, undefined,
iconCustomizer iconCustomizer
) );
} }
} }

View File

@ -16,7 +16,10 @@ const debugLegacy = createDebugger('@iconify-loader:legacy');
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {}; const _collections: Record<string, Promise<IconifyJSON | undefined>> = {};
const isLegacyExists = isPackageExists('@iconify/json'); const isLegacyExists = isPackageExists('@iconify/json');
export async function loadCollection(name: string, autoInstall = false): Promise<IconifyJSON | undefined> { export async function loadCollection(
name: string,
autoInstall = false
): Promise<IconifyJSON | undefined> {
if (!_collections[name]) { if (!_collections[name]) {
_collections[name] = task(); _collections[name] = task();
} }
@ -43,8 +46,7 @@ export async function loadCollection(name: string, autoInstall = false): Promise
if (jsonPath) { if (jsonPath) {
return JSON.parse(await fs.readFile(jsonPath, 'utf8')); return JSON.parse(await fs.readFile(jsonPath, 'utf8'));
} } else {
else {
debugModern(`failed to load ${name}`); debugModern(`failed to load ${name}`);
return undefined; return undefined;
} }
@ -55,18 +57,24 @@ export async function searchForIcon(
iconSet: IconifyJSON, iconSet: IconifyJSON,
collection: string, collection: string,
ids: string[], ids: string[],
iconCustomizations?: IconCustomizations, iconCustomizations?: IconCustomizations
): Promise<string | undefined> { ): Promise<string | undefined> {
let iconData: FullIconifyIcon | null; let iconData: FullIconifyIcon | null;
const { customize, additionalProps = {}, iconCustomizer } = iconCustomizations || {} const {
customize,
additionalProps = {},
iconCustomizer,
} = iconCustomizations || {};
for (const id of ids) { for (const id of ids) {
iconData = getIconData(iconSet, id, true); iconData = getIconData(iconSet, id, true);
if (iconData) { if (iconData) {
debug(`${collection}:${id}`); debug(`${collection}:${id}`);
const defaultCustomizations = { ...DefaultIconCustomizations } const defaultCustomizations = { ...DefaultIconCustomizations };
const { attributes, body } = iconToSVG( const { attributes, body } = iconToSVG(
iconData, iconData,
typeof customize === 'function' ? customize(defaultCustomizations) : defaultCustomizations typeof customize === 'function'
? customize(defaultCustomizations)
: defaultCustomizations
); );
return await mergeIconProps( return await mergeIconProps(
`<svg>${body}</svg>`, `<svg>${body}</svg>`,
@ -74,8 +82,8 @@ export async function searchForIcon(
id, id,
additionalProps, additionalProps,
() => attributes, () => attributes,
iconCustomizer, iconCustomizer
) );
} }
} }
} }

View File

@ -9,7 +9,11 @@ export type CustomIconLoader = (name: string) => Awaitable<string | undefined>;
/** /**
* Custom icon customizer, it will allow to customize all icons on a collection or individual icons. * 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<string, string>) => Awaitable<void>; export type IconCustomizer = (
collection: string,
icon: string,
props: Record<string, string>
) => Awaitable<void>;
/** /**
* Icon customizations: will be applied to all resolved icons. * Icon customizations: will be applied to all resolved icons.
@ -29,24 +33,26 @@ export type IconCustomizations = {
* @param svg The loaded `svg` * @param svg The loaded `svg`
* @return The transformed `svg`. * @return The transformed `svg`.
*/ */
transform?: (svg: string) => Awaitable<string> transform?: (svg: string) => Awaitable<string>;
/** /**
* Change default icon customizations values. * Change default icon customizations values.
* *
* @param defaultCustomizations Default icon's customizations values. * @param defaultCustomizations Default icon's customizations values.
* @return The modified icon's customizations values. * @return The modified icon's customizations values.
*/ */
customize?: (defaultCustomizations: FullIconCustomisations) => FullIconCustomisations customize?: (
defaultCustomizations: FullIconCustomisations
) => FullIconCustomisations;
/** /**
* Custom icon customizer. * Custom icon customizer.
*/ */
iconCustomizer?: IconCustomizer iconCustomizer?: IconCustomizer;
/** /**
* Additional icon properties. * Additional icon properties.
* *
* All properties without value will not be applied. * All properties without value will not be applied.
*/ */
additionalProps?: Record<string, string | undefined> additionalProps?: Record<string, string | undefined>;
}; };
/** /**

View File

@ -21,17 +21,21 @@ export async function mergeIconProps(
icon: string, icon: string,
additionalProps: Record<string, string | undefined>, additionalProps: Record<string, string | undefined>,
propsProvider?: () => Awaitable<Record<string, string>>, propsProvider?: () => Awaitable<Record<string, string>>,
iconCustomizer?: IconCustomizer, iconCustomizer?: IconCustomizer
): Promise<string> { ): Promise<string> {
const props: Record<string, string> = await propsProvider?.() ?? {} const props: Record<string, string> = (await propsProvider?.()) ?? {};
await iconCustomizer?.(collection, icon, props) await iconCustomizer?.(collection, icon, props);
Object.keys(additionalProps).forEach((p) => { Object.keys(additionalProps).forEach((p) => {
const v = additionalProps[p] const v = additionalProps[p];
if (v !== undefined && v !== null) if (v !== undefined && v !== null) props[p] = v;
props[p] = v });
}) const replacement = svg.startsWith('<svg ') ? '<svg ' : '<svg';
const replacement = svg.startsWith('<svg ') ? '<svg ' : '<svg' return svg.replace(
return svg.replace(replacement, `${replacement}${Object.keys(props).map(p => `${p}="${props[p]}"`).join(' ')}`) replacement,
`${replacement}${Object.keys(props)
.map((p) => `${p}="${props[p]}"`)
.join(' ')}`
);
} }
export async function tryInstallPkg(name: string): Promise<void | undefined> { export async function tryInstallPkg(name: string): Promise<void | undefined> {

View File

@ -12,17 +12,11 @@ describe('Testing getCustomIcon', () => {
test('CustomIconLoader with transform', async () => { test('CustomIconLoader with transform', async () => {
const svg = await fs.readFile(fixturesDir + '/circle.svg', 'utf8'); const svg = await fs.readFile(fixturesDir + '/circle.svg', 'utf8');
const result = await getCustomIcon( const result = await getCustomIcon(() => svg, 'a', 'b', {
() => svg, transform(icon) {
'a', return icon.replace('<svg ', '<svg width="1em" height="1em" ');
'b', },
{ });
transform(icon) {
return icon.replace('<svg ', '<svg width="1em" height="1em" ');
}
}
);
expect(result && result.indexOf('width="1em"') > -1).toBeTruthy(); expect(result && result.indexOf('width="1em"') > -1).toBeTruthy();
expect(result && result.indexOf('height="1em"') > -1).toBeTruthy(); expect(result && result.indexOf('height="1em"') > -1).toBeTruthy();
}); });