-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.ts
51 lines (39 loc) · 1.67 KB
/
utils.ts
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
import { join } from './path'
import { normalizeWindowsPath } from './_internal'
const pathSeparators = ['/', '\\', undefined]
const normalizedAliasSymbol = Symbol.for('pathe:normalizedAlias')
export function normalizeAliases (_aliases: Record<string, string>) {
if ((_aliases as any)[normalizedAliasSymbol]) { return _aliases }
// Sort aliases from specific to general (ie. fs/promises before fs)
const aliases = Object.fromEntries(Object.entries(_aliases).sort(([a], [b]) => _compareAliases(a, b)))
// Resolve alias values in relation to each other
for (const key in aliases) {
for (const alias in aliases) {
// don't resolve a more specific alias with regard to a less specific one
if (alias === key || key.startsWith(alias)) { continue }
if (aliases[key].startsWith(alias) && pathSeparators.includes(aliases[key][alias.length])) {
aliases[key] = aliases[alias] + aliases[key].slice(alias.length)
}
}
}
Object.defineProperty(aliases, normalizedAliasSymbol, { value: true, enumerable: false })
return aliases
}
export function resolveAlias (path: string, aliases: Record<string, string>) {
const _path = normalizeWindowsPath(path)
aliases = normalizeAliases(aliases)
for (const alias in aliases) {
if (_path.startsWith(alias) && pathSeparators.includes(_path[alias.length])) {
return join(aliases[alias], _path.slice(alias.length))
}
}
return _path
}
const FILENAME_RE = /(^|[\\/])([^\\/]+?)(?=(\.[^.]+)?$)/
export function filename (path: string) {
return path.match(FILENAME_RE)?.[2]
}
// --- internals ---
function _compareAliases (a: string, b: string) {
return b.split('/').length - a.split('/').length
}