Skip to content

Commit 16c3a8a

Browse files
committed
promote getChrome to a utility
1 parent ff036e5 commit 16c3a8a

File tree

9 files changed

+23
-10
lines changed

9 files changed

+23
-10
lines changed

workspaces/leetcode-zen-mode/src/extension/getChrome.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

workspaces/leetcode-zen-mode/src/extension/useChromeStorage.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import immutableUpdate from "immutability-helper";
22
import { useSyncExternalStore } from "react";
33

4+
import { getChrome } from "@code-chronicles/util/browser-extensions/chrome/getChrome";
45
import { getUniqueId } from "@code-chronicles/util/getUniqueId";
56

6-
import { getChrome } from "./getChrome.ts";
7-
87
let storage: Record<string, unknown> | null = null;
98

109
async function writeChanges(): Promise<void> {

workspaces/leetcode-zen-mode/src/extension/usePreferredDifficulty.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { z } from "zod";
22

3-
import { getChrome } from "./getChrome.ts";
3+
import { getChrome } from "@code-chronicles/util/browser-extensions/chrome/getChrome";
4+
45
import { useChromeStorage } from "./useChromeStorage.ts";
56

67
export const DIFFICULTIES = ["Easy", "Medium", "Hard"] as const;

workspaces/util/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@code-chronicles/repository-scripts": "workspace:*",
3535
"@code-chronicles/util": "workspace:*",
3636
"@jest/globals": "29.7.0",
37+
"@types/chrome": "patch:@types/chrome@npm%3A0.0.280#~/.yarn/patches/@types-chrome-npm-0.0.280-34ee4ba05a.patch",
3738
"@types/node": "22.9.0",
3839
"eslint": "9.14.0",
3940
"jest": "29.7.0",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Chrome } from "chrome";
2+
3+
export function getChrome(): Chrome | undefined {
4+
return typeof chrome === "object"
5+
? (chrome as unknown as Chrome | undefined)
6+
: undefined;
7+
}

workspaces/util/src/lastOrThrow.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ import invariant from "invariant";
22

33
export function lastOrThrow<T>(array: readonly T[]): T {
44
invariant(array.length > 0, "Expected a non-empty array!");
5+
6+
// The cast is safe because we checked the size of the array.
7+
// Not using `nullthrows` because `T` could be a union that includes
8+
// `null` or `undefined`.
59
return array.at(-1) as T;
610
}

workspaces/util/src/maybeThrow.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
export function maybeThrow(errors: readonly [unknown, unknown[]]): never;
1+
import type { NonEmptyArray } from "@code-chronicles/util/NonEmptyArray";
2+
3+
export function maybeThrow(errors: Readonly<NonEmptyArray<unknown>>): never;
4+
25
export function maybeThrow(errors: readonly unknown[]): void;
6+
37
export function maybeThrow(errors: readonly unknown[]): void {
48
if (errors.length === 0) {
59
return;

workspaces/util/src/popMany.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export function popMany<T>(array: T[], count: number): T[] {
99
const res: T[] = [];
1010
while (array.length > 0 && res.length < count) {
1111
// The cast is safe because we checked the size of the array.
12-
// Not using `nullthrows` because `T` could be a union that includes `undefined`.
12+
// Not using `nullthrows` because `T` could be a union that includes
13+
// `null` or `undefined`.
1314
res.push(array.pop() as T);
1415
}
1516

yarn.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)