Skip to content

Commit

Permalink
Merge pull request #8 from storyblok/EXT-1526-trim-slashes-always-ret…
Browse files Browse the repository at this point in the history
…urn-string

refactor: trimSlashes returns string
  • Loading branch information
johannes-lindgren authored May 10, 2023
2 parents 3fe4d06 + 3ffb970 commit 0f07527
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/storyblok-auth-api/refreshToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { token_endpoint } from './storyblok-oauth-api-endpoints'
import { hasKey } from '../utils'
import { trimSlashes } from '../utils/trimSlashes/trimSlashes'
import { hasKey, trimSlashes } from '../utils'
import { AuthHandlerParams } from './AuthHandlerParams'

export type RefreshTokenWithFetchParams = Pick<
Expand Down
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './hasKey'
export * from './GetCookie'
export * from './SetCookie'
export * from './numberFromString'
export * from './trimSlashes'
1 change: 1 addition & 0 deletions src/utils/trimSlashes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './trimSlashes'
9 changes: 9 additions & 0 deletions src/utils/trimSlashes/trimSlashes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ describe('trimSlashes', () => {
'hello/my/fellow/bloker',
)
})
it('should trim double slashes', () => {
expect(trimSlashes('//hello//')).toEqual('hello')
})
it('should trim a slash character to an empty string', () => {
expect(trimSlashes('/')).toEqual('')
})
it('should trim multiple slash characters to an empty string', () => {
expect(trimSlashes('//')).toEqual('')
})
})
8 changes: 6 additions & 2 deletions src/utils/trimSlashes/trimSlashes.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export const trimSlashes = (slugs: string): string | undefined =>
slugs.match(/^\/*(.*?)\/*$/)?.[1] ?? undefined
/**
* Removes leading and trailing slashes (`/`) from a string.
* @param slugs
*/
export const trimSlashes = (slugs: string): string =>
slugs.match(/^\/*(.*?)\/*$/)?.[1] ?? ''

0 comments on commit 0f07527

Please sign in to comment.