Skip to content

Commit c2a0098

Browse files
committed
Add pathParts to getRepositoryInfo
1 parent 278a6d2 commit c2a0098

File tree

3 files changed

+282
-45
lines changed

3 files changed

+282
-45
lines changed

__snapshots__/index.test.ts.snap

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`getRepositoryInfo 1`] = `
4+
{
5+
"name": "github-url-detection",
6+
"nameWithOwner": "refined-github/github-url-detection",
7+
"owner": "refined-github",
8+
"path": "",
9+
"pathParts": [],
10+
}
11+
`;
12+
13+
exports[`getRepositoryInfo 2`] = `
14+
{
15+
"name": "github-url-detection",
16+
"nameWithOwner": "refined-github/github-url-detection",
17+
"owner": "refined-github",
18+
"path": "",
19+
"pathParts": [],
20+
}
21+
`;
22+
23+
exports[`getRepositoryInfo 3`] = `
24+
{
25+
"name": "github-url-detection",
26+
"nameWithOwner": "refined-github/github-url-detection",
27+
"owner": "refined-github",
28+
"path": "blame/master/package.json",
29+
"pathParts": [
30+
"blame",
31+
"master",
32+
"package.json",
33+
],
34+
}
35+
`;
36+
37+
exports[`getRepositoryInfo 4`] = `
38+
{
39+
"name": "github-url-detection",
40+
"nameWithOwner": "refined-github/github-url-detection",
41+
"owner": "refined-github",
42+
"path": "commit/57bf4",
43+
"pathParts": [
44+
"commit",
45+
"57bf4",
46+
],
47+
}
48+
`;
49+
50+
exports[`getRepositoryInfo 5`] = `
51+
{
52+
"name": "github-url-detection",
53+
"nameWithOwner": "refined-github/github-url-detection",
54+
"owner": "refined-github",
55+
"path": "compare/test-branch",
56+
"pathParts": [
57+
"compare",
58+
"test-branch",
59+
],
60+
}
61+
`;
62+
63+
exports[`getRepositoryInfo 6`] = `
64+
{
65+
"name": "github-url-detection",
66+
"nameWithOwner": "refined-github/github-url-detection",
67+
"owner": "refined-github",
68+
"path": "tree/master/distribution",
69+
"pathParts": [
70+
"tree",
71+
"master",
72+
"distribution",
73+
],
74+
}
75+
`;
76+
77+
exports[`getRepositoryInfo 7`] = `
78+
{
79+
"name": "github-url-detection",
80+
"nameWithOwner": "refined-github/github-url-detection",
81+
"owner": "refined-github",
82+
"path": "tree/master/distribution",
83+
"pathParts": [
84+
"tree",
85+
"master",
86+
"distribution",
87+
],
88+
}
89+
`;
90+
91+
exports[`getRepositoryInfo 8`] = `
92+
{
93+
"name": "github-url-detection",
94+
"nameWithOwner": "refined-github/github-url-detection",
95+
"owner": "refined-github",
96+
"path": "",
97+
"pathParts": [],
98+
}
99+
`;
100+
101+
exports[`getRepositoryInfo 9`] = `
102+
{
103+
"name": "github-url-detection",
104+
"nameWithOwner": "refined-github/github-url-detection",
105+
"owner": "refined-github",
106+
"path": "",
107+
"pathParts": [],
108+
}
109+
`;
110+
111+
exports[`getRepositoryInfo 10`] = `
112+
{
113+
"name": "github-url-detection",
114+
"nameWithOwner": "refined-github/github-url-detection",
115+
"owner": "refined-github",
116+
"path": "blame/master/package.json",
117+
"pathParts": [
118+
"blame",
119+
"master",
120+
"package.json",
121+
],
122+
}
123+
`;
124+
125+
exports[`getRepositoryInfo 11`] = `
126+
{
127+
"name": "github-url-detection",
128+
"nameWithOwner": "refined-github/github-url-detection",
129+
"owner": "refined-github",
130+
"path": "commit/57bf4",
131+
"pathParts": [
132+
"commit",
133+
"57bf4",
134+
],
135+
}
136+
`;
137+
138+
exports[`getRepositoryInfo 12`] = `
139+
{
140+
"name": "github-url-detection",
141+
"nameWithOwner": "refined-github/github-url-detection",
142+
"owner": "refined-github",
143+
"path": "compare/test-branch",
144+
"pathParts": [
145+
"compare",
146+
"test-branch",
147+
],
148+
}
149+
`;
150+
151+
exports[`getRepositoryInfo 13`] = `
152+
{
153+
"name": "github-url-detection",
154+
"nameWithOwner": "refined-github/github-url-detection",
155+
"owner": "refined-github",
156+
"path": "tree/master/distribution",
157+
"pathParts": [
158+
"tree",
159+
"master",
160+
"distribution",
161+
],
162+
}
163+
`;
164+
165+
exports[`getRepositoryInfo 14`] = `
166+
{
167+
"name": "github-url-detection",
168+
"nameWithOwner": "refined-github/github-url-detection",
169+
"owner": "refined-github",
170+
"path": "tree/master/distribution",
171+
"pathParts": [
172+
"tree",
173+
"master",
174+
"distribution",
175+
],
176+
}
177+
`;
178+
179+
exports[`getRepositoryInfo 15`] = `
180+
{
181+
"name": "github-url-detection",
182+
"nameWithOwner": "refined-github/github-url-detection",
183+
"owner": "refined-github",
184+
"path": "",
185+
"pathParts": [],
186+
}
187+
`;
188+
189+
exports[`getRepositoryInfo 16`] = `
190+
{
191+
"name": "github-url-detection",
192+
"nameWithOwner": "refined-github/github-url-detection",
193+
"owner": "refined-github",
194+
"path": "",
195+
"pathParts": [],
196+
}
197+
`;
198+
199+
exports[`getRepositoryInfo 17`] = `
200+
{
201+
"name": "github-url-detection",
202+
"nameWithOwner": "refined-github/github-url-detection",
203+
"owner": "refined-github",
204+
"path": "blame/master/package.json",
205+
"pathParts": [
206+
"blame",
207+
"master",
208+
"package.json",
209+
],
210+
}
211+
`;
212+
213+
exports[`getRepositoryInfo 18`] = `
214+
{
215+
"name": "github-url-detection",
216+
"nameWithOwner": "refined-github/github-url-detection",
217+
"owner": "refined-github",
218+
"path": "commit/57bf4",
219+
"pathParts": [
220+
"commit",
221+
"57bf4",
222+
],
223+
}
224+
`;
225+
226+
exports[`getRepositoryInfo 19`] = `
227+
{
228+
"name": "github-url-detection",
229+
"nameWithOwner": "refined-github/github-url-detection",
230+
"owner": "refined-github",
231+
"path": "compare/test-branch",
232+
"pathParts": [
233+
"compare",
234+
"test-branch",
235+
],
236+
}
237+
`;
238+
239+
exports[`getRepositoryInfo 20`] = `
240+
{
241+
"name": "github-url-detection",
242+
"nameWithOwner": "refined-github/github-url-detection",
243+
"owner": "refined-github",
244+
"path": "tree/master/distribution",
245+
"pathParts": [
246+
"tree",
247+
"master",
248+
"distribution",
249+
],
250+
}
251+
`;
252+
253+
exports[`getRepositoryInfo 21`] = `
254+
{
255+
"name": "github-url-detection",
256+
"nameWithOwner": "refined-github/github-url-detection",
257+
"owner": "refined-github",
258+
"path": "tree/master/distribution",
259+
"pathParts": [
260+
"tree",
261+
"master",
262+
"distribution",
263+
],
264+
}
265+
`;

