-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
634 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'pattern-key-compare': major | ||
--- | ||
|
||
Initial release. | ||
Extract from `resolve.imports`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'resolve.imports': patch | ||
--- | ||
|
||
Extract `patternKeyCompare` to `pattern-key-compare` package. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ignores: | ||
- '@repobuddy/jest' | ||
- '@size-limit/preset-small-lib' | ||
- '@types/node' | ||
- jest | ||
- jest-* | ||
- ts-jest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
cjs | ||
esm | ||
tslib | ||
*.tsbuildinfo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[ | ||
{ | ||
"path": "./cjs/index.js", | ||
"limit": "2 KB" | ||
}, | ||
{ | ||
"path": "./esm/index.js", | ||
"limit": "2 KB" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright © 2022 Homa Wong (unional) ([email protected]) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# 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]. | ||
|
||
Here is a snapshot of the algorithm from Node.js v19.3.0: | ||
|
||
`PATTERN_KEY_COMPARE(keyA, keyB)` | ||
|
||
1. Assert: `keyA` ends with `"/"` or contains only a single `"*"`. | ||
2. Assert: `keyB` ends with `"/"` or contains only a single `"*"`. | ||
3. Let `baseLengthA` be the index of `"*"` in `keyA` plus one, if `keyA` contains `"*"`, or the length of `keyA` otherwise. | ||
4. Let `baseLengthB` be the index of `"*"` in `keyB` plus one, if `keyB` contains `"*"`, or the length of `keyB` otherwise. | ||
5. If `baseLengthA` is greater than `baseLengthB`, return `-1`. | ||
6. If `baseLengthB` is greater than `baseLengthA`, return `1`. | ||
7. If `keyA` does not contain "*", return `1`. | ||
8. If `keyB` does not contain "*", return `-1`. | ||
9. If the length of `keyA` is greater than the length of `keyB`, return `-1`. | ||
10. If the length of `keyB` is greater than the length of `keyA`, return `1`. | ||
11. 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/** @type {import('jest').Config} */ | ||
export default { | ||
preset: '@repobuddy/jest/presets/ts-esm-watch' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"type":"commonjs"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
{ | ||
"name": "pattern-key-compare", | ||
"version": "0.0.0", | ||
"description": "PATTERN_KEY_COMPARE implementation", | ||
"keywords": [ | ||
"resolve", | ||
"esm", | ||
"fields", | ||
"nodejs" | ||
], | ||
"homepage": "https://github.com/cyberuni/resolve.imports", | ||
"bugs": { | ||
"url": "https://github.com/cyberuni/resolve.imports/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/cyberuni/resolve.imports.git", | ||
"directory": "packages/pattern-key-compare" | ||
}, | ||
"license": "MIT", | ||
"author": { | ||
"name": "Homa Wong (unional)", | ||
"email": "[email protected]" | ||
}, | ||
"sideEffects": false, | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"types": "./esm/index.d.ts", | ||
"import": "./esm/index.js", | ||
"default": "./cjs/index.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"main": "./cjs/index.js", | ||
"types": "./esm/index.d.ts", | ||
"files": [ | ||
"cjs", | ||
"esm", | ||
"ts", | ||
"!cjs/**/*.d.ts", | ||
"!**/*.test.*", | ||
"!**/*.spec.*", | ||
"!**/*.unit.*", | ||
"!**/*.integrate.*", | ||
"!**/*.system.*", | ||
"!**/*.accept.*" | ||
], | ||
"scripts": { | ||
"build": "run-p build:cjs build:esm", | ||
"build:esm": "tsc --project tsconfig.esm.json", | ||
"build:cjs": "tsc --project tsconfig.cjs.json && ncp package.cjs.json cjs/package.json", | ||
"clean": "rimraf cjs esm tslib *.tsbuildinfo", | ||
"coverage": "cross-env NODE_OPTIONS=--experimental-vm-modules NODE_OPTIONS=--experimental-import-meta-resolve NODE_NO_WARNINGS=1 jest --coverage", | ||
"depcheck": "depcheck", | ||
"size": "size-limit", | ||
"test": "cross-env NODE_OPTIONS=--experimental-vm-modules NODE_OPTIONS=--experimental-import-meta-resolve jest", | ||
"test:watch": "cross-env NODE_OPTIONS=--experimental-vm-modules NODE_OPTIONS=--experimental-import-meta-resolve jest --watch", | ||
"verify": "npm-run-all -p build -p test depcheck size" | ||
}, | ||
"devDependencies": { | ||
"@jest/globals": "^29.3.1", | ||
"@repobuddy/jest": "^2.3.0", | ||
"@size-limit/preset-small-lib": "^8.1.0", | ||
"@types/node": "^14", | ||
"cross-env": "^7.0.3", | ||
"depcheck": "^1.4.3", | ||
"jest": "^29.3.1", | ||
"jest-watch-suspend": "^1.1.2", | ||
"jest-watch-toggle-config-2": "^2.1.0", | ||
"jest-watch-typeahead": "^2.2.1", | ||
"ncp": "^2.0.0", | ||
"npm-run-all": "^4.1.5", | ||
"rimraf": "^3.0.2", | ||
"size-limit": "^8.1.0", | ||
"ts-jest": "^29.0.3", | ||
"typescript": "^4.9.4" | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
packages/resolve.imports/ts/resolve.unit.ts → ...ages/pattern-key-compare/ts/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Implementation of `PATTERN_KEY_COMPARE` | ||
* | ||
* @see https://nodejs.org/api/esm.html#esm_resolver_algorithm_specification | ||
*/ | ||
export function patternKeyCompare(a: string, b: string) { | ||
const iA = a.indexOf('*') | ||
const iB = b.indexOf('*') | ||
const baseLengthA = iA === -1 ? a.length : iA + 1 | ||
const baseLengthB = iB === -1 ? b.length : iB + 1 | ||
if (baseLengthA > baseLengthB) return -1 | ||
if (baseLengthA < baseLengthB) return 1 | ||
if (iA === -1) return 1 | ||
if (iB === -1) return -1 | ||
return a.length > b.length ? -1 : a.length < b.length ? 1 : 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"compilerOptions": { | ||
"composite": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"moduleResolution": "Node16", | ||
"newLine": "LF", | ||
"rootDir": "ts", | ||
"strict": true, | ||
"target": "ES2020" | ||
}, | ||
"include": [ | ||
"ts", | ||
"types" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"extends": "./tsconfig.base.json", | ||
"compilerOptions": { | ||
"esModuleInterop": true, | ||
"module": "CommonJS", | ||
"outDir": "cjs" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "./tsconfig.base.json", | ||
"compilerOptions": { | ||
"declaration": true, | ||
"declarationMap": true, | ||
"module": "Node16", | ||
"outDir": "esm" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"extends": "./tsconfig.esm.json", | ||
"compilerOptions": { | ||
"esModuleInterop": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
ignores: | ||
- '@repobuddy/jest' | ||
- '@size-limit/preset-small-lib' | ||
- '@types/node' | ||
- jest | ||
- jest-* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
[ | ||
{ | ||
"path": "./cjs/index.js", | ||
"limit": "10 KB" | ||
"limit": "2 KB" | ||
}, | ||
{ | ||
"path": "./esm/index.js", | ||
"limit": "10 KB" | ||
"limit": "2 KB" | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.