2
0
mirror of https://github.com/iconify/iconify.git synced 2024-11-09 23:00:56 +00:00

Move reusable string functions to separate file

This commit is contained in:
Vjacheslav Trushkin 2022-01-09 22:05:18 +02:00
parent 052c86c07b
commit c1fb9f8e6c
5 changed files with 56 additions and 30 deletions

View File

@ -136,6 +136,10 @@
"require": "./lib/loader/utils.js",
"import": "./lib/loader/utils.mjs"
},
"./lib/misc/strings": {
"require": "./lib/misc/strings.js",
"import": "./lib/misc/strings.mjs"
},
"./lib/svg/build": {
"require": "./lib/svg/build.js",
"import": "./lib/svg/build.mjs"
@ -154,8 +158,8 @@
"@antfu/utils": "^0.3.0",
"@iconify/types": "^1.0.12",
"debug": "^4.3.3",
"kolorist": "^1.5.0"
},
"kolorist": "^1.5.0"
},
"devDependencies": {
"@iconify/library-builder": "^1.0.4",
"@types/debug": "^4.1.7",

View File

@ -45,7 +45,14 @@ export { colorKeywords } from './colors/keywords';
export { stringToColor, compareColors, colorToString } from './colors/index';
// SVG Icon loader
export type { CustomIconLoader, CustomCollections, InlineCollection } from './loader/types';
export { camelize, camelToKebab, pascalize, tryInstallPkg } from './loader/utils';
export type {
CustomIconLoader,
CustomCollections,
InlineCollection,
} from './loader/types';
export { tryInstallPkg } from './loader/utils';
export { FileSystemIconLoader } from './loader/loaders';
export { getCustomIcon } from './loader/custom';
// Misc
export { camelize, camelToKebab, pascalize } from './misc/strings';

View File

@ -1,19 +1,24 @@
import type { Awaitable } from '@antfu/utils';
import { existsSync, promises as fs } from 'fs';
import type { CustomIconLoader } from './types';
import { camelize, pascalize } from './utils';
import { camelize, pascalize } from '../misc/strings';
export function FileSystemIconLoader(dir: string, transform?: (svg: string) => Awaitable<string>): CustomIconLoader {
return async(name) => {
export function FileSystemIconLoader(
dir: string,
transform?: (svg: string) => Awaitable<string>
): CustomIconLoader {
return async (name) => {
const paths = [
`${dir}/${name}.svg`,
`${dir}/${camelize(name)}.svg`,
`${dir}/${pascalize(name)}.svg`
`${dir}/${pascalize(name)}.svg`,
];
for (const path of paths) {
if (existsSync(path)) {
const svg = await fs.readFile(path, 'utf-8');
return typeof transform === 'function' ? await transform(svg) : svg;
return typeof transform === 'function'
? await transform(svg)
: svg;
}
}
};

View File

@ -1,23 +1,6 @@
import { installPackage } from '@antfu/install-pkg';
import { sleep } from '@antfu/utils';
import { cyan, yellow } from 'kolorist'
export function camelize(str: string): string {
return str.replace(/-([a-z0-9])/g, g => g[1].toUpperCase());
}
export function pascalize(str: string): string {
const camel = camelize(str);
return `${camel[0].toUpperCase()}${camel.slice(1)}`;
}
export function camelToKebab(key: string): string {
const result = key
.replace(/:/g, '-')
.replace(/([A-Z])/g, ' $1')
.trim();
return result.split(/\s+/g).join('-').toLowerCase();
}
import { cyan, yellow } from 'kolorist';
const warned = new Set<string>();
@ -28,7 +11,6 @@ export function warnOnce(msg: string): void {
}
}
let pending: Promise<void> | undefined;
const tasks: Record<string, Promise<void> | undefined> = {};
@ -39,8 +21,11 @@ export async function tryInstallPkg(name: string): Promise<void | undefined> {
if (!tasks[name]) {
// eslint-disable-next-line no-console
console.log(cyan(`Installing ${name}...`))
tasks[name] = pending = installPackage(name, { dev: true, preferOffline: true })
console.log(cyan(`Installing ${name}...`));
tasks[name] = pending = installPackage(name, {
dev: true,
preferOffline: true,
})
.then(() => sleep(300))
// eslint-disable-next-line
.catch((e: any) => {

View File

@ -0,0 +1,25 @@
/**
* Convert string to camelCase
*/
export function camelize(str: string): string {
return str.replace(/-([a-z0-9])/g, (g) => g[1].toUpperCase());
}
/**
* Convert string to PascaleCase
*/
export function pascalize(str: string): string {
const camel = camelize(str);
return camel.slice(0, 1).toUpperCase() + camel.slice(1);
}
/**
* Convert camelCase string to kebab-case
*/
export function camelToKebab(key: string): string {
const result = key
.replace(/:/g, '-')
.replace(/([A-Z])/g, ' $1')
.trim();
return result.split(/\s+/g).join('-').toLowerCase();
}