-
Notifications
You must be signed in to change notification settings - Fork 636
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
lionel-rowe
wants to merge
4
commits into
denoland:main
Choose a base branch
from
lionel-rowe:strip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+298
−0
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
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, ""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'll break on the
str === prev
condition as soon as there's nothing left to strip.