mirror of
https://github.com/iconify/iconify.git
synced 2024-12-12 13:47:49 +00:00
Move reusable string functions to separate file
This commit is contained in:
parent
0a62d009a8
commit
2a0dbf8fa9
@ -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",
|
||||
|
@ -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';
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -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) => {
|
||||
|
25
packages/utils/src/misc/strings.ts
Normal file
25
packages/utils/src/misc/strings.ts
Normal 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();
|
||||
}
|
Loading…
Reference in New Issue
Block a user