Skip to content

Commit

Permalink
refactor(lib)!: [toPosix] skip delimiters
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Sep 22, 2024
1 parent 5c6035d commit 5ef0893
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 37 deletions.
34 changes: 17 additions & 17 deletions src/lib/__snapshots__/to-posix.snap
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (0) 1`] = `"C:."`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (0) 1`] = `"C:."`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (1) 1`] = `"C:/"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (1) 1`] = `"C:/"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (2) 1`] = `"C:"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (2) 1`] = `"C:"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (3) 1`] = `"C:/Windows/system32:C:/Windows:C:/Program Files/node/"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (3) 1`] = `"C:/Windows/system32;C:/Windows;C:/Program Files/node/"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (4) 1`] = `"C:/abc"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (4) 1`] = `"C:/abc"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (5) 1`] = `"C:/another_path/DIR/1/2/33//index"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (5) 1`] = `"C:/another_path/DIR/1/2/33//index"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (6) 1`] = `"C:/path/dir/index.html"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (6) 1`] = `"C:/path/dir/index.html"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (7) 1`] = `"C:abc"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (7) 1`] = `"C:abc"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (8) 1`] = `"//?/UNC"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (8) 1`] = `"//?/UNC"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (9) 1`] = `"//?/UNC/server/share"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (9) 1`] = `"//?/UNC/server/share"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (10) 1`] = `"//server two/shared folder/file path.zip"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (10) 1`] = `"//server two/shared folder/file path.zip"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (11) 1`] = `"//server/share/file_path"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (11) 1`] = `"//server/share/file_path"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (12) 1`] = `"//user/admin$/system32"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (12) 1`] = `"//user/admin$/system32"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (13) 1`] = `"/foo/C:"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (13) 1`] = `"/foo/C:"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (14) 1`] = `"another_path/DIR with spaces/1/2/33/index"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (14) 1`] = `"another_path/DIR with spaces/1/2/33/index"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (15) 1`] = `"./file"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (15) 1`] = `"./file"`;

exports[`unit:lib/toPosix > should return posix-compliant \`path\` (16) 1`] = `"/"`;
exports[`unit:lib/toPosix > should return \`path\` with posix-compliant separators (16) 1`] = `"/"`;
2 changes: 1 addition & 1 deletion src/lib/__tests__/file-url-to-path.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('unit:lib/fileURLToPath', () => {
const result = testSubject(url)

// Expect
expect(result).to.eq(toPosix(fileURLToPath(url, { windows: false })))
expect(result).to.eq(fileURLToPath(url, { windows: false }))
})

it('should throw if `url` hostname is invalid', () => {
Expand Down
6 changes: 1 addition & 5 deletions src/lib/__tests__/path-to-file-url.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,11 @@ describe('unit:lib/pathToFileURL', () => {
['/€'],
['/🚀']
])('should return `path` as `file:` URL (%#)', path => {
// Arrange
const url: URL = pathToFileURL(path, { windows: false })

// Act
const result = testSubject(path)

// Expect
expect(result.href).to.eq(toPosix(url.href))
expect(result.pathname).to.eq(toPosix(url.pathname))
expect(result).to.eql(pathToFileURL(path, { windows: false }))
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/lib/__tests__/to-posix.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('unit:lib/toPosix', () => {
['another_path\\DIR with spaces\\1\\2\\33\\index'],
[dot + '\\file'],
[win32.sep]
])('should return posix-compliant `path` (%#)', path => {
])('should return `path` with posix-compliant separators (%#)', path => {
expect(testSubject(path)).toMatchSnapshot()
})
})
16 changes: 3 additions & 13 deletions src/lib/to-posix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,22 @@
*/

import validateString from '#internal/validate-string'
import delimiter from './delimiter'
import sep from './sep'

/**
* Make `path` POSIX-compliant.
*
* This includes:
*
* - Converting Windows-style path delimiters (`;`) to POSIX (`:`)
* - Converting Windows-style path segment separators (`\`) to POSIX (`/`)
*
* @see https://nodejs.org/api/path.html#windows-vs-posix
* @see https://nodejs.org/api/path.html#pathdelimiter
* @see https://nodejs.org/api/path.html#pathsep
* Make separators in `path` POSIX-compliant.
*
* @category
* utils
*
* @param {string} path
* Path to handle
* @return {string}
* POSIX-compliant `path`
* `path` with POSIX-compliant separators
*/
function toPosix(path: string): string {
validateString(path, 'path')
return path.replace(/;/g, delimiter).replace(/\\/g, sep)
return path.replace(/\\/g, sep)
}

export default toPosix

0 comments on commit 5ef0893

Please sign in to comment.