2.0.0 • Published 3 years ago
pattern-key-compare v2.0.0
pattern-key-compare
Implementation of PATTERN_KEY_COMPARE of Node.js resolver algorithm.
It implements the updated algorithm:
PATTERN_KEY_COMPARE(keyA, keyB)
- Assert:
keyAcontains only a single"*". - Assert:
keyBcontains only a single"*". - Let
baseLengthAbe the index of"*"inkeyAplus one. - Let
baseLengthBbe the index of"*"inkeyBplus one. - If
baseLengthAis greater thanbaseLengthB, return-1. - If
baseLengthBis greater thanbaseLengthA, return1. - If the length of
keyAis greater than the length ofkeyB, return-1. - If the length of
keyBis 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-compareUsage
import { patternKeyCompare } from 'pattern-key-compare'
[...].sort(patternKeyCompare)