-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern-match.mts
110 lines (99 loc) · 2.95 KB
/
pattern-match.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @file patternMatch
* @module mlly/lib/patternMatch
*/
import chars from '#internal/chars'
import patternKeyCompare from '#lib/pattern-key-compare'
import type { PatternMatch } from '@flex-development/mlly'
import { ok } from 'devlop'
/**
* Get a subpath pattern match for `matchKey`.
*
* @see {@linkcode PatternMatch}
*
* @param {string} matchKey
* The string to expand
* @param {unknown} matchObject
* Object containing match keys
* @return {PatternMatch | null}
* List containing key of `matchObject` and subpath pattern match or `null`
*/
function patternMatch(
matchKey: string,
matchObject: unknown
): PatternMatch | null {
if (typeof matchObject === 'object' && matchObject) {
if (
Object.prototype.hasOwnProperty.call(matchObject, matchKey) &&
!matchKey.includes(chars.asterisk)
) {
return [matchKey, null]
}
/**
* Keys of {@linkcode matchObject} containing only a single "*".
*
* Keys are sorted by {@linkcode patternKeyCompare}, which orders in
* descending order of specificity.
*
* @const {string[]} expansionKeys
*/
const expansionKeys: string[] = Object
.getOwnPropertyNames(matchObject)
.filter(expansionKey => {
/**
* Index of {@linkcode chars.asterisk} in {@linkcode expansionKey}.
*
* @const {number} patternIndex
*/
const patternIndex: number = expansionKey.indexOf(chars.asterisk)
return (
patternIndex >= 0 &&
patternIndex === expansionKey.lastIndexOf(chars.asterisk)
)
})
.sort(patternKeyCompare)
for (const expansionKey of expansionKeys) {
/**
* Index of {@linkcode chars.asterisk} in {@linkcode expansionKey}.
*
* @const {number} patternIndex
*/
const patternIndex: number = expansionKey.indexOf(chars.asterisk)
/**
* Segment before {@linkcode chars.asterisk} in {@linkcode expansionKey}.
*
* @const {string} patternBase
*/
const patternBase: string = expansionKey.slice(0, patternIndex)
ok(patternIndex >= 0, 'expected `patternIndex >= 0`')
if (matchKey !== patternBase && matchKey.startsWith(patternBase)) {
/**
* Segment after {@linkcode chars.asterisk} in {@linkcode expansionKey}.
*
* @const {string} patternTrailer
*/
const patternTrailer: string = expansionKey.slice(patternIndex + 1)
if (
!patternTrailer.length ||
(
matchKey.endsWith(patternTrailer) &&
(
matchKey.length >= expansionKey.length ||
matchKey === patternBase + patternTrailer
)
)
) {
return [
expansionKey,
matchKey.slice(
patternBase.length,
matchKey.length - patternTrailer.length
)
]
}
}
}
}
return null
}
export default patternMatch