Skip to content

Commit

Permalink
feat!: trim now requires both arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Feb 4, 2025
1 parent 8e3b1c8 commit 26f1260
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions docs/string/trim.mdx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
---
title: trim
description: Trim values from a string
description: Remove characters from the ends of a string
since: 12.1.0
---

### Usage

Trims all prefix and suffix characters from the given string. Like the builtin trim function but accepts alternate (other than space) characters you would like to trim.
Trim specific characters from the start and end of a given string. Like the built-in `String.prototype.trim` method, but it accepts alternate (other than space) characters you would like to trim.

```ts
import * as _ from 'radashi'

_.trim(' hello ') // => hello
_.trim(' hello ', ' ') // => hello
_.trim('__hello__', '_') // => hello
_.trim('/repos/:owner/', '/') // => repos/:owner
```

Trim also handles more than one character to trim.
It also handles more than one character to trim.

```ts
_.trim('222__hello__111', '12_') // => hello
Expand Down
2 changes: 1 addition & 1 deletion src/string/trim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* ```
* @version 12.1.0
*/
export function trim(str: string, charsToTrim = ' '): string {
export function trim(str: string, charsToTrim: string): string {
const toTrim = charsToTrim.replace(/[\W]{1}/g, '\\$&')
const regex = new RegExp(`^[${toTrim}]+|[${toTrim}]+$`, 'g')
return str.replace(regex, '')
Expand Down
2 changes: 1 addition & 1 deletion tests/string/trim.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('trim', () => {
test('returns input string correctly trimmed', () => {
expect(_.trim('\n\n\t\nhello\n\t \n', '\n\t ')).toBe('hello')
expect(_.trim('hello', 'x')).toBe('hello')
expect(_.trim(' hello ')).toBe('hello')
expect(_.trim(' hello ', ' ')).toBe('hello')
expect(_.trim(' __hello__ ', '_')).toBe(' __hello__ ')
expect(_.trim('__hello__', '_')).toBe('hello')
expect(_.trim('//repos////', '/')).toBe('repos')
Expand Down

0 comments on commit 26f1260

Please sign in to comment.