mirror of
https://github.com/iconify/iconify.git
synced 2024-11-14 00:34:04 +00:00
Move reusable string functions to separate file
This commit is contained in:
parent
052c86c07b
commit
c1fb9f8e6c
@ -136,6 +136,10 @@
|
|||||||
"require": "./lib/loader/utils.js",
|
"require": "./lib/loader/utils.js",
|
||||||
"import": "./lib/loader/utils.mjs"
|
"import": "./lib/loader/utils.mjs"
|
||||||
},
|
},
|
||||||
|
"./lib/misc/strings": {
|
||||||
|
"require": "./lib/misc/strings.js",
|
||||||
|
"import": "./lib/misc/strings.mjs"
|
||||||
|
},
|
||||||
"./lib/svg/build": {
|
"./lib/svg/build": {
|
||||||
"require": "./lib/svg/build.js",
|
"require": "./lib/svg/build.js",
|
||||||
"import": "./lib/svg/build.mjs"
|
"import": "./lib/svg/build.mjs"
|
||||||
@ -154,8 +158,8 @@
|
|||||||
"@antfu/utils": "^0.3.0",
|
"@antfu/utils": "^0.3.0",
|
||||||
"@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"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@iconify/library-builder": "^1.0.4",
|
"@iconify/library-builder": "^1.0.4",
|
||||||
"@types/debug": "^4.1.7",
|
"@types/debug": "^4.1.7",
|
||||||
|
@ -45,7 +45,14 @@ export { colorKeywords } from './colors/keywords';
|
|||||||
export { stringToColor, compareColors, colorToString } from './colors/index';
|
export { stringToColor, compareColors, colorToString } from './colors/index';
|
||||||
|
|
||||||
// SVG Icon loader
|
// SVG Icon loader
|
||||||
export type { CustomIconLoader, CustomCollections, InlineCollection } from './loader/types';
|
export type {
|
||||||
export { camelize, camelToKebab, pascalize, tryInstallPkg } from './loader/utils';
|
CustomIconLoader,
|
||||||
|
CustomCollections,
|
||||||
|
InlineCollection,
|
||||||
|
} from './loader/types';
|
||||||
|
export { tryInstallPkg } from './loader/utils';
|
||||||
export { FileSystemIconLoader } from './loader/loaders';
|
export { FileSystemIconLoader } from './loader/loaders';
|
||||||
export { getCustomIcon } from './loader/custom';
|
export { getCustomIcon } from './loader/custom';
|
||||||
|
|
||||||
|
// Misc
|
||||||
|
export { camelize, camelToKebab, pascalize } from './misc/strings';
|
||||||
|
@ -1,19 +1,24 @@
|
|||||||
import type { Awaitable } from '@antfu/utils';
|
import type { Awaitable } from '@antfu/utils';
|
||||||
import { existsSync, promises as fs } from 'fs';
|
import { existsSync, promises as fs } from 'fs';
|
||||||
import type { CustomIconLoader } from './types';
|
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 {
|
export function FileSystemIconLoader(
|
||||||
return async(name) => {
|
dir: string,
|
||||||
|
transform?: (svg: string) => Awaitable<string>
|
||||||
|
): CustomIconLoader {
|
||||||
|
return async (name) => {
|
||||||
const paths = [
|
const paths = [
|
||||||
`${dir}/${name}.svg`,
|
`${dir}/${name}.svg`,
|
||||||
`${dir}/${camelize(name)}.svg`,
|
`${dir}/${camelize(name)}.svg`,
|
||||||
`${dir}/${pascalize(name)}.svg`
|
`${dir}/${pascalize(name)}.svg`,
|
||||||
];
|
];
|
||||||
for (const path of paths) {
|
for (const path of paths) {
|
||||||
if (existsSync(path)) {
|
if (existsSync(path)) {
|
||||||
const svg = await fs.readFile(path, 'utf-8');
|
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 { installPackage } from '@antfu/install-pkg';
|
||||||
import { sleep } from '@antfu/utils';
|
import { sleep } from '@antfu/utils';
|
||||||
import { cyan, yellow } from 'kolorist'
|
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
const warned = new Set<string>();
|
const warned = new Set<string>();
|
||||||
|
|
||||||
@ -28,7 +11,6 @@ export function warnOnce(msg: string): void {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let pending: Promise<void> | undefined;
|
let pending: Promise<void> | undefined;
|
||||||
const tasks: Record<string, 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]) {
|
if (!tasks[name]) {
|
||||||
// eslint-disable-next-line no-console
|
// eslint-disable-next-line no-console
|
||||||
console.log(cyan(`Installing ${name}...`))
|
console.log(cyan(`Installing ${name}...`));
|
||||||
tasks[name] = pending = installPackage(name, { dev: true, preferOffline: true })
|
tasks[name] = pending = installPackage(name, {
|
||||||
|
dev: true,
|
||||||
|
preferOffline: true,
|
||||||
|
})
|
||||||
.then(() => sleep(300))
|
.then(() => sleep(300))
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
.catch((e: any) => {
|
.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