Skip to content

Commit

Permalink
Merge pull request #202 from sasjs/getOSSpecificPath
Browse files Browse the repository at this point in the history
feat: add utility getOSSpecificPath
  • Loading branch information
allanbowe authored Sep 19, 2022
2 parents 71caf32 + a067e8b commit fb5bb3e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { asyncForEach, uuidv4, isWindows, isLinux } from './utils'
import {
asyncForEach,
uuidv4,
isWindows,
isLinux,
escapeWinSlashes
} from './utils'
import * as utilsModule from './utils'

describe('uuidv4', () => {
it('should generate 10000 uniq UUID', () => {
const uuids = []
const uuids: string[] = []

for (let i = 0; i < 10 * 1000; i++) {
const uuid = uuidv4()
Expand Down Expand Up @@ -39,3 +46,21 @@ describe('isLinux', () => {
expect(isLinux()).toEqual(process.platform === 'linux')
})
})

describe('escapeWinSlashes', () => {
describe('when platform is win32', () => {
it('should return the path with double backslashes', () => {
jest.spyOn(utilsModule, 'isWindows').mockImplementationOnce(() => true)
const path = 'some\\file\\path'
expect(escapeWinSlashes(path)).toEqual('some\\\\file\\\\path')
})
})

describe('when platform is not win32', () => {
it('should return the path as it is ', () => {
jest.spyOn(utilsModule, 'isWindows').mockImplementationOnce(() => false)
const path = 'some/file/path'
expect(escapeWinSlashes(path)).toEqual(path)
})
})
})
6 changes: 6 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ export const uniqArray = (data: any[]) => Array.from(new Set(data))
export const isWindows = () => process.platform === 'win32'

export const isLinux = () => process.platform === 'linux'

/**
* replace single backslash with double backslash
*/
export const escapeWinSlashes = (path: string) =>
isWindows() ? path.replace(/\\/g, '\\\\') : path

0 comments on commit fb5bb3e

Please sign in to comment.