-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern-key-compare.mts
84 lines (75 loc) · 2.16 KB
/
pattern-key-compare.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
/**
* @file patternKeyCompare
* @module mlly/lib/patternKeyCompare
*/
import chars from '#internal/chars'
import type { PatternKeyCompareResult } from '@flex-development/mlly'
import { ok } from 'devlop'
/**
* Compares two strings that may contain a wildcard character (`'*'`) and
* returns a value indicating their order:
*
* - `-1`: `a` should come before `b`
* - `0`: `a` and `b` are equal
* - `1`: `a` should come after `b`
*
* Implements the `PATTERN_KEY_COMPARE` algorithm.
*
* @see {@linkcode PatternKeyCompareResult}
*
* @param {string} a
* First string to compare
* @param {string} b
* Second string to compare
* @return {PatternKeyCompareResult}
* Comparsion result
*/
function patternKeyCompare(a: string, b: string): PatternKeyCompareResult {
/**
* Index of pattern character in {@linkcode a}.
*
* @var {number} aPatternIndex
*/
let aPatternIndex: number = a.indexOf(chars.asterisk)
/**
* Index of pattern character in {@linkcode b}.
*
* @var {number} bPatternIndex
*/
let bPatternIndex: number = b.indexOf(chars.asterisk)
ok(aPatternIndex === a.lastIndexOf(chars.asterisk), 'expected a single "*"')
ok(bPatternIndex === b.lastIndexOf(chars.asterisk), 'expected a single "*"')
/**
* Base length of {@linkcode a}.
*
* @const {number} baseLenA
*/
const baseLenA: number = aPatternIndex === patternKeyCompare.LESS_THAN
? a.length
: aPatternIndex + 1
/**
* Base length of {@linkcode b}.
*
* @const {number} baseLenB
*/
const baseLenB: number = bPatternIndex === patternKeyCompare.LESS_THAN
? b.length
: bPatternIndex + 1
return baseLenA > baseLenB
? patternKeyCompare.LESS_THAN
: baseLenB > baseLenA
? patternKeyCompare.GREATER_THAN
: aPatternIndex === -1
? patternKeyCompare.GREATER_THAN
: bPatternIndex === -1
? patternKeyCompare.LESS_THAN
: a.length > b.length
? patternKeyCompare.LESS_THAN
: b.length > a.length
? patternKeyCompare.GREATER_THAN
: patternKeyCompare.EQUAL
}
patternKeyCompare.EQUAL = 0 as const
patternKeyCompare.GREATER_THAN = 1 as const
patternKeyCompare.LESS_THAN = -1 as const
export default patternKeyCompare