-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
54fcf13
commit 319ea84
Showing
5 changed files
with
99 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./client.ts"; | ||
export * from "./urls.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\/)/; |