# pattern-key-compare

> PATTERN_KEY_COMPARE implementation

Latest version **2.0.1** (published 2026-09-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install pattern-key-compare
pnpm add pattern-key-compare
yarn add pattern-key-compare
bun add pattern-key-compare
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2026-09-01 |
| First published | 2023-01-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20 |
| Dependencies | 0 |
| Unpacked size | 7.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 5 |
| Author | Homa Wong (unional) |
| Maintainers | unional |
| Keywords | resolve, esm, fields, nodejs |

## Links

- npm: https://www.npmjs.com/package/pattern-key-compare
- Repository: https://github.com/cyberuni/resolve.imports
- Issues: https://github.com/cyberuni/resolve.imports/issues
- npm.io page: https://npm.io/package/pattern-key-compare

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 2.0.1 (latest) — 2026-09-01
- 2.0.0 — 2023-01-03
- 1.0.0 — 2023-01-01

## README

# pattern-key-compare

[![NPM version][npm-image]][npm-url]
[![NPM downloads][downloads-image]][downloads-url]
[![Codecov][codecov-image]][codecov-url]

Implementation of `PATTERN_KEY_COMPARE` of [Node.js resolver algorithm][resolver-algorithm].

It implements the [updated algorithm](https://github.com/nodejs/node/pull/46068):

`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

```sh
# 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

```ts
import { patternKeyCompare } from 'pattern-key-compare'

[...].sort(patternKeyCompare)
```

[codecov-image]: https://codecov.io/gh/cyberuni/resolve.imports/branch/main/graph/badge.svg
[codecov-url]: https://codecov.io/gh/cyberuni/resolve.imports
[downloads-image]: https://img.shields.io/npm/dm/pattern-key-compare.svg?style=flat
[downloads-url]: https://npmjs.org/package/pattern-key-compare
[npm-image]: https://img.shields.io/npm/v/pattern-key-compare.svg?style=flat
[npm-url]: https://npmjs.org/package/pattern-key-compare
[resolver-algorithm]: https://nodejs.org/api/esm.html#resolver-algorithm-specification

---
_Source: https://npm.io/package/pattern-key-compare · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
