-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto-namespaced-path.mts
77 lines (68 loc) · 2.02 KB
/
to-namespaced-path.mts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/**
* @file toNamespacedPath
* @module pathe/lib/toNamespacedPath
*/
import { DRIVE_PATH_REGEX } from '#internal/constants'
import validateString from '#internal/validate-string'
import dot from '#lib/dot'
import isSep from '#lib/is-sep'
import resolveWith from '#lib/resolve-with'
import toPosix from '#lib/to-posix'
import type { ResolveWithOptions } from '@flex-development/pathe'
/**
* Get an equivalent [namespace-prefixed path][namespace] for `path`.
*
* > 👉 **Note**: If `path` is not a [drive][drive] or [UNC][unc] path, it will
* > be returned without modifications.
*
* [drive]: https://learn.microsoft.com/windows/win32/fileio/naming-a-file#naming-conventions
* [namespace]: https://docs.microsoft.com/windows/desktop/FileIO/naming-a-file#namespaces
* [unc]: https://learn.microsoft.com/dotnet/standard/io/file-path-formats#unc-paths
*
* @see {@linkcode ResolveWithOptions}
*
* @category
* core
*
* @this {void}
*
* @param {string} path
* The path to handle
* @param {ResolveWithOptions | null | undefined} [options]
* Resolution options
* @return {string}
* Namespace-prefixed path or `path` without modifications
*/
function toNamespacedPath(
this: void,
path: string,
options?: ResolveWithOptions | null | undefined
): string {
validateString(path, 'path')
if (path) {
path = toPosix(path)
/**
* Fully resolved {@linkcode path}.
*
* @const {string} resolved
*/
const resolved: string = resolveWith(path, options)
if (resolved.length > 2) {
// matched non-long unc root -> convert the path to long unc path
if (
isSep(resolved[0]) &&
isSep(resolved[1]) &&
!['?', dot].includes(resolved[2]!)
) {
return toPosix('\\\\?\\UNC\\') + resolved.slice(2)
}
// matched device root -> convert path to long unc path
if (DRIVE_PATH_REGEX.test(resolved) && isSep(resolved[2])) {
return toPosix('\\\\?\\') + resolved
}
}
return resolved
}
return path
}
export default toNamespacedPath