index.test.ts

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference lib="dom" />
22
import assert from 'node:assert/strict';
3-
import {test} from 'vitest';
3+
import {test, expect} from 'vitest';
44
import stripIndent from 'strip-indent';
55
import {getAllUrls, getTests} from './collector.js';
66
import * as pageDetect from './index.js';
@@ -153,47 +153,12 @@ test('getRepositoryInfo', () => {
153153
assert.equal(getRepositoryInfoAdapter('https://github.com'), undefined);
154154
assert.equal(getRepositoryInfoAdapter('https://gist.github.com/'), undefined);
155155
assert.equal(getRepositoryInfoAdapter('https://github.com/settings/developers'), undefined);
156-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection'), {
157-
owner: 'refined-github',
158-
name: 'github-url-detection',
159-
nameWithOwner: 'refined-github/github-url-detection',
160-
path: '',
161-
});
162-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/'), {
163-
owner: 'refined-github',
164-
name: 'github-url-detection',
165-
nameWithOwner: 'refined-github/github-url-detection',
166-
path: '',
167-
});
168-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/blame/master/package.json'), {
169-
owner: 'refined-github',
170-
name: 'github-url-detection',
171-
nameWithOwner: 'refined-github/github-url-detection',
172-
path: 'blame/master/package.json',
173-
});
174-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/commit/57bf4'), {
175-
owner: 'refined-github',
176-
name: 'github-url-detection',
177-
nameWithOwner: 'refined-github/github-url-detection',
178-
path: 'commit/57bf4',
179-
});
180-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/compare/test-branch?quick_pull=0'), {
181-
owner: 'refined-github',
182-
name: 'github-url-detection',
183-
nameWithOwner: 'refined-github/github-url-detection',
184-
path: 'compare/test-branch',
185-
});
186-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution'), {
187-
owner: 'refined-github',
188-
name: 'github-url-detection',
189-
nameWithOwner: 'refined-github/github-url-detection',
190-
path: 'tree/master/distribution',
191-
});
192-
assert.deepEqual(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution/'), {
193-
owner: 'refined-github',
194-
name: 'github-url-detection',
195-
nameWithOwner: 'refined-github/github-url-detection',
196-
path: 'tree/master/distribution',
197-
});
156+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection')).toMatchSnapshot();
157+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/')).toMatchSnapshot();
158+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/blame/master/package.json')).toMatchSnapshot();
159+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/commit/57bf4')).toMatchSnapshot();
160+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/compare/test-branch?quick_pull=0')).toMatchSnapshot();
161+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution')).toMatchSnapshot();
162+
expect(getRepositoryInfoAdapter('https://github.com/refined-github/github-url-detection/tree/master/distribution/')).toMatchSnapshot();
198163
}
199164
});

index.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,12 @@ export type RepositoryInfo = {
811811
@example '/user/repo/' -> ''
812812
@example '/settings/token/' -> undefined */
813813
path: string;
814+
815+
/** The `path` segments
816+
@example '/user/repo/' -> []
817+
@example '/user/repo/issues/' -> ['issues']
818+
@example '/user/repo/issues/new' -> ['issues', 'new'] */
819+
pathParts: string[];
814820
};
815821

816822
/**
@@ -840,12 +846,13 @@ const getRepo = (url?: URL | HTMLAnchorElement | Location | string): RepositoryI
840846
return;
841847
}
842848

843-
const [owner, name, ...path] = getCleanPathname(url).split('/') as [string, string, string];
849+
const [owner, name, ...pathParts] = getCleanPathname(url).split('/') as [string, string, string];
844850
return {
845851
owner,
846852
name,
853+
pathParts,
847854
nameWithOwner: `${owner}/${name}`,
848-
path: path.join('/'),
855+
path: pathParts.join('/'),
849856
};
850857
};
851858

0 commit comments

Comments
 (0)