Skip to content

Commit

Permalink
Refactor parseSubmissionID (#25)
Browse files Browse the repository at this point in the history
* Refactor parseSubmissionID

TODO: Add unit testing
Co-Authored-By: Ethan Davidson <[email protected]>

* split `lib/lc/client.ts` into separate files

* Create client_test.ts

---------

Co-authored-by: Ethan Davidson <[email protected]>
  • Loading branch information
karnikaavelumani and EthanThatOneKid authored Sep 10, 2023
1 parent 54fcf13 commit 319ea84
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 35 deletions.
38 changes: 3 additions & 35 deletions lib/lc/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { makeQuestionURL } from "./urls.ts";
import { gql } from "./gql.ts";

/**
* DailyQuestion is the representation of Leetcode's daily question.
*
Expand Down Expand Up @@ -181,38 +184,3 @@ export class LCClient {
);
}
}

function makeQuestionURL(titleSlug: string): string {
return `https://leetcode.com/problems/${titleSlug}/`;
}

/**
* gql executes a query to Leetcode's GraphQL API.
*/
async function gql(body: string): Promise<Response> {
return await fetch("https://leetcode.com/graphql/", {
method: "POST",
headers: {
accept: "*/*",
"accept-language": "en-US,en;q=0.9",
authorization: "",
"content-type": "application/json",
},
body,
});
}

/**
* parseSubmissionID parses the submission ID from the submission URL.
*/
export function parseSubmissionID(submissionURLOrID: string): string {
if (LEETCODE_SUBMISSIONS_URL_PATTERN.test(submissionURLOrID)) {
return submissionURLOrID.replace(LEETCODE_SUBMISSIONS_URL_PATTERN, "")
.replace(/\/$/, "");
}

return submissionURLOrID;
}

const LEETCODE_SUBMISSIONS_URL_PATTERN =
/^https:\/\/leetcode\.com\/(problems\/.*\/submissions\/|submissions\/detail\/)/;
43 changes: 43 additions & 0 deletions lib/lc/client_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { assertEquals } from "lc-dailies/deps.ts";
import { makeQuestionURL, parseSubmissionID } from "./urls.ts";

Deno.test("makeQuestionURL", () => {
assertEquals(
makeQuestionURL("implement-stack-using-queues"),
"https://leetcode.com/problems/implement-stack-using-queues/",
);
});

Deno.test("parseSubmissionID full URL", () => {
assertEquals(
parseSubmissionID(
"https://leetcode.com/problems/implement-stack-using-queues/submissions/1035629181/",
),
"1035629181",
);
});

Deno.test("parseSubmissionID detail URL", () => {
assertEquals(
parseSubmissionID(
"https://leetcode.com/submissions/detail/1035629181/",
),
"1035629181",
);
});

Deno.test("parseSubmissionID submission ID ignores search params", () => {
assertEquals(
parseSubmissionID(
"https://leetcode.com/problems/unique-paths/submissions/1039832006/?envType=daily-question&envId=2023-09-03",
),
"1039832006",
);
});

Deno.test("parseSubmissionID submission ID", () => {
assertEquals(
parseSubmissionID("1035629181"),
"1035629181",
);
});
15 changes: 15 additions & 0 deletions lib/lc/gql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* gql executes a query to Leetcode's GraphQL API.
*/
export async function gql(body: string): Promise<Response> {
return await fetch("https://leetcode.com/graphql/", {
method: "POST",
headers: {
accept: "*/*",
"accept-language": "en-US,en;q=0.9",
authorization: "",
"content-type": "application/json",
},
body,
});
}
1 change: 1 addition & 0 deletions lib/lc/mod.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./client.ts";
export * from "./urls.ts";
37 changes: 37 additions & 0 deletions lib/lc/urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* makeQuestionURL makes a Leetcode question URL from the title slug.
*/
export function makeQuestionURL(titleSlug: string): string {
return `https://leetcode.com/problems/${titleSlug}/`;
}

/**
* parseSubmissionID parses the submission ID from the submission URL.
*/
export function parseSubmissionID(submissionURLOrID: string): string {
let submissionID = submissionURLOrID;
try {
const url = new URL(submissionURLOrID);
if (LEETCODE_SUBMISSIONS_PATHNAME_PATTERN.test(url.pathname)) {
submissionID = url.pathname
.replace(LEETCODE_SUBMISSIONS_PATHNAME_PATTERN, "")
.replace(/\/$/, "");
}
} catch {
// noop
}
return submissionID;
}

/**
* LEETCODE_SUBMISSIONS_PATHNAME_PATTERN is the pattern for Leetcode's
* submission URLs.
*
* Valid submission URLs: This entails a full URL or the direct submission ID.
* https://leetcode.com/problems/implement-stack-using-queues/submissions/1035629181/
* https://leetcode.com/submissions/detail/1035629181/
* 1035629181
* https://leetcode.com/problems/unique-paths/submissions/1039832006/?envType=daily-question&envId=2023-09-03
*/
const LEETCODE_SUBMISSIONS_PATHNAME_PATTERN =
/^\/(problems\/.*\/submissions\/|submissions\/detail\/)/;

0 comments on commit 319ea84

Please sign in to comment.