Skip to content

Commit 793dcd7

Browse files
committed
WIP
1 parent 21f49bd commit 793dcd7

45 files changed

Lines changed: 74 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// TODO - fix tests
2+
3+
import { it } from "vitest";
4+
import { Equal, Expect } from "../helpers/type-utils";
5+
import { z } from "zod";
6+
7+
export const useLocalStorage = <T,>(prefix: string, schema: any) => {
8+
const schemaOrNull = schema.nullable();
9+
return {
10+
get: (key: string): T | null => {
11+
return schemaOrNull.parse(
12+
JSON.parse(window.localStorage.getItem(prefix + key) || "null"),
13+
);
14+
},
15+
set: (key: string, value: T) => {
16+
window.localStorage.setItem(prefix + key, JSON.stringify(value));
17+
},
18+
};
19+
};
20+
21+
const user = useLocalStorage("user", z.object({ name: z.string() }));
22+
23+
it("Should let you set and get values", () => {
24+
user.set("matt", { name: "Matt" });
25+
26+
const mattUser = user.get("matt");
27+
28+
type tests = [Expect<Equal<typeof mattUser, { name: string } | null>>];
29+
});
30+
31+
it("Should not let you set a value that is not the same type as the type argument passed", () => {
32+
user.set(
33+
"something",
34+
// @ts-expect-error
35+
{},
36+
);
37+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// TODO - fix tests
2+
3+
import { it } from "vitest";
4+
import { Equal, Expect } from "../helpers/type-utils";
5+
import { z } from "zod";
6+
7+
export const useLocalStorage = <T,>(prefix: string, schema: z.Schema<T>) => {
8+
const schemaOrNull = schema.nullable();
9+
return {
10+
get: (key: string): T | null => {
11+
return schemaOrNull.parse(
12+
JSON.parse(window.localStorage.getItem(prefix + key) || "null"),
13+
);
14+
},
15+
set: (key: string, value: T) => {
16+
window.localStorage.setItem(prefix + key, JSON.stringify(value));
17+
},
18+
};
19+
};
20+
21+
const user = useLocalStorage("user", z.object({ name: z.string() }));
22+
23+
it("Should let you set and get values", () => {
24+
user.set("matt", { name: "Matt" });
25+
26+
const mattUser = user.get("matt");
27+
28+
type tests = [Expect<Equal<typeof mattUser, { name: string } | null>>];
29+
});
30+
31+
it("Should not let you set a value that is not the same type as the type argument passed", () => {
32+
user.set(
33+
"something",
34+
// @ts-expect-error
35+
{},
36+
);
37+
});
File renamed without changes.

src/03-custom-hooks/23-unions-in-usestate.solution.tsx renamed to src/03-custom-hooks/24-unions-in-usestate.solution.tsx

File renamed without changes.

src/03-custom-hooks/24-discriminated-unions-in-usestate.problem.tsx renamed to src/03-custom-hooks/25-discriminated-unions-in-usestate.problem.tsx

File renamed without changes.

src/03-custom-hooks/24-discriminated-unions-in-usestate.solution.tsx renamed to src/03-custom-hooks/25-discriminated-unions-in-usestate.solution.tsx

File renamed without changes.

src/03-custom-hooks/22.5-generic-localstorage-hook-with-zod.problem.tsx renamed to src/03-custom-hooks/26-discriminated-union-returns-from-custom-hooks.problem.ts

File renamed without changes.

src/03-custom-hooks/26-discriminated-tuples-from-custom-hooks.problem.tsx renamed to src/03-custom-hooks/27-discriminated-tuples-from-custom-hooks.problem.tsx

File renamed without changes.

src/03-custom-hooks/26-discriminated-tuples-from-custom-hooks.solution.tsx renamed to src/03-custom-hooks/27-discriminated-tuples-from-custom-hooks.solution.tsx

File renamed without changes.

src/03-custom-hooks/25-discriminated-union-returns-from-custom-hooks.problem.ts renamed to src/03-custom-hooks/28-hooks-from-external-libraries.problem.ts

File renamed without changes.

0 commit comments

Comments
 (0)