Skip to content

Commit

Permalink
feat(string): add toBackSlashes
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi committed Aug 26, 2023
1 parent e9157f6 commit 554ca3f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/string.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, it } from 'vitest'
import { capitalize, ensureStartsWith, ensureEndsWith, slash, template } from './string'
import { capitalize, ensureStartsWith, ensureEndsWith, toForwardSlashes, template, toBackSlashes } from './string'

it('template', () => {
expect(
Expand Down Expand Up @@ -96,10 +96,16 @@ it('namedTemplate', () => {
).toEqual('2 4 6 known key')
})

it('slash', () => {
expect(slash('\\123')).toEqual('/123')
expect(slash('\\\\')).toEqual('//')
expect(slash('\\\h\\\i')).toEqual('/h/i')
it('toForwardSlashes', () => {
expect(toForwardSlashes('\\123')).toEqual('/123')
expect(toForwardSlashes('\\\\')).toEqual('//')
expect(toForwardSlashes('\\\h\\\i')).toEqual('/h/i')
})

it('toBackSlashes', () => {
expect(toBackSlashes('/123')).toEqual('\\123')
expect(toBackSlashes('//')).toEqual('\\\\')
expect(toBackSlashes('/\h/\i')).toEqual('\\h\\i')
})

it('ensurePrefix', () => {
Expand Down
11 changes: 10 additions & 1 deletion src/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@ import { isObject } from './is'
*
* @category String
*/
export function slash(str: string) {
export function toForwardSlashes(str: string) {
return str.replace(/\\/g, '/')
}

/**
* Replaces forward slashes with backslashes.
*
* @category String
*/
export function toBackSlashes(str: string) {
return str.replace(/\//g, '\\')
}

/**
* Ensures the given string starts with the given prefix.
*
Expand Down

0 comments on commit 554ca3f

Please sign in to comment.