All files / text / unstable_trim_by.ts

100.00% Branches 5/5
100.00% Lines 36/36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
 
 
x10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x10
x10
x10
 
x18
x18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x10
x10
x10
 
x34
x34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x10
x10
x10
 
x26
x26
 
x50
x50
x50
x50
x50
x50
 
x50
x50
x73
x232
x73
x73
x73
x73
x73
x50
 
x50
 
x50
 
x50
x50






































































































































































// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
import { escape } from "@std/regexp/escape";

/**
 * A pattern that can be used to trim characters from an input string.
 * - If `string`, trim all substrings equal to the string.
 * - If `Iterable<string>`, trim all substrings equal to any member.
 * - If `RegExp`, trim all substrings that match the regex.
 */
export type TrimPattern =
  | string
  | Iterable<string>
  | RegExp;

/**
 * Trims all instances of the specified pattern at the start and end of the string.
 *
 * @experimental **UNSTABLE**: New API, yet to be vetted.
 *
 * @param str The input string
 * @param pattern The pattern to trim
 * @returns The trimmed input string
 *
 * @example Remove slashes from start and end of a path
 * ```ts
 * import { trimBy } from "@std/text/unstable-trim-by";
 * import { assertEquals } from "@std/assert";
 *
 * const result = trimBy("/path/to/file/", "/");
 * assertEquals(result, "path/to/file");
 * ```
 *
 * @example Remove leading and trailing line breaks
 * ```ts
 * import { trimBy } from "@std/text/unstable-trim-by";
 * import { assertEquals } from "@std/assert";
 *
 * const result = trimBy("\r\nHello, World!\r\n", ["\n", "\r"]);
 * assertEquals(result, "Hello, World!");
 * ```
 *
 * @example Strip non-word characters from start and end of a string
 * ```ts
 * import { trimBy } from "@std/text/unstable-trim-by";
 * import { assertEquals } from "@std/assert";
 *
 * const result = trimBy("¡¿Seguro que no?!", /[^\p{L}\p{M}\p{N}]/u);
 * assertEquals(result, "Seguro que no");
 * ```
 */
export function trimBy(
  str: string,
  pattern: TrimPattern,
): string {
  return trimStartBy(trimEndBy(str, pattern), pattern);
}

/**
 * Trims all instances of the specified pattern at the start of the string.
 *
 * @experimental **UNSTABLE**: New API, yet to be vetted.
 *
 * @param str The input string
 * @param pattern The pattern to trim
 * @returns The trimmed input string
 *
 * @example Remove leading byte-order marks
 * ```ts
 * import { trimStartBy } from "@std/text/unstable-trim-by";
 * import { assertEquals } from "@std/assert";
 *
 * const result = trimStartBy("\ufeffhello world", "\ufeff");
 * assertEquals(result, "hello world");
 * ```
 *
 * @example Remove leading "http://" or "https://" from a URL
 * ```ts
 * import { trimStartBy } from "@std/text/unstable-trim-by";
 * import { assertEquals } from "@std/assert";
 *
 * const result = trimStartBy("https://example.com", ["http://", "https://"]);
 * assertEquals(result, "example.com");
 * ```
 *
 * @example Remove leading numbers from a string
 * ```ts
 * import { trimStartBy } from "@std/text/unstable-trim-by";
 * import { assertEquals } from "@std/assert";
 *
 * const result = trimStartBy("123abc456", /[0-9]+/);
 * assertEquals(result, "abc456");
 * ```
 */
export function trimStartBy(
  str: string,
  pattern: TrimPattern,
): string {
  return trimUntilDone(str, regExpFromTrimPattern`^${pattern}`);
}

/**
 * Trims all instances of the specified pattern at the end of the string.
 *
 * @experimental **UNSTABLE**: New API, yet to be vetted.
 *
 * @param str The input string
 * @param pattern The pattern to trim
 * @returns The trimmed input string
 *
 * @example Remove trailing period from a string
 * ```ts
 * import { trimEndBy } from "@std/text/unstable-trim-by";
 * import { assertEquals } from "@std/assert";
 *
 * const result = trimEndBy("Hello, World.", ".");
 * assertEquals(result, "Hello, World");
 * ```
 *
 * @example Remove trailing line endings
 * ```ts
 * import { trimEndBy } from "@std/text/unstable-trim-by";
 * import { assertEquals } from "@std/assert";
 *
 * const result = trimEndBy("file contents\n", ["\r", "\n"]);
 * assertEquals(result, "file contents");
 * ```
 *
 * @example Remove trailing whitespace characters
 * ```ts
 * import { trimEndBy } from "@std/text/unstable-trim-by";
 * import { assertEquals } from "@std/assert";
 *
 * const result = trimEndBy("  Hello, World!  ", /\s+/);
 * assertEquals(result, "  Hello, World!");
 * ```
 */
export function trimEndBy(
  str: string,
  pattern: TrimPattern,
): string {
  return trimUntilDone(str, regExpFromTrimPattern`${pattern}$`);
}

function trimUntilDone(str: string, regex: RegExp): string {
  let current = str;
  let next;
  while ((next = current.replace(regex, "")) !== current) current = next;
  return current;
}

function regExpFromTrimPattern(t: TemplateStringsArray, pattern: TrimPattern) {
  let { source, flags } = pattern instanceof RegExp ? pattern : {
    source: `${
      typeof pattern === "string" ? escape(pattern) : [...pattern]
        .sort((a, b) => b.length - a.length)
        .map(escape)
        .join("|")
    }`,
    flags: "",
  };

  source = `${t[0]!}(?:${source})${t[1]!}`;
  // remove any stateful flags to avoid `lastIndex` issues
  flags = flags.replace(/[gy]+/g, "");

  return new RegExp(source, flags);
}