trimEndBy

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

function ยท experimental

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

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

str: string

The input string

pattern: TrimPattern

The pattern to trim

Returns

The trimmed input string

Example: Remove trailing period from a string

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

const result = trimEndBy("Hello, World.", ".");
assertEquals(result, "Hello, World");

Example: Remove trailing line endings

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

const result = trimEndBy("file contents\n", ["\r", "\n"]);
assertEquals(result, "file contents");

Example: Remove trailing whitespace characters

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

const result = trimEndBy("  Hello, World!  ", /\s+/);
assertEquals(result, "  Hello, World!");

Source