From 2a0dbf8fa9a99931747b532af372198450b7aef3 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Sun, 9 Jan 2022 22:05:18 +0200 Subject: [PATCH] Move reusable string functions to separate file --- packages/utils/package.json | 8 ++++++-- packages/utils/src/index.ts | 11 +++++++++-- packages/utils/src/loader/loaders.ts | 15 ++++++++++----- packages/utils/src/loader/utils.ts | 27 ++++++--------------------- packages/utils/src/misc/strings.ts | 25 +++++++++++++++++++++++++ 5 files changed, 56 insertions(+), 30 deletions(-) create mode 100644 packages/utils/src/misc/strings.ts diff --git a/packages/utils/package.json b/packages/utils/package.json index 91bed94..c24a5d4 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -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", diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts index fa4e353..b7be4a8 100644 --- a/packages/utils/src/index.ts +++ b/packages/utils/src/index.ts @@ -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'; diff --git a/packages/utils/src/loader/loaders.ts b/packages/utils/src/loader/loaders.ts index 502f2ea..b9cb9a0 100644 --- a/packages/utils/src/loader/loaders.ts +++ b/packages/utils/src/loader/loaders.ts @@ -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): CustomIconLoader { - return async(name) => { +export function FileSystemIconLoader( + dir: string, + transform?: (svg: string) => Awaitable +): 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; } } }; diff --git a/packages/utils/src/loader/utils.ts b/packages/utils/src/loader/utils.ts index 41bdb0b..282b1e9 100644 --- a/packages/utils/src/loader/utils.ts +++ b/packages/utils/src/loader/utils.ts @@ -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(); @@ -28,7 +11,6 @@ export function warnOnce(msg: string): void { } } - let pending: Promise | undefined; const tasks: Record | undefined> = {}; @@ -39,8 +21,11 @@ export async function tryInstallPkg(name: string): Promise { 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) => { diff --git a/packages/utils/src/misc/strings.ts b/packages/utils/src/misc/strings.ts new file mode 100644 index 0000000..74cd827 --- /dev/null +++ b/packages/utils/src/misc/strings.ts @@ -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(); +}