Skip to content

Commit

Permalink
feat(lib): fileURLToPath, pathToFileURL
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 587e890 commit 5c6035d
Show file tree
Hide file tree
Showing 27 changed files with 799 additions and 29 deletions.
4 changes: 0 additions & 4 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ flags:
carryforward: false
paths:
- src/
node20:
carryforward: false
paths:
- src/

github_checks:
annotations: true
Expand Down
6 changes: 6 additions & 0 deletions .dictionary.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
abar
attw
barx
cbar
cefc
codecov
commitlintrc
dbar
dedupe
deno
dessant
devlop
docast
dohm
dprint
fbar
fbca
fóóbàr
ggshield
gpgsign
hmarr
Expand All @@ -35,4 +40,5 @@ unstub
vates
vfile
vitest
whatwg
yarnrc
1 change: 0 additions & 1 deletion .github/infrastructure.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ branches:
- context: gitguardian
- context: lint
- context: spelling
- context: test (20)
- context: test (22)
- context: typescript (5.0.4)
- context: typescript (5.4.5)
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ jobs:
matrix:
node-version:
- 22
- 20
steps:
- id: checkout
name: Checkout ${{ env.REF_NAME }}
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import {
dirname,
dot,
extname,
fileURLToPath,
format,
formatExt,
isAbsolute,
Expand All @@ -102,6 +103,7 @@ import {
matchesGlob,
normalize,
parse,
pathToFileURL,
relative,
removeExt,
resolve,
Expand All @@ -126,6 +128,7 @@ This package exports the following identifiers:
- [`dot`](./src/lib/dot.ts)
- [`extname`](./src/lib/extname.ts)
- [`extnames`](./src/lib/extnames.ts)
- [`fileURLToPath`](./src/lib/file-url-to-path.ts)
- [`formatExt`](./src/lib/format-ext.ts)
- [`format`](./src/lib/format.ts)
- [`isAbsolute`](./src/lib/is-absolute.ts)
Expand All @@ -135,6 +138,7 @@ This package exports the following identifiers:
- [`matchesGlob`](./src/lib/matches-glob.ts)
- [`normalize`](./src/lib/normalize.ts)
- [`parse`](./src/lib/parse.ts)
- [`pathToFileURL`](./src/lib/path-to-file-url.ts)
- [`posix`](./src/pathe.ts)
- [`relative`](./src/lib/relative.ts)
- [`removeExt`](./src/lib/remove-ext.ts)
Expand Down Expand Up @@ -164,13 +168,14 @@ This package is fully typed with [TypeScript][].
- [`FormatInputPathObject`](src/interfaces/format-input-path-object.ts)
- [`ParsedPath`](src/interfaces/parsed-path.ts)
- [`Pathe`](src/interfaces/pathe.ts)
- [`PlatformPath`](src/interfaces/platform-path-posix.ts)
- [`PlatformOptions`](src/interfaces/platform-options.ts)
- [`PlatformPath`](src/interfaces/platform-path.ts)
- [`PosixDelimiter`](src/types/delimiter-posix.ts)
- [`PosixPlatformPath`](src/interfaces/platform-path-windows.ts)
- [`PosixPlatformPath`](src/interfaces/platform-path-posix.ts)
- [`PosixSep`](src/types/sep-posix.ts)
- [`Sep`](src/types/sep.ts)
- [`WindowsDelimiter`](src/types/delimiter-windows.ts)
- [`WindowsPlatformPath`](src/interfaces/platform-path.ts)
- [`WindowsPlatformPath`](src/interfaces/platform-path-windows.ts)
- [`WindowsSep`](src/types/sep-windows.ts)

## Contribute
Expand Down
15 changes: 14 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@
"./win32": "./dist/win32.mjs"
},
"imports": {
"#internal/domain-to-ascii": {
"pathe": "./src/internal/domain-to-ascii.node.ts",
"browser": "./dist/internal/domain-to-ascii.browser.mjs",
"node": "./dist/internal/domain-to-ascii.node.mjs",
"default": "./dist/internal/domain-to-ascii.node.mjs"
},
"#internal/domain-to-unicode": {
"pathe": "./src/internal/domain-to-unicode.node.ts",
"browser": "./dist/internal/domain-to-unicode.browser.mjs",
"node": "./dist/internal/domain-to-unicode.node.mjs",
"default": "./dist/internal/domain-to-unicode.node.mjs"
},
"#internal/process": {
"browser": "./dist/internal/process.browser.mjs",
"node": "process",
Expand Down Expand Up @@ -113,7 +125,8 @@
"dependencies": {
"@flex-development/errnode": "3.1.1",
"@types/micromatch": "4.0.9",
"micromatch": "4.0.8"
"micromatch": "4.0.8",
"punycode.js": "2.3.1"
},
"devDependencies": {
"@arethetypeswrong/cli": "0.16.4",
Expand Down
2 changes: 2 additions & 0 deletions src/__snapshots__/index.e2e.snap
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ exports[`e2e:pathe > should expose public api 1`] = `
"dot",
"extname",
"extnames",
"fileURLToPath",
"format",
"formatExt",
"isAbsolute",
Expand All @@ -20,6 +21,7 @@ exports[`e2e:pathe > should expose public api 1`] = `
"matchesGlob",
"normalize",
"parse",
"pathToFileURL",
"relative",
"removeExt",
"resolve",
Expand Down
15 changes: 15 additions & 0 deletions src/interfaces/__tests__/platform-options.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* @file Unit Tests - PlatformOptions
* @module pathe/interfaces/tests/unit-d/PlatformOptions
*/

import type { Nilable } from '@flex-development/tutils'
import type TestSubject from '../platform-options'

describe('unit-d:interfaces/PlatformOptions', () => {
it('should match [windows?: boolean | null | undefined]', () => {
expectTypeOf<TestSubject>()
.toHaveProperty('windows')
.toEqualTypeOf<Nilable<boolean>>()
})
})
1 change: 1 addition & 0 deletions src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type {
} from './format-input-path-object'
export type { default as ParsedPath } from './parsed-path'
export type { default as Pathe } from './pathe'
export type { default as PlatformOptions } from './platform-options'
export type { default as PlatformPath } from './platform-path'
export type { default as PosixPlatformPath } from './platform-path-posix'
export type { default as WindowsPlatformPath } from './platform-path-windows'
88 changes: 75 additions & 13 deletions src/interfaces/pathe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
*/

import type extname from '#lib/extname'
import type {
ErrInvalidArgType,
ErrInvalidArgValue,
ErrInvalidFileUrlHost,
ErrInvalidFileUrlPath,
ErrInvalidUrlScheme
} from '@flex-development/errnode'
import type {
Cwd,
DeviceRoot,
Expand All @@ -12,6 +19,7 @@ import type {
Ext,
Sep
} from '@flex-development/pathe'
import type PlatformOptions from './platform-options'
import type PosixPlatformPath from './platform-path-posix'

/**
Expand Down Expand Up @@ -70,19 +78,6 @@ interface Pathe extends PosixPlatformPath {
*/
readonly dot: Dot

/**
* Format a file extension.
*
* @see {@linkcode EmptyString}
* @see {@linkcode Ext}
*
* @param {string | null | undefined} ext
* File extension to format
* @return {EmptyString | Ext}
* Formatted file extension or empty string
*/
formatExt(this: void, ext: string | null | undefined): EmptyString | Ext

/**
* Get a list of file extensions for `path`.
*
Expand All @@ -96,6 +91,44 @@ interface Pathe extends PosixPlatformPath {
*/
extnames(path: string): Ext[]

/**
* Convert a `file:` URL to a path.
*
* @see {@linkcode ErrInvalidArgType}
* @see {@linkcode ErrInvalidFileUrlHost}
* @see {@linkcode ErrInvalidFileUrlPath}
* @see {@linkcode ErrInvalidUrlScheme}
* @see {@linkcode PlatformOptions}
*
* @param {URL | string} url
* The file URL string or URL object to convert to a path
* @param {PlatformOptions | null | undefined} [options]
* Platform options
* @return {string}
* `url` as path
* @throws {ErrInvalidArgType}
* @throws {ErrInvalidFileUrlHost}
* @throws {ErrInvalidFileUrlPath}
* @throws {ErrInvalidUrlScheme}
*/
fileURLToPath(
url: URL | string,
options?: PlatformOptions | null | undefined
): string

/**
* Format a file extension.
*
* @see {@linkcode EmptyString}
* @see {@linkcode Ext}
*
* @param {string | null | undefined} ext
* File extension to format
* @return {EmptyString | Ext}
* Formatted file extension or empty string
*/
formatExt(this: void, ext: string | null | undefined): EmptyString | Ext

/**
* Check if `value` is a device root.
*
Expand All @@ -120,6 +153,35 @@ interface Pathe extends PosixPlatformPath {
*/
isSep(this: void, value: unknown): value is Sep

/**
* Convert a file `path` to a `file:` {@linkcode URL}.
*
* > The following characters are percent-encoded when converting from file
* > path to a `URL`:
* >
* > - %: Only character not encoded by the `pathname` setter
* > - CR: Stripped out by the `pathname` setter (see [`whatwg/url#419`][419])
* > - LF: Stripped out by the `pathname` setter (see [`whatwg/url#419`][419])
* > - TAB: Stripped out by the `pathname` setter
*
* [419]: https://github.com/whatwg/url/issues/419
*
* @see {@linkcode ErrInvalidArgValue}
* @see {@linkcode PlatformOptions}
*
* @param {URL | string} path
* Path to handle
* @param {PlatformOptions | null | undefined} [options]
* Platform options
* @return {URL}
* `path` as `file:` URL
* @throws {ErrInvalidArgValue}
*/
pathToFileURL(
path: string,
options?: PlatformOptions | null | undefined
): URL

/**
* Remove the file extension of `path`.
*
Expand Down
16 changes: 16 additions & 0 deletions src/interfaces/platform-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @file Interfaces - PlatformOptions
* @module pathe/lib/PlatformOptions
*/

/**
* Platform-specific options.
*/
interface PlatformOptions {
/**
* Use windows-specific logic.
*/
windows?: boolean | null | undefined
}

export type { PlatformOptions as default }
23 changes: 23 additions & 0 deletions src/internal/__tests__/is-url.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @file Unit Tests - isURL
* @module pathe/internal/tests/unit/isURL
*/

import testSubject from '../is-url'

describe('unit:internal/isURL', () => {
it.each<Parameters<typeof testSubject>>([
[null],
['https://github.com/flex-development/errnode'],
[{ href: 'file://host/a', path: '/a', pathname: '/a', protocol: 'file:' }]
])('should return `false` if `value` is not URL-like (%#)', value => {
expect(testSubject(value)).to.be.false
})

it.each<Parameters<typeof testSubject>>([
[new URL('https://github.com/flex-development/pathe')],
[{ href: 'file:///', pathname: '/', protocol: 'file:' }]
])('should return `true` if `value` is URL-like (%#)', value => {
expect(testSubject(value)).to.be.true
})
})
6 changes: 3 additions & 3 deletions src/internal/__tests__/validate-object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @module pathe/internal/tests/unit/validateObject
*/

import { codes, type ErrInvalidArgType } from '@flex-development/errnode'
import { codes, isNodeError, type NodeError } from '@flex-development/errnode'
import testSubject from '../validate-object'

describe('unit:internal/validateObject', () => {
Expand All @@ -19,7 +19,7 @@ describe('unit:internal/validateObject', () => {

it('should throw if `value` is not curly-braced object', () => {
// Arrange
let error!: ErrInvalidArgType
let error!: NodeError

// Act
try {
Expand All @@ -29,7 +29,7 @@ describe('unit:internal/validateObject', () => {
}

// Expect
expect(error).to.be.instanceof(TypeError)
expect(error).to.satisfy(isNodeError)
expect(error).to.have.property('code', codes.ERR_INVALID_ARG_TYPE)
})
})
6 changes: 3 additions & 3 deletions src/internal/__tests__/validate-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @module pathe/internal/tests/unit/validateString
*/

import { codes, type ErrInvalidArgType } from '@flex-development/errnode'
import { codes, isNodeError, type NodeError } from '@flex-development/errnode'
import testSubject from '../validate-string'

describe('unit:internal/validateString', () => {
Expand All @@ -19,7 +19,7 @@ describe('unit:internal/validateString', () => {

it('should throw if `value` is not a string', () => {
// Arrange
let error!: ErrInvalidArgType
let error!: NodeError

// Act
try {
Expand All @@ -29,7 +29,7 @@ describe('unit:internal/validateString', () => {
}

// Expect
expect(error).to.be.instanceof(TypeError)
expect(error).to.satisfy(isNodeError)
expect(error).to.have.property('code', codes.ERR_INVALID_ARG_TYPE)
})
})
6 changes: 6 additions & 0 deletions src/internal/domain-to-ascii.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @file Internal - domainToASCII
* @module pathe/internal/domainToASCII/browser
*/

export { toASCII as default } from 'punycode.js'
Loading

0 comments on commit 5c6035d

Please sign in to comment.