Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(text/unstable): add replaceStart, replaceEnd, replaceBoth functions (#6265) #6286

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions text/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"./compare-similarity": "./compare_similarity.ts",
"./levenshtein-distance": "./levenshtein_distance.ts",
"./unstable-slugify": "./unstable_slugify.ts",
"./unstable-strip": "./unstable_strip.ts",
"./to-camel-case": "./to_camel_case.ts",
"./unstable-to-constant-case": "./unstable_to_constant_case.ts",
"./to-kebab-case": "./to_kebab_case.ts",
Expand Down
161 changes: 161 additions & 0 deletions text/unstable_strip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { escape } from "@std/regexp";

export type StripOptions = {
/**
* The number of occurrences to strip.
* @default {Infinity}
*/
count?: number;
};

/**
* Strips the specified pattern from 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 strip from the input
* @param options Options for the strip operation
*
* @example Strip using a string
* ```ts
* import { strip } from "@std/text/unstable-strip";
* import { assertEquals } from "@std/assert";
* assertEquals(strip("---x---", "-"), "x");
* ```
*
* @example Strip using a regexp
* ```ts
* import { strip } from "@std/text/unstable-strip";
* import { assertEquals } from "@std/assert";
* assertEquals(strip("-_-x-_-", /[-_]/), "x");
* ```
*
* @example Strip a specified count
* ```ts
* import { strip } from "@std/text/unstable-strip";
* import { assertEquals } from "@std/assert";
* assertEquals(strip("---x---", "-", { count: 1 }), "--x--");
* ```
*/
export function strip(
str: string,
pattern: string | RegExp,
options?: StripOptions,
): string {
const { source, flags } = cloneAsStatelessRegExp(pattern);
return stripByRegExp(
stripByRegExp(str, new RegExp(`^${source}`, flags), options),
new RegExp(`${source}$`, flags),
options,
);
}

/**
* Strips the specified pattern from the start of the string.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @param str The input string
* @param pattern The pattern to strip from the input
* @param options Options for the strip operation
*
* @example Strip using a string
* ```ts
* import { stripStart } from "@std/text/unstable-strip";
* import { assertEquals } from "@std/assert";
* assertEquals(stripStart("---x---", "-"), "x---");
* ```
*
* @example Strip using a regexp
* ```ts
* import { stripStart } from "@std/text/unstable-strip";
* import { assertEquals } from "@std/assert";
* assertEquals(stripStart("-_-x-_-", /[-_]/), "x-_-");
* ```
*
* @example Strip a specified count
* ```ts
* import { stripStart } from "@std/text/unstable-strip";
* import { assertEquals } from "@std/assert";
* assertEquals(stripStart("---x---", "-", { count: 1 }), "--x---");
* ```
*/
export function stripStart(
str: string,
pattern: string | RegExp,
options?: StripOptions,
): string {
const { source, flags } = cloneAsStatelessRegExp(pattern);
return stripByRegExp(str, new RegExp(`^${source}`, flags), options);
}

/**
/**
* Strips the specified pattern from the start of the string.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @param str The input string
* @param pattern The pattern to strip from the input
* @param options Options for the strip operation
*
* @example Strip using a string
* ```ts
* import { stripEnd } from "@std/text/unstable-strip";
* import { assertEquals } from "@std/assert";
* assertEquals(stripEnd("---x---", "-"), "---x");
* ```
*
* @example Strip using a regexp
* ```ts
* import { stripEnd } from "@std/text/unstable-strip";
* import { assertEquals } from "@std/assert";
* assertEquals(stripEnd("-_-x-_-", /[-_]/), "-_-x");
* ```
*
* @example Strip a specified count
* ```ts
* import { stripEnd } from "@std/text/unstable-strip";
* import { assertEquals } from "@std/assert";
* assertEquals(stripEnd("---x---", "-", { count: 1 }), "---x--");
* ```
*/
export function stripEnd(
str: string,
pattern: string | RegExp,
options?: StripOptions,
): string {
const { source, flags } = cloneAsStatelessRegExp(pattern);
return stripByRegExp(str, new RegExp(`${source}$`, flags), options);
}

function stripByRegExp(
str: string,
regExp: RegExp,
options?: StripOptions,
): string {
let prev = str;

const count = options?.count ?? Infinity;

for (let i = 0; i < count; ++i) {
str = str.replace(regExp, "");
if (str === prev) break;
prev = str;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Infinity? If the string doesn't include the pattern to strip, it will loop forever, won't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Infinity? If the string doesn't include the pattern to strip, it will loop forever, won't it?

It'll break on the str === prev condition as soon as there's nothing left to strip.

const aaa = 'a'.repeat(1000)
const aab = aaa + 'b'
const bbb = 'b'.repeat(1000)

stripStart(aaa, 'a') === ''
stripStart(aab, 'a') === 'b'
stripStart(bbb, 'a') === bbb


return str;
}

function cloneAsStatelessRegExp(pattern: string | RegExp) {
return {
source: typeof pattern === "string" ? escape(pattern) : pattern.source,
flags: typeof pattern === "string" ? "" : getStatelessFlags(pattern),
};
}

function getStatelessFlags(re: RegExp): string {
return re.flags.replaceAll(/[gy]/g, "");
}
72 changes: 72 additions & 0 deletions text/unstable_strip_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { assertEquals } from "@std/assert";
import { strip, stripEnd, stripStart } from "./unstable_strip.ts";

Deno.test("stripStart()", async (t) => {
await t.step("strips prefixes", () => {
assertEquals(stripStart("..x.x..", "."), "x.x..");
});

await t.step("strips prefixes with count", () => {
assertEquals(stripStart("..x.x..", ".", { count: 0 }), "..x.x..");
assertEquals(stripStart("..x.x..", ".", { count: 1 }), ".x.x..");
assertEquals(stripStart("..x.x..", ".", { count: 2 }), "x.x..");
assertEquals(stripStart("..x.x..", ".", { count: 3 }), "x.x..");
});

await t.step("strips multi-char prefixes", () => {
assertEquals(stripStart("._.._._.x.x._._.._.", "._."), "_.x.x._._.._.");
});

await t.step("strips prefixes by regex pattern", () => {
assertEquals(stripStart("!@#$%x.x!@#$%", /./), "");
assertEquals(
stripStart("!@#$%x.x!@#$%", /[^\p{L}\p{M}\p{N}]+/u),
"x.x!@#$%",
);
});
});

Deno.test("stripEnd()", async (t) => {
await t.step("strips suffixes", () => {
assertEquals(stripEnd("..x.x..", "."), "..x.x");
});

await t.step("strips suffixes with a specific count", () => {
assertEquals(stripEnd("..x.x..", ".", { count: 0 }), "..x.x..");
assertEquals(stripEnd("..x.x..", ".", { count: 1 }), "..x.x.");
assertEquals(stripEnd("..x.x..", ".", { count: 2 }), "..x.x");
assertEquals(stripEnd("..x.x..", ".", { count: 3 }), "..x.x");
});

await t.step("strips multi-char suffixes", () => {
assertEquals(stripEnd("._.._._.x.x._._.._.", "._."), "._.._._.x.x._");
});

await t.step("strips suffixes by regex pattern", () => {
assertEquals(stripEnd("!@#$%x.x!@#$%", /./), "");
assertEquals(stripEnd("!@#$%x.x!@#$%", /[^\p{L}\p{M}\p{N}]+/u), "!@#$%x.x");
});
});

Deno.test("strip()", async (t) => {
await t.step("strips prefixes and suffixes", () => {
assertEquals(strip("..x.x..", "."), "x.x");
});

await t.step("strips prefixes and suffixes with a specific count", () => {
assertEquals(strip("..x.x..", ".", { count: 0 }), "..x.x..");
assertEquals(strip("..x.x..", ".", { count: 1 }), ".x.x.");
assertEquals(strip("..x.x..", ".", { count: 2 }), "x.x");
assertEquals(strip("..x.x..", ".", { count: 3 }), "x.x");
});

await t.step("strips multi-char prefixes and suffixes", () => {
assertEquals(strip("._.._._.x.x._._.._.", "._."), "_.x.x._");
});

await t.step("strips prefixes and suffixes by regex pattern", () => {
assertEquals(strip("!@#$%x.x!@#$%", /./), "");
assertEquals(strip("!@#$%x.x!@#$%", /[^\p{L}\p{M}\p{N}]+/u), "x.x");
});
});
Loading