|
| 1 | +// Copyright 2018-2025 the Deno authors. MIT license. |
| 2 | +// This module is browser compatible. |
| 3 | +import { escape } from "@std/regexp/escape"; |
| 4 | + |
| 5 | +/** |
| 6 | + * A pattern that can be used to trim characters from an input string. |
| 7 | + * - If `string`, trim all characters in the pattern string. |
| 8 | + * - If `Iterable<string>`, trim all substrings equal to any item. |
| 9 | + * - If `RegExp`, trim all substrings that match the regex. |
| 10 | + */ |
| 11 | +export type TrimPattern = |
| 12 | + | string |
| 13 | + | Iterable<string> |
| 14 | + | RegExp; |
| 15 | + |
| 16 | +/** |
| 17 | + * Trims all instances of the specified pattern at the start and end of the string. |
| 18 | + * |
| 19 | + * @experimental **UNSTABLE**: New API, yet to be vetted. |
| 20 | + * |
| 21 | + * @param str The input string |
| 22 | + * @param pattern The pattern to trim |
| 23 | + * |
| 24 | + * @example Strip non-word characters from start and end of a string |
| 25 | + * ```ts |
| 26 | + * import { trim } from "@std/text/unstable-trim"; |
| 27 | + * import { assertEquals } from "@std/assert"; |
| 28 | + * |
| 29 | + * const result = trim("¡¿Seguro que no?!", /[^\p{L}\p{M}\p{N}]/u); |
| 30 | + * assertEquals(result, "Seguro que no"); |
| 31 | + * ``` |
| 32 | + */ |
| 33 | +export function trim( |
| 34 | + str: string, |
| 35 | + pattern: TrimPattern, |
| 36 | +): string { |
| 37 | + return trimStart(trimEnd(str, pattern), pattern); |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Trims all instances of the specified pattern at the start of the string. |
| 42 | + * |
| 43 | + * @experimental **UNSTABLE**: New API, yet to be vetted. |
| 44 | + * |
| 45 | + * @param str The input string |
| 46 | + * @param pattern The pattern to trim |
| 47 | + * |
| 48 | + * @example Remove leading byte-order marks |
| 49 | + * ```ts |
| 50 | + * import { trimStart } from "@std/text/unstable-trim"; |
| 51 | + * import { assertEquals } from "@std/assert"; |
| 52 | + * |
| 53 | + * const result = trimStart("\ufeffhello world", "\ufeff"); |
| 54 | + * assertEquals(result, "hello world"); |
| 55 | + * ``` |
| 56 | + */ |
| 57 | +export function trimStart( |
| 58 | + str: string, |
| 59 | + pattern: TrimPattern, |
| 60 | +): string { |
| 61 | + return str.replace(regExpFromTrimPattern`^${pattern}`, ""); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Trims all instances of the specified pattern at the end of the string. |
| 66 | + * |
| 67 | + * @experimental **UNSTABLE**: New API, yet to be vetted. |
| 68 | + * |
| 69 | + * @param str The input string |
| 70 | + * @param pattern The pattern to trim |
| 71 | + * |
| 72 | + * @example Remove trailing line endings |
| 73 | + * ```ts |
| 74 | + * import { trimEnd } from "@std/text/unstable-trim"; |
| 75 | + * import { assertEquals } from "@std/assert"; |
| 76 | + * |
| 77 | + * const result = trimEnd("file contents\r\n\n", ["\n", "\r\n"]); |
| 78 | + * assertEquals(result, "file contents"); |
| 79 | + * ``` |
| 80 | + */ |
| 81 | +export function trimEnd( |
| 82 | + str: string, |
| 83 | + pattern: TrimPattern, |
| 84 | +): string { |
| 85 | + return str.replace(regExpFromTrimPattern`${pattern}$`, ""); |
| 86 | +} |
| 87 | + |
| 88 | +function regExpFromTrimPattern(t: TemplateStringsArray, pattern: TrimPattern) { |
| 89 | + let { source, flags } = pattern instanceof RegExp ? pattern : { |
| 90 | + source: `${ |
| 91 | + [...new Set(pattern)] |
| 92 | + .sort((a, b) => b.length - a.length) |
| 93 | + .map(escape) |
| 94 | + .join("|") |
| 95 | + }`, |
| 96 | + flags: "", |
| 97 | + }; |
| 98 | + |
| 99 | + source = `${t[0]!}(?:${source})+${t[1]!}`; |
| 100 | + flags = flags.replace(/[gy]+/g, ""); |
| 101 | + |
| 102 | + return new RegExp(source, flags); |
| 103 | +} |
0 commit comments