Skip to content

Commit

Permalink
Allow each req key in the URL hash to have comma-separated multiple…
Browse files Browse the repository at this point in the history
… requirements (#595)
  • Loading branch information
whitphx authored Jul 24, 2023
1 parent 1eea90e commit e5c0bda
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
18 changes: 16 additions & 2 deletions packages/sharing-common/src/url.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, test, expect } from "vitest";
import { processGitblobUrl } from "./url";
import { describe, it, test, expect } from "vitest";
import { processGitblobUrl, parseHash } from "./url";

describe("processGitblobUrl", () => {
const githubUrls: [string, string][] = [
Expand Down Expand Up @@ -73,3 +73,17 @@ describe("processGitblobUrl", () => {
});
});
});

describe("parseHash", () => {
const reqTestCases: [string, string[]][] = [
["code=xxx&req=foo", ["foo"]],
["code=xxx&req=foo&req=bar", ["foo", "bar"]],
["code=xxx&req=foo&req=bar&req=baz", ["foo", "bar", "baz"]],
["code=xxx&req=foo&req=bar,baz", ["foo", "bar", "baz"]],
];
reqTestCases.forEach(([hash, requirements]) => {
it(`parses requirements from "${hash}"`, () => {
expect(parseHash(hash)).toEqual({ requirements, code: "xxx" });
});
});
});
9 changes: 7 additions & 2 deletions packages/sharing-common/src/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function processGitblobUrl(url: string): string {
return url + "/raw";
}

function parseHash(
export function parseHash(
hashValue: string
):
| { url: string; requirements: string[] }
Expand All @@ -49,7 +49,12 @@ function parseHash(
const params = new URLSearchParams(hashValue);
const url = params.get("url");
const code = params.get("code");
const requirements = params.getAll("req");
const requirements = params
.getAll("req")
.map((r) => r.split(","))
.flat()
.map((r) => r.trim())
.filter((r) => r.length > 0);

if (code) {
if (url) {
Expand Down

0 comments on commit e5c0bda

Please sign in to comment.