pattern-key-compare
v2.0.0
Published
PATTERN_KEY_COMPARE implementation
Downloads
49,104
Readme
pattern-key-compare
Implementation of PATTERN_KEY_COMPARE
of Node.js resolver algorithm.
It implements the updated algorithm:
PATTERN_KEY_COMPARE(keyA, keyB)
- Assert:
keyA
contains only a single"*"
. - Assert:
keyB
contains only a single"*"
. - Let
baseLengthA
be the index of"*"
inkeyA
plus one. - Let
baseLengthB
be the index of"*"
inkeyB
plus one. - If
baseLengthA
is greater thanbaseLengthB
, return-1
. - If
baseLengthB
is greater thanbaseLengthA
, return1
. - If the length of
keyA
is greater than the length ofkeyB
, return-1
. - If the length of
keyB
is greater than the length ofkeyA
, return1
. - Return
0
.
Notes
Assert: keyA/B ends with "/" or contains only a single "*"
Note that this is not correct as nowadays it supports file extensions. e.g.:
#a/b.js
./foo/*.js
Install
# npm
npm install pattern-key-compare
# yarn
yarn add pattern-key-compare
# pnpm
pnpm add pattern-key-compare
# rush
rush add -p pattern-key-compare
Usage
import { patternKeyCompare } from 'pattern-key-compare'
[...].sort(patternKeyCompare)