-
Notifications
You must be signed in to change notification settings - Fork 631
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
Suggestion: stripPrefix/stripSuffix functions #6265
Comments
I think this is a special case and is not too difficult to achieve natively. It can be partially done with String.prototype.replace: string.replace(/^str/, "") // Note: returns whole string if nothing is replaced
string.replace(/^ing/, "") // Note: returns whole string if nothing is replaced Alternatively String.prototype.startsWith and String.prototype.endsWith can also be used: const startStripped = string.startsWith("str") ? value.slice(3) : null
const endStripped = string.startsWith("ing") ? value.slice(0, -3) : null So I think this is too trivial and should not be abstracted one-liner in std. |
@timreichen I'm trying to argue that it's a useful function that I've found myself wanting multiple times. I'm not saying that it's not trivial to implement. I agree with you there. |
I see. What I meant was that if it is just one line that gets abstracted, it probably should not be in std imo. I don't know though where std draws the line of too trivial/ too short etc. |
The two functions in |
I wonder if a better/more versatile abstraction is import { strip } from 'jsr:@std/text'
assertEquals(strip('string', 'str'), 'ing')
assertEquals(strip('string', 'ing'), 'str')
assertEquals(strip('string', 'ing', { suffix: true }), 'str')
assertEquals(strip('string', 'ing', { prefix: true }), 'string')
assertEquals(strip('string', 'str', { suffix: true }), 'string')
assertEquals(strip('string', 'str', { prefix: true }), 'ing')
assertEquals(strip('sstringss', 's'), 'tring')
assertEquals(strip('sstringss', 's', { prefix: 1, suffix: 1 }), 'strings') Maybe const input = '#&(*@&abc*&($#$'
// stripping non-word chars without `strip` (v1, unreadable and error-prone)
input.replaceAll(/^[^\p{L}\p{M}\p{N}]+|[^\p{L}\p{M}\p{N}]+$/gu, '')
// stripping non-word chars without `strip`
// (v2, requires assigning a variable and recreating the regex manually)
const re = /[^\p{L}\p{M}\p{N}]+/gu
input
.replaceAll(new RegExp(`^(?:${re.source})`, re.flags), '')
.replaceAll(new RegExp(`(?:${re.source})$`, re.flags), '')
// stripping non-word chars with `strip`
strip(input, /[^\p{L}\p{M}\p{N}]+/u) |
I think |
@timreichen Maybe all 3 then: |
@timreichen Good suggestion about @lionel-rowe The nice thing about the Rust versions is that they return |
That feels very rust-ey but not very JS-ey, and also further away from the |
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
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.
The text was updated successfully, but these errors were encountered: