Skip to content

Commit

Permalink
improve trim docs and tests (#225)
Browse files Browse the repository at this point in the history
Co-authored-by: Ray Epps <[email protected]>
  • Loading branch information
sodiray and Ray Epps authored Jan 5, 2023
1 parent 8949bf9 commit 7022f29
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
14 changes: 10 additions & 4 deletions docs/string/trim.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ group: String

## Basic usage

Trims all prefix and suffix characters from the given string. Like the builtin trim function but accepts other characters you would like to trim.
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.

```ts
import { trim } from 'radash'

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

Trim also handles more than one character to trim.

```ts
trim('222__hello__111', '12_') // => hello
```
4 changes: 3 additions & 1 deletion src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ export const template = (
/**
* Trims all prefix and suffix characters from the given
* string. Like the builtin trim function but accepts
* other characters you would like to trim.
* other characters you would like to trim and trims
* multiple characters.
*
* ```typescript
* trim(' hello ') // => 'hello'
* trim('__hello__', '_') // => 'hello'
* trim('/repos/:owner/:repo/', '/') // => 'repos/:owner/:repo'
* trim('222222__hello__1111111', '12_') // => 'hello'
* ```
*/
export const trim = (
Expand Down
1 change: 1 addition & 0 deletions src/tests/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ describe('string module', () => {
assert.equal(_.trim(undefined), '')
})
test('returns input string correctly trimmed', () => {
assert.equal(_.trim('\n\n\t\nhello\n\t \n', '\n\t '), 'hello')
assert.equal(_.trim('hello', 'x'), 'hello')
assert.equal(_.trim(' hello '), 'hello')
assert.equal(_.trim(' __hello__ ', '_'), ' __hello__ ')
Expand Down

1 comment on commit 7022f29

@vercel
Copy link

@vercel vercel bot commented on 7022f29 Jan 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.