Skip to content

Commit fb1cf06

Browse files
committed
feat: [toPosix] url support
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent 86497f4 commit fb1cf06

21 files changed

+254
-97
lines changed

.dictionary.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
abar
22
attw
33
barx
4+
cdir
45
cefc
6+
cindex
57
codecov
68
commitlintrc
79
dbar

src/interfaces/pathe.mts

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
import type extname from '#lib/extname'
77
import type {
8-
ErrInvalidArgType,
98
ErrInvalidArgValue,
109
ErrInvalidFileUrlHost,
1110
ErrInvalidFileUrlPath,
@@ -36,8 +35,10 @@ interface Pathe extends PosixPlatformPath {
3635
* Does nothing if a file extension is not provided, or the
3736
* {@linkcode extname} of `path` is already `ext`.
3837
*
38+
* @this {void}
39+
*
3940
* @param {string} path
40-
* Path to handle
41+
* The path to handle
4142
* @param {string | null | undefined} ext
4243
* File extension to add
4344
* @return {string}
@@ -52,8 +53,10 @@ interface Pathe extends PosixPlatformPath {
5253
* If the file extension is an empty string, however, `path`'s file extension
5354
* will be removed.
5455
*
56+
* @this {void}
57+
*
5558
* @param {string} path
56-
* Path to handle
59+
* The path to handle
5760
* @param {string | null | undefined} [ext]
5861
* File extension to add
5962
* @return {string} `path` unmodified or with changed file extension
@@ -64,6 +67,8 @@ interface Pathe extends PosixPlatformPath {
6467
/**
6568
* Get the path to the current working directory.
6669
*
70+
* @this {void}
71+
*
6772
* @return {string}
6873
* Absolute path to current working directory
6974
*/
@@ -84,34 +89,37 @@ interface Pathe extends PosixPlatformPath {
8489
* @see {@linkcode Ext}
8590
* @see {@linkcode extname}
8691
*
92+
* @this {void}
93+
*
8794
* @param {string} path
88-
* Path to handle
95+
* The path to handle
8996
* @return {Ext[]}
9097
* List of extensions
9198
*/
92-
extnames(path: string): Ext[]
99+
extnames(this: void, path: string): Ext[]
93100

94101
/**
95102
* Convert a `file:` URL to a path.
96103
*
97-
* @see {@linkcode ErrInvalidArgType}
98104
* @see {@linkcode ErrInvalidFileUrlHost}
99105
* @see {@linkcode ErrInvalidFileUrlPath}
100106
* @see {@linkcode ErrInvalidUrlScheme}
101107
* @see {@linkcode PlatformOptions}
102108
*
109+
* @this {void}
110+
*
103111
* @param {URL | string} url
104112
* The file URL string or URL object to convert to a path
105113
* @param {PlatformOptions | null | undefined} [options]
106114
* Platform options
107115
* @return {string}
108116
* `url` as path
109-
* @throws {ErrInvalidArgType}
110117
* @throws {ErrInvalidFileUrlHost}
111118
* @throws {ErrInvalidFileUrlPath}
112119
* @throws {ErrInvalidUrlScheme}
113120
*/
114121
fileURLToPath(
122+
this: void,
115123
url: URL | string,
116124
options?: PlatformOptions | null | undefined
117125
): string
@@ -122,6 +130,8 @@ interface Pathe extends PosixPlatformPath {
122130
* @see {@linkcode EmptyString}
123131
* @see {@linkcode Ext}
124132
*
133+
* @this {void}
134+
*
125135
* @param {string | null | undefined} ext
126136
* File extension to format
127137
* @return {EmptyString | Ext}
@@ -134,6 +144,8 @@ interface Pathe extends PosixPlatformPath {
134144
*
135145
* @see {@linkcode DeviceRoot}
136146
*
147+
* @this {void}
148+
*
137149
* @param {unknown} [value]
138150
* Value to check
139151
* @return {value is DeviceRoot}
@@ -146,6 +158,8 @@ interface Pathe extends PosixPlatformPath {
146158
*
147159
* @see {@linkcode Sep}
148160
*
161+
* @this {void}
162+
*
149163
* @param {unknown} [value]
150164
* Value to check
151165
* @return {value is Sep}
@@ -169,15 +183,18 @@ interface Pathe extends PosixPlatformPath {
169183
* @see {@linkcode ErrInvalidArgValue}
170184
* @see {@linkcode PlatformOptions}
171185
*
186+
* @this {void}
187+
*
172188
* @param {URL | string} path
173-
* Path to handle
189+
* The path to handle
174190
* @param {PlatformOptions | null | undefined} [options]
175191
* Platform options
176192
* @return {URL}
177193
* `path` as `file:` URL
178194
* @throws {ErrInvalidArgValue}
179195
*/
180196
pathToFileURL(
197+
this: void,
181198
path: string,
182199
options?: PlatformOptions | null | undefined
183200
): URL
@@ -188,8 +205,10 @@ interface Pathe extends PosixPlatformPath {
188205
* Does nothing if `path` does not end with the provided file extension, or if
189206
* a file extension is not provided.
190207
*
208+
* @this {void}
209+
*
191210
* @param {string} path
192-
* Path to handle
211+
* The path to handle
193212
* @param {string | null | undefined} ext
194213
* File extension to remove
195214
* @return {string}
@@ -220,6 +239,8 @@ interface Pathe extends PosixPlatformPath {
220239
*
221240
* @see {@linkcode Cwd}
222241
*
242+
* @this {void}
243+
*
223244
* @param {ReadonlyArray<string> | string} paths
224245
* Sequence of paths or path segments
225246
* @param {Cwd | null | undefined} [cwd]
@@ -239,31 +260,38 @@ interface Pathe extends PosixPlatformPath {
239260
/**
240261
* Get the root of `path`.
241262
*
263+
* @this {void}
264+
*
242265
* @param {string} path
243-
* Path to handle
266+
* The path to handle
244267
* @return {string}
245268
* Root of `path`
246269
*/
247270
root(this: void, path: string): string
248271

249272
/**
250-
* Make `path` POSIX-compliant.
273+
* Make separators in `path` POSIX-compliant.
251274
*
252-
* This includes:
253-
*
254-
* - Converting Windows-style path delimiters (`;`) to POSIX (`:`)
255-
* - Converting Windows-style path segment separators (`\`) to POSIX (`/`)
256-
*
257-
* @see https://nodejs.org/api/path.html#windows-vs-posix
258-
* @see https://nodejs.org/api/path.html#pathdelimiter
259-
* @see https://nodejs.org/api/path.html#pathsep
275+
* @this {void}
260276
*
261277
* @param {string} path
262-
* Path to handle
278+
* The path to handle
263279
* @return {string}
264-
* POSIX-compliant `path`
280+
* `path` with POSIX-compliant separators
265281
*/
266282
toPosix(this: void, path: string): string
283+
284+
/**
285+
* Make separators in `url` POSIX-compliant.
286+
*
287+
* @this {void}
288+
*
289+
* @param {string} url
290+
* The {@linkcode URL} to handle
291+
* @return {URL}
292+
* `url` with POSIX-compliant separators
293+
*/
294+
toPosix(this: void, url: URL): URL
267295
}
268296

269297
export type { Pathe as default }

src/interfaces/platform-path.mts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ interface PlatformPath {
6060
* [sep]: https://nodejs.org/api/path.html#pathsep
6161
*
6262
* @param {string} path
63-
* Path to handle
63+
* The path to handle
6464
* @param {string | null | undefined} [suffix]
6565
* Suffix to remove
6666
* @return {string}
@@ -76,7 +76,7 @@ interface PlatformPath {
7676
* [sep]: https://nodejs.org/api/path.html#pathsep
7777
*
7878
* @param {string} path
79-
* Path to handle
79+
* The path to handle
8080
* @return {string}
8181
* Directory name of `path`
8282
*/
@@ -94,7 +94,7 @@ interface PlatformPath {
9494
* @see {@linkcode Ext}
9595
*
9696
* @param {string} path
97-
* Path to handle
97+
* The path to handle
9898
* @return {EmptyString | Ext}
9999
* Extension of `path` or empty string
100100
*/
@@ -210,7 +210,7 @@ interface PlatformPath {
210210
* @see {@linkcode ParsedPath}
211211
*
212212
* @param {string} path
213-
* Path to handle
213+
* The path to handle
214214
* @return {ParsedPath}
215215
* Significant elements of `path`
216216
*/
@@ -286,7 +286,7 @@ interface PlatformPath {
286286
* [unc]: https://learn.microsoft.com/dotnet/standard/io/file-path-formats#unc-paths
287287
*
288288
* @param {string} path
289-
* Path to handle
289+
* The path to handle
290290
* @return {string}
291291
* Namespace-prefixed path or `path` without modifications
292292
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @file Unit Tests - validateURLString
3+
* @module pathe/internal/tests/unit/validateURLString
4+
*/
5+
6+
import testSubject from '#internal/validate-url-string'
7+
import { codes, isNodeError, type NodeError } from '@flex-development/errnode'
8+
9+
describe('unit:internal/validateURLString', () => {
10+
let name: string
11+
12+
beforeAll(() => {
13+
name = 'value'
14+
})
15+
16+
it('should return `true` if `value` is a string', () => {
17+
expect(testSubject(new URL(import.meta.url), name)).to.be.true
18+
})
19+
20+
it('should return `true` if `value` is a string', () => {
21+
expect(testSubject(import.meta.url, name)).to.be.true
22+
})
23+
24+
it('should throw if `value` is not a `URL` or string', () => {
25+
// Arrange
26+
let error!: NodeError
27+
28+
// Act
29+
try {
30+
testSubject(null, name)
31+
} catch (e: unknown) {
32+
error = e as typeof error
33+
}
34+
35+
// Expect
36+
expect(error).to.satisfy(isNodeError)
37+
expect(error).to.have.property('code', codes.ERR_INVALID_ARG_TYPE)
38+
})
39+
})

src/internal/validate-string.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
* @param {string} name
1919
* Name of invalid argument or property
2020
* @return {value is string}
21-
* `true` if `value` is a string, `false` otherwise
21+
* `true` if `value` is a string
2222
* @throws {ErrInvalidArgType}
2323
* If `value` is not a string
2424
*/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @file Internal - validateURLString
3+
* @module pathe/internal/validateURLString
4+
*/
5+
6+
import isURL from '#internal/is-url'
7+
import {
8+
ERR_INVALID_ARG_TYPE,
9+
type ErrInvalidArgType
10+
} from '@flex-development/errnode'
11+
12+
/**
13+
* Check if `value` is a {@linkcode URL} or string.
14+
*
15+
* @internal
16+
*
17+
* @param {unknown} value
18+
* Value to check
19+
* @param {string} name
20+
* Name of invalid argument or property
21+
* @return {value is URL | string}
22+
* `true` if `value` is a `URL` or string
23+
* @throws {ErrInvalidArgType}
24+
* If `value` is not a `URL` or string
25+
*/
26+
function validateURLString(
27+
value: unknown,
28+
name: string
29+
): value is URL | string {
30+
if (isURL(value) || typeof value === 'string') return true
31+
throw new ERR_INVALID_ARG_TYPE(name, ['URL', 'string'], value)
32+
}
33+
34+
export default validateURLString

0 commit comments

Comments
 (0)