2.0.0 • Published 1 year ago

pattern-key-compare v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

pattern-key-compare

NPM version NPM downloads Codecov

Implementation of PATTERN_KEY_COMPARE of Node.js resolver algorithm.

It implements the updated algorithm:

PATTERN_KEY_COMPARE(keyA, keyB)

  1. Assert: keyA contains only a single "*".
  2. Assert: keyB contains only a single "*".
  3. Let baseLengthA be the index of "*" in keyA plus one.
  4. Let baseLengthB be the index of "*" in keyB plus one.
  5. If baseLengthA is greater than baseLengthB, return -1.
  6. If baseLengthB is greater than baseLengthA, return 1.
  7. If the length of keyA is greater than the length of keyB, return -1.
  8. If the length of keyB is greater than the length of keyA, return 1.
  9. 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)