From c2a00982193861f5c33f2d3636884c7ae5b1601a Mon Sep 17 00:00:00 2001 From: Federico Date: Mon, 31 Mar 2025 12:00:24 +0800 Subject: [PATCH] Add `pathParts` to `getRepositoryInfo` --- __snapshots__/index.test.ts.snap | 265 +++++++++++++++++++++++++++++++ index.test.ts | 51 +----- index.ts | 11 +- 3 files changed, 282 insertions(+), 45 deletions(-) create mode 100644 __snapshots__/index.test.ts.snap diff --git a/__snapshots__/index.test.ts.snap b/__snapshots__/index.test.ts.snap new file mode 100644 index 0000000..824fc4a --- /dev/null +++ b/__snapshots__/index.test.ts.snap @@ -0,0 +1,265 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`getRepositoryInfo 1`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "", + "pathParts": [], +} +`; + +exports[`getRepositoryInfo 2`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "", + "pathParts": [], +} +`; + +exports[`getRepositoryInfo 3`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "blame/master/package.json", + "pathParts": [ + "blame", + "master", + "package.json", + ], +} +`; + +exports[`getRepositoryInfo 4`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "commit/57bf4", + "pathParts": [ + "commit", + "57bf4", + ], +} +`; + +exports[`getRepositoryInfo 5`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "compare/test-branch", + "pathParts": [ + "compare", + "test-branch", + ], +} +`; + +exports[`getRepositoryInfo 6`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "tree/master/distribution", + "pathParts": [ + "tree", + "master", + "distribution", + ], +} +`; + +exports[`getRepositoryInfo 7`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "tree/master/distribution", + "pathParts": [ + "tree", + "master", + "distribution", + ], +} +`; + +exports[`getRepositoryInfo 8`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "", + "pathParts": [], +} +`; + +exports[`getRepositoryInfo 9`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "", + "pathParts": [], +} +`; + +exports[`getRepositoryInfo 10`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "blame/master/package.json", + "pathParts": [ + "blame", + "master", + "package.json", + ], +} +`; + +exports[`getRepositoryInfo 11`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "commit/57bf4", + "pathParts": [ + "commit", + "57bf4", + ], +} +`; + +exports[`getRepositoryInfo 12`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "compare/test-branch", + "pathParts": [ + "compare", + "test-branch", + ], +} +`; + +exports[`getRepositoryInfo 13`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "tree/master/distribution", + "pathParts": [ + "tree", + "master", + "distribution", + ], +} +`; + +exports[`getRepositoryInfo 14`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "tree/master/distribution", + "pathParts": [ + "tree", + "master", + "distribution", + ], +} +`; + +exports[`getRepositoryInfo 15`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "", + "pathParts": [], +} +`; + +exports[`getRepositoryInfo 16`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "", + "pathParts": [], +} +`; + +exports[`getRepositoryInfo 17`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "blame/master/package.json", + "pathParts": [ + "blame", + "master", + "package.json", + ], +} +`; + +exports[`getRepositoryInfo 18`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "commit/57bf4", + "pathParts": [ + "commit", + "57bf4", + ], +} +`; + +exports[`getRepositoryInfo 19`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "compare/test-branch", + "pathParts": [ + "compare", + "test-branch", + ], +} +`; + +exports[`getRepositoryInfo 20`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "tree/master/distribution", + "pathParts": [ + "tree", + "master", + "distribution", + ], +} +`; + +exports[`getRepositoryInfo 21`] = ` +{ + "name": "github-url-detection", + "nameWithOwner": "refined-github/github-url-detection", + "owner": "refined-github", + "path": "tree/master/distribution", + "pathParts": [ + "tree", + "master", + "distribution", + ], +} +`; diff --git a/index.test.ts b/index.test.ts index bf230e5..68e8d9a 100644 --- a/index.test.ts +++ b/index.test.ts @@ -1,6 +1,6 @@ /// import assert from 'node:assert/strict'; -import {test} from 'vitest'; +import {test, expect} from 'vitest'; import stripIndent from 'strip-indent'; import {getAllUrls, getTests} from './collector.js'; import * as pageDetect from './index.js'; @@ -153,47 +153,12 @@ test('getRepositoryInfo', () => { assert.equal(getRepositoryInfoAdapter('https://github.com'), undefined); assert.equal(getRepositoryInfoAdapter('https://gist.github.com/'), undefined); assert.equal(getRepositoryInfoAdapter('https://github.com/settings/developers'), undefined); - assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection'), { - owner: 'refined-github', - name: 'github-url-detection', - nameWithOwner: 'refined-github/github-url-detection', - path: '', - }); - assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/'), { - owner: 'refined-github', - name: 'github-url-detection', - nameWithOwner: 'refined-github/github-url-detection', - path: '', - }); - assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/blame/master/package.json'), { - owner: 'refined-github', - name: 'github-url-detection', - nameWithOwner: 'refined-github/github-url-detection', - path: 'blame/master/package.json', - }); - assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/commit/57bf4'), { - owner: 'refined-github', - name: 'github-url-detection', - nameWithOwner: 'refined-github/github-url-detection', - path: 'commit/57bf4', - }); - assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/compare/test-branch?quick_pull=0'), { - owner: 'refined-github', - name: 'github-url-detection', - nameWithOwner: 'refined-github/github-url-detection', - path: 'compare/test-branch', - }); - assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution'), { - owner: 'refined-github', - name: 'github-url-detection', - nameWithOwner: 'refined-github/github-url-detection', - path: 'tree/master/distribution', - }); - assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution/'), { - owner: 'refined-github', - name: 'github-url-detection', - nameWithOwner: 'refined-github/github-url-detection', - path: 'tree/master/distribution', - }); + expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection')).toMatchSnapshot(); + expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/')).toMatchSnapshot(); + expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/blame/master/package.json')).toMatchSnapshot(); + expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/commit/57bf4')).toMatchSnapshot(); + expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/compare/test-branch?quick_pull=0')).toMatchSnapshot(); + expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution')).toMatchSnapshot(); + expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution/')).toMatchSnapshot(); } }); diff --git a/index.ts b/index.ts index 7f7f057..6993934 100644 --- a/index.ts +++ b/index.ts @@ -811,6 +811,12 @@ export type RepositoryInfo = { @example '/user/repo/' -> '' @example '/settings/token/' -> undefined */ path: string; + + /** The `path` segments + @example '/user/repo/' -> [] + @example '/user/repo/issues/' -> ['issues'] + @example '/user/repo/issues/new' -> ['issues', 'new'] */ + pathParts: string[]; }; /** @@ -840,12 +846,13 @@ const getRepo = (url?: URL | HTMLAnchorElement | Location | string): RepositoryI return; } - const [owner, name, ...path] = getCleanPathname(url).split('/') as [string, string, string]; + const [owner, name, ...pathParts] = getCleanPathname(url).split('/') as [string, string, string]; return { owner, name, + pathParts, nameWithOwner: `${owner}/${name}`, - path: path.join('/'), + path: pathParts.join('/'), }; };