diff --git a/README.md b/README.md index d9673aaf..50b0adff 100644 --- a/README.md +++ b/README.md @@ -282,6 +282,23 @@ describe("your-consola-mock-test", () => { } ``` +## Console Utils + +```ts +// ESM +import { + stripAnsi, + centerAlign, + rightAlign, + leftAlign, + align, + box, +} from "consola/utils"; + +// CommonJS +const { stripAnsi } = require("consola/utils"); +``` + ## License MIT diff --git a/package.json b/package.json index 378c47a0..ebfb7760 100644 --- a/package.json +++ b/package.json @@ -27,34 +27,39 @@ "require": "./lib/index.cjs" }, "default": { - "types": "./dist/index.browser.d.ts", - "import": "./dist/index.browser.mjs" + "types": "./dist/browser.d.ts", + "import": "./dist/browser.mjs" } }, "./browser": { - "types": "./dist/index.browser.d.ts", - "import": "./dist/index.browser.mjs" + "types": "./dist/browser.d.ts", + "import": "./dist/browser.mjs" }, "./basic": { "node": { - "types": "./dist/index.basic.d.ts", - "import": "./dist/index.basic.mjs", - "require": "./dist/index.basic.cjs" + "types": "./dist/basic.d.ts", + "import": "./dist/basic.mjs", + "require": "./dist/basic.cjs" }, "default": { - "types": "./dist/index.browser.d.ts", - "import": "./dist/index.browser.mjs" + "types": "./dist/browser.d.ts", + "import": "./dist/browser.mjs" } }, "./core": { - "types": "./dist/index.core.d.ts", - "import": "./dist/index.core.mjs", - "require": "./dist/index.core.cjs" + "types": "./dist/core.d.ts", + "import": "./dist/core.mjs", + "require": "./dist/core.cjs" + }, + "./utils": { + "types": "./dist/utils.d.ts", + "import": "./dist/utils.mjs", + "require": "./dist/utils.cjs" } }, "main": "./lib/index.cjs", "module": "./dist/index.mjs", - "browser": "./dist/index.browser.mjs", + "browser": "./dist/browser.mjs", "types": "./dist/index.d.ts", "files": [ "dist", diff --git a/src/index.basic.ts b/src/basic.ts similarity index 96% rename from src/index.basic.ts rename to src/basic.ts index b9a364b5..e5ccfa05 100644 --- a/src/index.basic.ts +++ b/src/basic.ts @@ -3,7 +3,7 @@ import type { ConsolaOptions } from "./types"; import { BasicReporter } from "./reporters/basic"; import { ConsolaInstance, createConsola as _createConsola } from "./consola"; -export * from "./index.shared"; +export * from "./shared"; export function createConsola( options: Partial = {} diff --git a/src/index.browser.ts b/src/browser.ts similarity index 95% rename from src/index.browser.ts rename to src/browser.ts index 6e0b953e..685e0878 100644 --- a/src/index.browser.ts +++ b/src/browser.ts @@ -2,7 +2,7 @@ import { BrowserReporter } from "./reporters/browser"; import { createConsola as _createConsola } from "./consola"; import type { ConsolaOptions } from "./types"; -export * from "./index.shared"; +export * from "./shared"; export function createConsola(options: Partial = {}) { const consola = _createConsola({ diff --git a/src/consola.ts b/src/consola.ts index e874d024..e33c7fc4 100644 --- a/src/consola.ts +++ b/src/consola.ts @@ -1,6 +1,6 @@ import { defu } from "defu"; import { LogTypes, LogType, LogLevel } from "./constants"; -import { isLogObj } from "./utils/index"; +import { isLogObj } from "./utils/log"; import type { ConsolaReporter, InputLogObject, diff --git a/src/index.core.ts b/src/core.ts similarity index 57% rename from src/index.core.ts rename to src/core.ts index cd743998..377b1c62 100644 --- a/src/index.core.ts +++ b/src/core.ts @@ -1,2 +1,2 @@ export { createConsola } from "./consola"; -export * from "./index.shared"; +export * from "./shared"; diff --git a/src/index.ts b/src/index.ts index 59a7b0ec..d617185a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ import { BasicReporter } from "./reporters/basic"; import { FancyReporter } from "./reporters/fancy"; import { ConsolaInstance, createConsola as _createConsola } from "./consola"; -export * from "./index.shared"; +export * from "./shared"; export function createConsola( options: Partial = {} diff --git a/src/index.shared.ts b/src/shared.ts similarity index 100% rename from src/index.shared.ts rename to src/shared.ts diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 00000000..c508e99d --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,9 @@ +export * from "./utils/box"; + +export { + stripAnsi, + centerAlign, + rightAlign, + leftAlign, + align, +} from "./utils/string"; diff --git a/src/utils/index.ts b/src/utils/log.ts similarity index 100% rename from src/utils/index.ts rename to src/utils/log.ts diff --git a/src/utils/string.ts b/src/utils/string.ts index 5de29221..1d3ffce7 100644 --- a/src/utils/string.ts +++ b/src/utils/string.ts @@ -3,9 +3,9 @@ const ansiRegex = [ "(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))", ].join("|"); -export const stripAnsi = (text: string) => { +export function stripAnsi(text: string) { return text.replace(new RegExp(ansiRegex, "g"), ""); -}; +} export function centerAlign(str: string, len: number, space = " ") { const free = len - str.length;