Repository navigation
Expand file tree
/
Copy pathview.js
More file actions
76 lines (68 loc) · 2.74 KB
/
Copy pathview.js
File metadata and controls
76 lines (68 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* @import { Role } from "./pull-request.js"
*
* @typedef {object} View - The URL parameters that decide which PRs a load finds and how it counts them, e.g. not `authors`, which only hides rows
* @property {string} repo
* @property {string | null} user - None for the signed-in user
* @property {Role | null} [role]
* @property {string | null} [filter] - Narrows the search further, e.g. `label:foo -label:bar`
* @property {string | null} [exclude] - A regular expression for the paths of files whose lines count apart, e.g. `test|\.lock$`
*/
import { normalize } from "./pull-request.js";
/** The URL parameters of a view, see {@link viewOf} */
export const viewNames = ["repo", "user", "role", "filter", "exclude"];
/** The URL parameters that only hide rows of a view */
export const pickNames = ["authors", "drafts"];
/** The URL parameters the page reads */
export const paramNames = [...viewNames, ...pickNames];
/**
* The view of these URL parameters, e.g. of a key from {@link viewKey}. A missing one means its default, e.g. no `user` is the signed-in user.
* @param {URLSearchParams | string} [input]
* @returns {View}
*/
export function viewOf (input = location.search) {
let params = new URLSearchParams(input);
return Object.fromEntries(viewNames.map(name => [name, params.get(name)]));
}
/**
* One string per view, e.g. `repo=foo%2Fbar&role=author`. Equal views have equal keys.
* @param {View} view
*/
export function viewKey (view) {
return `${new URLSearchParams(viewNames.filter(name => view[name]).map(name => [name, view[name]]))}`;
}
/**
* Rewrite URL parameters as GitHub’s pulls page rewrites its query, see {@link normalize}, e.g. `?role=author&user=foo` for `?role=author&filter=author:foo`.
* @param {URLSearchParams | FormData} params
*/
export function normalizeParams (params) {
let { user, filter, authors, drafts } = normalize(
Object.fromEntries(paramNames.map(name => [name, params.get(name)])),
);
assign(params, { user, filter, authors, drafts });
}
/**
* The page’s URL with these parameters changed, and rewritten as {@link normalizeParams} does, e.g. `href({ role: "author" })`. An empty value removes one.
* @param {Partial<Record<keyof View | "authors" | "drafts", string | null>>} changes
*/
export function href (changes) {
let params = new URLSearchParams(location.search);
assign(params, changes);
normalizeParams(params);
return `?${params}`;
}
/**
* Set each of these parameters, or delete it when its value is empty.
* @param {URLSearchParams | FormData} params
* @param {Record<string, string | null | undefined>} changes
*/
function assign (params, changes) {
for (let [name, value] of Object.entries(changes)) {
if (value) {
params.set(name, value);
}
else {
params.delete(name);
}
}
}