deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

Suggestion: stripPrefix/stripSuffix functions

Open canac opened this issue 10 months ago • 9 comments

Is your feature request related to a problem? Please describe.

The Rust standard library has strip_prefix/strip_suffix functions to remove a substring from the start or end of a string if it exists, or return None/null if the string doesn't have that substring at the start/end. I find myself wanting that function often when manipulating URLs or CLI output. Would that be a good fit for inclusion in @std? Maybe under @std/text? I would be happy to submit a PR.

Describe the solution you'd like

import { stripPrefix, stripSuffix } from 'jsr:@std/text';

assert(stripPrefix('string', 'str') === 'ing');
assert(stripPrefix('string', 'abc') === null);

assert(stripSuffix('string', 'ing') === 'str');
assert(stripSuffix('string', 'abc') === null);

Describe alternatives you've considered

Just writing a helper function whenever I need it in each project. I think it would be a generally useful feature though.

canac avatar Dec 13 '24 22:12 canac