UNPKG

@visulima/string

Version:

Functions for manipulating strings.

51 lines (50 loc) 1.6 kB
import { StringWidthOptions } from "./get-string-width.js"; import "./get-string-truncated-width.js"; /** * Configuration options for string slicing operations. * @example * ```typescript * const options: SliceOptions = { * width: { * fullWidth: 2, * ambiguousIsNarrow: true * }, * segmenter: new Intl.Segmenter('ja-JP', { granularity: 'word' }) * }; * ``` */ type SliceOptions = { /** * Custom segmenter instance for text segmentation */ segmenter?: Intl.Segmenter; /** * Options for string width calculations */ width?: StringWidthOptions; }; /** * Slices a string based on visible character width, preserving ANSI escape codes. * * Handles complex scenarios including grapheme clusters, full-width characters, * and ANSI styling (colors, formatting, hyperlinks). * @example * ```typescript * const styledString = "\u001B[31mHello\u001B[39m \u001B[1mWorld\u001B[22m!"; * * // Slice from visible index 2 to 8 * slice(styledString, 2, 8); * // => "\u001B[31mllo\u001B[39m \u001B[1mWo\u001B[22m" * * // Slice from visible index 7 onwards * slice(styledString, 7); * // => "\u001B[1morld\u001B[22m!" * ``` * @param inputString The string to slice. * @param startIndex The starting visible character index (inclusive). Defaults to 0. * @param endIndex The ending visible character index (exclusive). Defaults to the end of the string. * @param options Slicing options. * @returns The sliced string with ANSI codes preserved. */ declare const slice: (inputString: string, startIndex?: number, endIndex?: number, options?: SliceOptions) => string; export { SliceOptions, slice };