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",
"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",

View File

@ -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<string | undefined> {
let result: string | undefined | null;
@ -26,10 +30,16 @@ export async function getCustomIcon(
if (result) {
if (!result.startsWith('<svg ')) {
console.warn(`Custom icon "${icon}" in "${collection}" is not a valid SVG`)
return result
console.warn(
`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(
transform ? await transform(result) : result,
collection,
@ -37,7 +47,6 @@ export async function getCustomIcon(
additionalProps,
undefined,
iconCustomizer
)
);
}
}

View File

@ -16,7 +16,10 @@ const debugLegacy = createDebugger('@iconify-loader:legacy');
const _collections: Record<string, Promise<IconifyJSON | undefined>> = {};
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]) {
_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<string | undefined> {
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(
`<svg>${body}</svg>`,
@ -74,8 +82,8 @@ export async function searchForIcon(
id,
additionalProps,
() => 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.
*/
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.
@ -29,24 +33,26 @@ export type IconCustomizations = {
* @param svg The loaded `svg`
* @return The transformed `svg`.
*/
transform?: (svg: string) => Awaitable<string>
transform?: (svg: string) => Awaitable<string>;
/**
* 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<string, string | undefined>
additionalProps?: Record<string, string | undefined>;
};
/**

View File

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

View File

@ -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 ', '<svg width="1em" height="1em" ');
}
}
);
const result = await getCustomIcon(() => svg, 'a', 'b', {
transform(icon) {
return icon.replace('<svg ', '<svg width="1em" height="1em" ');
},
});
expect(result && result.indexOf('width="1em"') > -1).toBeTruthy();
expect(result && result.indexOf('height="1em"') > -1).toBeTruthy();
});