Skip to content

Commit

Permalink
🤦
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperupcall committed Sep 28, 2023
1 parent 268cf61 commit c7b2d5e
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@ Commonly, [`.slice`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Ref
```js
// removePrefix
let str = 'greninja'
str.slice('gren'.length - 1)
<<<<<<< Updated upstream
str.slice('gre'.length)
// => ninja
=======
let substr = 'gren'
if (str.startsWith(substr)) {
str.slice(substr.length - 1)
// => ninja
}

>>>>>>> Stashed changes

// removeSuffix
let str = 'charizard'
str.slice(0, str.length - 'izard'.length)
// => char
let substr = 'izard'
if (str.endsWith(substr)) {
str.slice(0, str.length - 'substr'.length)
// => char
}
```

This approach is difficult to read (especially when nested/repeated) and not semantic.
Expand All @@ -39,14 +51,14 @@ let str = 'mudkip'
str.replace(/kip$/, '')
```

This isn't performant [jsPerf](https://jsperf.app/haxumu) and requires knowledge of regular expressions.
This isn't performant [jsPerf](https://jsperf.app/haxumu/2) and requires knowledge of regular expressions.

## Proposed Solution

We propose adding `String.prototype.removePrefix` and `String.prototype.removeSuffix` to the String prototype.

```js
'greninja'.removePrefix('gren')
'greninja'.removePrefix('gre')
// => ninja

'charizard'.removeSuffix('izard')
Expand Down

0 comments on commit c7b2d5e

Please sign in to comment.