trimBy

import { trimBy } from "jsr:@ende/text/unstable-trim-by";

function · experimental

function trimBy(str: string, pattern: TrimPattern): string

Trims all instances of the specified pattern at the start and end of the string.

str: string

The input string

pattern: TrimPattern

The pattern to trim

Returns

The trimmed input string

Example: Remove slashes from start and end of a path

import { trimBy } from "@ende/text/unstable-trim-by";
import { assertEquals } from "@ende/assert";

const result = trimBy("/path/to/file/", "/");
assertEquals(result, "path/to/file");

Example: Remove leading and trailing line breaks

import { trimBy } from "@ende/text/unstable-trim-by";
import { assertEquals } from "@ende/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

import { trimBy } from "@ende/text/unstable-trim-by";
import { assertEquals } from "@ende/assert";

const result = trimBy("¡¿Seguro que no?!", /[^\p{L}\p{M}\p{N}]/u);
assertEquals(result, "Seguro que no");

Source