Skip to content

Commit 7afa7df

Browse files
authored
Turn into ES Module (#48)
1 parent 0185847 commit 7afa7df

File tree

6 files changed

+34
-36
lines changed

6 files changed

+34
-36
lines changed

.github/workflows/main.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- uses: actions/checkout@v4
1212
- uses: actions/setup-node@v4
1313
with:
14-
node-version: 16
14+
node-version-file: package.json
1515
- run: npm install
1616
- run: npx xo
1717

@@ -21,7 +21,7 @@ jobs:
2121
- uses: actions/checkout@v4
2222
- uses: actions/setup-node@v4
2323
with:
24-
node-version: 16
24+
node-version-file: package.json
2525
- run: npm install
2626
- run: npx tsd
2727

@@ -31,6 +31,6 @@ jobs:
3131
- uses: actions/checkout@v4
3232
- uses: actions/setup-node@v4
3333
with:
34-
node-version: 16
34+
node-version-file: package.json
3535
- run: npm install
3636
- run: npx ava

.github/workflows/npm-publish.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ jobs:
1919
NPM:
2020
runs-on: ubuntu-latest
2121
steps:
22-
- uses: actions/checkout@v3
23-
- uses: actions/setup-node@v3
22+
- uses: actions/checkout@v4
23+
- uses: actions/setup-node@v4
2424
with:
25-
node-version: 18
25+
node-version-file: package.json
2626
registry-url: https://registry.npmjs.org
2727
- run: npm ci || npm install
2828
- uses: fregante/setup-git-user@v2

index.d.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,14 @@ Shortens a GitHub URL string.
77
@example https://github.com/nodejs/node/tree/v0.12/doc becomes nodejs/node@<code>v0.12</code>
88
99
*/
10-
declare function shortenRepoUrl(url: string, currentUrl?: string): string;
10+
export default function shortenRepoUrl(url: string, currentUrl?: string): string;
1111

12-
declare namespace shortenRepoUrl {
13-
/**
14-
Shortens a GitHub URL in a DOM anchor.
12+
/**
13+
Shortens a GitHub URL in a DOM anchor if the link label is not customized (i.e. if the `href` matches the `textContent`)
1514
16-
@param anchor An HTMLAnchorElement
17-
@param url The GitHub URL to shorten.
18-
@example https://github.com/nodejs/node/tree/v0.12/doc becomes nodejs/node@<code>v0.12</code>
15+
@param anchor An HTMLAnchorElement
16+
@param url The GitHub URL to shorten.
17+
@example<a href="https://github.com">https://github.com</a> becomes <a href="https://github.com">github.com</a>
1918
20-
*/
21-
const applyToLink: (anchor: HTMLAnchorElement, url?: string) => void;
22-
}
23-
export = shortenRepoUrl;
19+
*/
20+
export function applyToLink(anchor: HTMLAnchorElement, url?: string): void;

index.js

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const reservedPaths = require('github-reserved-names/reserved-names.json');
1+
import reservedNames from 'github-reserved-names/reserved-names.json' with { type: 'json' };
22

33
const patchDiffRegex = /[.](patch|diff)$/;
44
const releaseRegex = /^releases[/]tag[/]([^/]+)/;
@@ -43,7 +43,7 @@ function joinValues(array, delimiter = '/') {
4343
return array.filter(Boolean).join(delimiter);
4444
}
4545

46-
function shortenURL(href, currentUrl = 'https://github.com') {
46+
function shortenRepoUrl(href, currentUrl = 'https://github.com') {
4747
if (!href) {
4848
return;
4949
}
@@ -101,20 +101,20 @@ function shortenURL(href, currentUrl = 'https://github.com') {
101101

102102
const isLocal = origin === currentUrl.origin;
103103
const isThisRepo = (isLocal || isRaw || isRedirection) && currentRepo === `${user}/${repo}`;
104-
const isReserved = reservedPaths.includes(user);
104+
const isReserved = reservedNames.includes(user);
105105
const isDependents = dependentsRegex.test(repoPath);
106106
const isDependencies = dependenciesRegex.test(repoPath);
107107
const [, diffOrPatch] = repoPath.match(patchDiffRegex) || [];
108108
const [, release] = repoPath.match(releaseRegex) || [];
109-
const [, releaseTag, releaseTagExt] = repoPath.match(releaseArchiveRegex) || [];
109+
const [, releaseTag, releaseTagExtension] = repoPath.match(releaseArchiveRegex) || [];
110110
const [, downloadTag, downloadFilename] = repoPath.match(releaseDownloadRegex) || [];
111111
const [, label] = repoPath.match(labelRegex) || [];
112112
const [, compare] = repoPath.match(compareRegex) || [];
113113
const {pull, pullPage, pullPartialStart, pullPartialEnd} = repoPath.match(pullRegex)?.groups ?? {};
114114
const [, issue] = isRedirection ? repoPath.match(issueRegex) || [] : [];
115115
const [, commit] = isRedirection ? repoPath.match(commitRegex) || [] : [];
116116
const [, wiki] = repoPath.match(wikiRegex) || [];
117-
const isFileOrDir = revision && [
117+
const isFileOrDirectory = revision && [
118118
'raw',
119119
'tree',
120120
'blob',
@@ -139,7 +139,7 @@ function shortenURL(href, currentUrl = 'https://github.com') {
139139
return `@${user}${search}${hash}`;
140140
}
141141

142-
if (isFileOrDir) {
142+
if (isFileOrDirectory) {
143143
const revisioned = joinValues(
144144
[joinValues([repoUrl, revision], '@'), filePath],
145145
'/',
@@ -162,9 +162,9 @@ function shortenURL(href, currentUrl = 'https://github.com') {
162162
return `${partial}${search}${hash} (release)`;
163163
}
164164

165-
if (releaseTagExt) {
165+
if (releaseTagExtension) {
166166
const partial = joinValues([repoUrl, `<code>${releaseTag}</code>`], '@');
167-
return `${partial}${releaseTagExt}${search}${hash}`;
167+
return `${partial}${releaseTagExtension}${search}${hash}`;
168168
}
169169

170170
if (downloadFilename) {
@@ -241,7 +241,7 @@ function shortenURL(href, currentUrl = 'https://github.com') {
241241
return pathname.replaceAll(/^[/]|[/]$/g, '') + url.search + hash + query;
242242
}
243243

244-
function applyToLink(a, currentUrl) {
244+
export function applyToLink(a, currentUrl) {
245245
// Shorten only if the link name hasn't been customized.
246246
// .href automatically adds a / to naked origins so that needs to be tested too
247247
// `trim` makes it compatible with this feature: https://github.com/sindresorhus/refined-github/pull/3085
@@ -250,13 +250,12 @@ function applyToLink(a, currentUrl) {
250250
(url === a.textContent.trim() || url === `${a.textContent}/`)
251251
&& !a.firstElementChild
252252
) {
253-
const shortened = shortenURL(url, currentUrl);
253+
const shortened = shortenRepoUrl(url, currentUrl);
254254
a.innerHTML = shortened;
255255
return true;
256256
}
257257

258258
return false;
259259
}
260260

261-
module.exports = shortenURL;
262-
module.exports.applyToLink = applyToLink;
261+
export default shortenRepoUrl;

test.js renamed to index.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const test = require('ava');
2-
const shortenUrl = require('.');
1+
import test from 'ava';
2+
import shortenUrl from './index.js';
33

44
const currentLocation = 'https://github.com/fregante/shorten-repo-url/issue/1';
55

package.json

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"funding": "https://github.com/sponsors/fregante",
2020
"license": "MIT",
2121
"author": "Federico Brigante <[email protected]> (https://fregante.com)",
22+
"type": "module",
2223
"exports": "./index.js",
2324
"main": "./index.js",
2425
"types": "./index.d.ts",
@@ -30,6 +31,7 @@
3031
"test": "xo && tsd && ava"
3132
},
3233
"xo": {
34+
"parser": "@typescript-eslint/parser",
3335
"rules": {
3436
"unicorn/better-regex": "off",
3537
"unicorn/prefer-module": "off",
@@ -40,12 +42,12 @@
4042
"github-reserved-names": "^2.0.5"
4143
},
4244
"devDependencies": {
43-
"@sindresorhus/tsconfig": "^4.0.0",
44-
"ava": "^5.3.1",
45-
"tsd": "^0.29.0",
46-
"xo": "^0.56.0"
45+
"@sindresorhus/tsconfig": "^5.0.0",
46+
"ava": "^6.1.3",
47+
"tsd": "^0.31.1",
48+
"xo": "^0.58.0"
4749
},
4850
"engines": {
49-
"node": ">=18"
51+
"node": ">=20.10"
5052
}
5153
}

0 commit comments

Comments
 (0)