diff --git a/scripts/benchmarks/src/compareToBaseline.ts b/scripts/benchmarks/src/compareToBaseline.ts index c10624a2..7a6bd049 100644 --- a/scripts/benchmarks/src/compareToBaseline.ts +++ b/scripts/benchmarks/src/compareToBaseline.ts @@ -1,8 +1,8 @@ import * as esbuild from 'esbuild' import { execa } from 'execa' import fs from 'node:fs/promises' +import { dedent } from 'radashi/string/dedent.ts' import { Project, SyntaxKind } from 'ts-morph' -import { dedent } from './dedent.ts' import { normalizeIdentifiers } from './normalizeIdentifiers.ts' export async function compareToBaseline( diff --git a/scripts/benchmarks/src/dedent.ts b/scripts/benchmarks/src/dedent.ts deleted file mode 100644 index 131939e9..00000000 --- a/scripts/benchmarks/src/dedent.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { isArray } from 'radashi/typed/isArray.ts' - -/** - * Remove indentation from a string. The given string is expected to - * be consistently indented (i.e. the leading whitespace of the first - * non-empty line is the minimum required for all non-empty lines). - * - * If the `indent` argument is nullish, the indentation is detected - * from the first non-empty line. Detection is cheap and robust for - * most use cases, so you should only set an explicit `indent` if - * necessary. - * - * @see https://radashi.js.org/reference/string/dedent - * @example - * ```ts - * // This is indented with 4 spaces. - * const input = ` - * Hello - * World - * ` - * - * // Explicit indentation - * dedent(input, ' ') - * // => ' Hello\n World\n' - * - * // Detected indentation - * dedent(input) - * // => 'Hello\nWorld\n' - * - * // Tagged template strings - * const str = dedent` - * Foo ${1 + 1} - * Bar ${2 * 2} - * ` - * // => 'Foo 2\nBar 4' - * ``` - */ -export function dedent( - template: TemplateStringsArray, - ...values: unknown[] -): string - -export function dedent(text: string, indent?: string | null): string - -export function dedent( - text: string | TemplateStringsArray, - ...values: unknown[] -): string { - // Support tagged template strings - if (isArray(text)) { - if (values.length > 0) { - return dedent( - text.reduce((acc, input, i) => { - let value = String(values[i] ?? '') - - // Detect the indentation before this embedded string. - const indent = - value.includes('\n') && input.match(/[ \t]*(?=[^\n]*$)/)?.[0] - - // Ensure the multi-line, embedded string can be correctly - // dedented. - if (indent) { - value = value.replace(/\n(?=[^\n]*?\S)/g, '\n' + indent) - } - - return acc + input + value - }, ''), - ) - } - - text = text[0] - } - - const indent = values[0] ?? detectIndent(text) - const output = indent - ? text.replace(new RegExp(`^${indent}`, 'gm'), '') - : text - - // Remove the first and last lines (if empty). - return output.replace(/^[ \t]*\n|\n[ \t]*$/g, '') -} - -// Find the indentation of the first non-empty line. -function detectIndent(text: string) { - return text.match(/^[ \t]*(?=\S)/m)?.[0] -} diff --git a/scripts/benchmarks/src/normalizeIdentifiers.test.ts b/scripts/benchmarks/src/normalizeIdentifiers.test.ts index f0e1538a..d1b01eb9 100644 --- a/scripts/benchmarks/src/normalizeIdentifiers.test.ts +++ b/scripts/benchmarks/src/normalizeIdentifiers.test.ts @@ -1,5 +1,5 @@ +import { dedent } from 'radashi/string/dedent.ts' import { describe, expect, it } from 'vitest' -import { dedent } from './dedent.ts' import { normalizeIdentifiers } from './normalizeIdentifiers.ts' describe('normalizeIdentifiers', () => { diff --git a/scripts/bundle-impact/src/dedent.ts b/scripts/bundle-impact/src/dedent.ts deleted file mode 100644 index 131939e9..00000000 --- a/scripts/bundle-impact/src/dedent.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { isArray } from 'radashi/typed/isArray.ts' - -/** - * Remove indentation from a string. The given string is expected to - * be consistently indented (i.e. the leading whitespace of the first - * non-empty line is the minimum required for all non-empty lines). - * - * If the `indent` argument is nullish, the indentation is detected - * from the first non-empty line. Detection is cheap and robust for - * most use cases, so you should only set an explicit `indent` if - * necessary. - * - * @see https://radashi.js.org/reference/string/dedent - * @example - * ```ts - * // This is indented with 4 spaces. - * const input = ` - * Hello - * World - * ` - * - * // Explicit indentation - * dedent(input, ' ') - * // => ' Hello\n World\n' - * - * // Detected indentation - * dedent(input) - * // => 'Hello\nWorld\n' - * - * // Tagged template strings - * const str = dedent` - * Foo ${1 + 1} - * Bar ${2 * 2} - * ` - * // => 'Foo 2\nBar 4' - * ``` - */ -export function dedent( - template: TemplateStringsArray, - ...values: unknown[] -): string - -export function dedent(text: string, indent?: string | null): string - -export function dedent( - text: string | TemplateStringsArray, - ...values: unknown[] -): string { - // Support tagged template strings - if (isArray(text)) { - if (values.length > 0) { - return dedent( - text.reduce((acc, input, i) => { - let value = String(values[i] ?? '') - - // Detect the indentation before this embedded string. - const indent = - value.includes('\n') && input.match(/[ \t]*(?=[^\n]*$)/)?.[0] - - // Ensure the multi-line, embedded string can be correctly - // dedented. - if (indent) { - value = value.replace(/\n(?=[^\n]*?\S)/g, '\n' + indent) - } - - return acc + input + value - }, ''), - ) - } - - text = text[0] - } - - const indent = values[0] ?? detectIndent(text) - const output = indent - ? text.replace(new RegExp(`^${indent}`, 'gm'), '') - : text - - // Remove the first and last lines (if empty). - return output.replace(/^[ \t]*\n|\n[ \t]*$/g, '') -} - -// Find the indentation of the first non-empty line. -function detectIndent(text: string) { - return text.match(/^[ \t]*(?=\S)/m)?.[0] -} diff --git a/scripts/publish-version/src/dedent.ts b/scripts/publish-version/src/dedent.ts deleted file mode 100644 index 131939e9..00000000 --- a/scripts/publish-version/src/dedent.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { isArray } from 'radashi/typed/isArray.ts' - -/** - * Remove indentation from a string. The given string is expected to - * be consistently indented (i.e. the leading whitespace of the first - * non-empty line is the minimum required for all non-empty lines). - * - * If the `indent` argument is nullish, the indentation is detected - * from the first non-empty line. Detection is cheap and robust for - * most use cases, so you should only set an explicit `indent` if - * necessary. - * - * @see https://radashi.js.org/reference/string/dedent - * @example - * ```ts - * // This is indented with 4 spaces. - * const input = ` - * Hello - * World - * ` - * - * // Explicit indentation - * dedent(input, ' ') - * // => ' Hello\n World\n' - * - * // Detected indentation - * dedent(input) - * // => 'Hello\nWorld\n' - * - * // Tagged template strings - * const str = dedent` - * Foo ${1 + 1} - * Bar ${2 * 2} - * ` - * // => 'Foo 2\nBar 4' - * ``` - */ -export function dedent( - template: TemplateStringsArray, - ...values: unknown[] -): string - -export function dedent(text: string, indent?: string | null): string - -export function dedent( - text: string | TemplateStringsArray, - ...values: unknown[] -): string { - // Support tagged template strings - if (isArray(text)) { - if (values.length > 0) { - return dedent( - text.reduce((acc, input, i) => { - let value = String(values[i] ?? '') - - // Detect the indentation before this embedded string. - const indent = - value.includes('\n') && input.match(/[ \t]*(?=[^\n]*$)/)?.[0] - - // Ensure the multi-line, embedded string can be correctly - // dedented. - if (indent) { - value = value.replace(/\n(?=[^\n]*?\S)/g, '\n' + indent) - } - - return acc + input + value - }, ''), - ) - } - - text = text[0] - } - - const indent = values[0] ?? detectIndent(text) - const output = indent - ? text.replace(new RegExp(`^${indent}`, 'gm'), '') - : text - - // Remove the first and last lines (if empty). - return output.replace(/^[ \t]*\n|\n[ \t]*$/g, '') -} - -// Find the indentation of the first non-empty line. -function detectIndent(text: string) { - return text.match(/^[ \t]*(?=\S)/m)?.[0] -} diff --git a/scripts/publish-version/src/publishVersion.ts b/scripts/publish-version/src/publishVersion.ts index 8de23ef6..d8bc51ab 100644 --- a/scripts/publish-version/src/publishVersion.ts +++ b/scripts/publish-version/src/publishVersion.ts @@ -6,8 +6,8 @@ import { green } from 'kleur/colors' import crypto from 'node:crypto' import fs from 'node:fs/promises' import { sift } from 'radashi/array/sift.ts' +import { dedent } from 'radashi/string/dedent.ts' import { glob } from 'tinyglobby' -import { dedent } from './dedent.ts' import { trackVersion } from './trackVersion.ts' export const VALID_TAGS = ['beta', 'next'] as const diff --git a/scripts/release-notes/src/main.ts b/scripts/release-notes/src/main.ts index b3ed18d9..e024977f 100644 --- a/scripts/release-notes/src/main.ts +++ b/scripts/release-notes/src/main.ts @@ -6,7 +6,7 @@ import fs from 'node:fs' import os from 'node:os' import path from 'node:path' import { uid } from 'radashi/random/uid.ts' -import { dedent } from './util/dedent.ts' +import { dedent } from 'radashi/string/dedent.ts' main() diff --git a/scripts/release-notes/src/util/dedent.ts b/scripts/release-notes/src/util/dedent.ts deleted file mode 100644 index 131939e9..00000000 --- a/scripts/release-notes/src/util/dedent.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { isArray } from 'radashi/typed/isArray.ts' - -/** - * Remove indentation from a string. The given string is expected to - * be consistently indented (i.e. the leading whitespace of the first - * non-empty line is the minimum required for all non-empty lines). - * - * If the `indent` argument is nullish, the indentation is detected - * from the first non-empty line. Detection is cheap and robust for - * most use cases, so you should only set an explicit `indent` if - * necessary. - * - * @see https://radashi.js.org/reference/string/dedent - * @example - * ```ts - * // This is indented with 4 spaces. - * const input = ` - * Hello - * World - * ` - * - * // Explicit indentation - * dedent(input, ' ') - * // => ' Hello\n World\n' - * - * // Detected indentation - * dedent(input) - * // => 'Hello\nWorld\n' - * - * // Tagged template strings - * const str = dedent` - * Foo ${1 + 1} - * Bar ${2 * 2} - * ` - * // => 'Foo 2\nBar 4' - * ``` - */ -export function dedent( - template: TemplateStringsArray, - ...values: unknown[] -): string - -export function dedent(text: string, indent?: string | null): string - -export function dedent( - text: string | TemplateStringsArray, - ...values: unknown[] -): string { - // Support tagged template strings - if (isArray(text)) { - if (values.length > 0) { - return dedent( - text.reduce((acc, input, i) => { - let value = String(values[i] ?? '') - - // Detect the indentation before this embedded string. - const indent = - value.includes('\n') && input.match(/[ \t]*(?=[^\n]*$)/)?.[0] - - // Ensure the multi-line, embedded string can be correctly - // dedented. - if (indent) { - value = value.replace(/\n(?=[^\n]*?\S)/g, '\n' + indent) - } - - return acc + input + value - }, ''), - ) - } - - text = text[0] - } - - const indent = values[0] ?? detectIndent(text) - const output = indent - ? text.replace(new RegExp(`^${indent}`, 'gm'), '') - : text - - // Remove the first and last lines (if empty). - return output.replace(/^[ \t]*\n|\n[ \t]*$/g, '') -} - -// Find the indentation of the first non-empty line. -function detectIndent(text: string) { - return text.match(/^[ \t]*(?=\S)/m)?.[0] -}