diff --git a/app/converters/json-yaml/page.tsx b/app/converters/json-yaml/page.tsx index c4efed1..4a51283 100644 --- a/app/converters/json-yaml/page.tsx +++ b/app/converters/json-yaml/page.tsx @@ -1,7 +1,6 @@ "use client"; import { useCallback, useMemo, useState } from "react"; -import * as O from "fp-ts/lib/Option"; import yaml from "js-yaml"; import { toolGroups } from "@/config/tools"; @@ -45,7 +44,7 @@ export default function Page() { setForm(prev => ({ ...prev, json: text, - yaml: O.isNone(parsed) + yaml: parsed.isErr() ? "" : yaml.dump(parsed.value, { indent: prev.indentation.length, quotingType: '"' }), })); @@ -57,7 +56,7 @@ export default function Page() { setForm(prev => ({ ...prev, yaml: text, - json: O.isNone(parsed) ? "" : JSON.stringify(parsed.value, null, prev.indentation), + json: parsed.isErr() ? "" : JSON.stringify(parsed.value, null, prev.indentation), })); }, []); @@ -68,7 +67,7 @@ export default function Page() { const onIndentationChange: SelectProps["onValueChange"] = value => { const parsed = safeJsonParse(form.json); - const jsonYaml = O.isNone(parsed) + const jsonYaml = parsed.isErr() ? { json: "", yaml: "" } : { json: JSON.stringify(parsed.value, null, value), diff --git a/app/encoders-decoders/jwt/page.tsx b/app/encoders-decoders/jwt/page.tsx index ac09bd3..f28d7e8 100644 --- a/app/encoders-decoders/jwt/page.tsx +++ b/app/encoders-decoders/jwt/page.tsx @@ -1,7 +1,6 @@ "use client"; import { useCallback, useMemo, useState } from "react"; -import * as O from "fp-ts/lib/Option"; import { toolGroups } from "@/config/tools"; import { decode } from "@/lib/jwt"; @@ -21,8 +20,8 @@ export default function Page() { ); const { header: h, payload: p } = decode(jwt); - const header = O.isNone(h) ? "" : JSON.stringify(h.value, null, 2); - const payload = O.isNone(p) ? "" : JSON.stringify(p.value, null, 2); + const header = h.isErr() ? "" : JSON.stringify(h.value, null, 2); + const payload = p.isErr() ? "" : JSON.stringify(p.value, null, 2); const clearJwt = useCallback(() => setJwt(""), []); diff --git a/app/encoders-decoders/url/page.tsx b/app/encoders-decoders/url/page.tsx index eac521a..7bb545e 100644 --- a/app/encoders-decoders/url/page.tsx +++ b/app/encoders-decoders/url/page.tsx @@ -1,8 +1,6 @@ "use client"; import { useCallback, useMemo, useState } from "react"; -import { constant } from "fp-ts/lib/function"; -import * as O from "fp-ts/lib/Option"; import { toolGroups } from "@/config/tools"; import { safeDecodeURIComponent, safeEncodeURIComponent } from "@/lib/uri"; @@ -24,14 +22,14 @@ export default function Page() { const setDecodedReactively = useCallback((text: string) => { setForm({ decoded: text, - encoded: O.getOrElse(constant(""))(safeEncodeURIComponent(text)), + encoded: safeEncodeURIComponent(text).unwrapOr(""), }); }, []); const setEncodedReactively = useCallback((text: string) => { setForm({ encoded: text, - decoded: O.getOrElse(constant(""))(safeDecodeURIComponent(text)), + decoded: safeDecodeURIComponent(text).unwrapOr(""), }); }, []); diff --git a/app/formatters/json/page.tsx b/app/formatters/json/page.tsx index db19e72..8467728 100644 --- a/app/formatters/json/page.tsx +++ b/app/formatters/json/page.tsx @@ -1,7 +1,6 @@ "use client"; import { useCallback, useMemo, useState } from "react"; -import * as O from "fp-ts/lib/Option"; import { toolGroups } from "@/config/tools"; import { safeJsonParse } from "@/lib/json"; @@ -36,7 +35,7 @@ export default function Page() { const [input, setInput] = useState('{\n"foo":"bar"\n}'); const parsed = safeJsonParse(input); - const output = O.isNone(parsed) ? "" : JSON.stringify(parsed.value, null, indentation); + const output = parsed.isErr() ? "" : JSON.stringify(parsed.value, null, indentation); const clearInput = useCallback(() => setInput(""), []); diff --git a/lib/json.ts b/lib/json.ts index 5499fce..9a8ecf5 100644 --- a/lib/json.ts +++ b/lib/json.ts @@ -1,3 +1,3 @@ -import { tryCatchK } from "fp-ts/lib/Option"; +import { fromThrowable } from "neverthrow"; -export const safeJsonParse = tryCatchK(JSON.parse); +export const safeJsonParse = fromThrowable(JSON.parse); diff --git a/lib/jwt.ts b/lib/jwt.ts index afe9e3f..c1efee5 100644 --- a/lib/jwt.ts +++ b/lib/jwt.ts @@ -1,11 +1,11 @@ -import * as O from "fp-ts/lib/Option"; import jwt_decode from "jwt-decode"; +import { err, fromThrowable, Result } from "neverthrow"; -const safeJwtDecode = O.tryCatchK(jwt_decode); +const safeJwtDecode = fromThrowable(jwt_decode); export const decode = (token: string) => { - let header: O.Option> = O.none; - let payload: O.Option = O.none; + let header: Result = err(""); + let payload: Result = err(""); if (token.split(".").length === 3) { header = safeJwtDecode(token, { header: true }); diff --git a/lib/uri.ts b/lib/uri.ts index c02d85b..0e81ab4 100644 --- a/lib/uri.ts +++ b/lib/uri.ts @@ -1,4 +1,4 @@ -import { tryCatchK } from "fp-ts/lib/Option"; +import { fromThrowable } from "neverthrow"; -export const safeEncodeURIComponent = tryCatchK(encodeURIComponent); -export const safeDecodeURIComponent = tryCatchK(decodeURIComponent); +export const safeEncodeURIComponent = fromThrowable(encodeURIComponent); +export const safeDecodeURIComponent = fromThrowable(decodeURIComponent); diff --git a/lib/yaml.ts b/lib/yaml.ts index 40315db..c8b0fb2 100644 --- a/lib/yaml.ts +++ b/lib/yaml.ts @@ -1,4 +1,4 @@ -import { tryCatchK } from "fp-ts/lib/Option"; import yaml from "js-yaml"; +import { fromThrowable } from "neverthrow"; -export const safeYamlParse = tryCatchK(yaml.load); +export const safeYamlParse = fromThrowable(yaml.load); diff --git a/package.json b/package.json index ecb7da6..77e7641 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "js-yaml": "^4.1.0", "jwt-decode": "^3.1.2", "lucide-react": "^0.221.0", + "neverthrow": "^6.0.0", "next": "13.4.4", "next-themes": "^0.2.1", "react": "18.2.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9b4891c..d976acc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -61,6 +61,9 @@ dependencies: lucide-react: specifier: ^0.221.0 version: 0.221.0(react@18.2.0) + neverthrow: + specifier: ^6.0.0 + version: 6.0.0 next: specifier: 13.4.4 version: 13.4.4(@babel/core@7.22.1)(react-dom@18.2.0)(react@18.2.0) @@ -3476,6 +3479,10 @@ packages: resolution: {integrity: sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw==} dev: true + /neverthrow@6.0.0: + resolution: {integrity: sha512-kPZKRs4VkdloCGQXPoP84q4sT/1Z+lYM61AXyV8wWa2hnuo5KpPBF2S3crSFnMrOgUISmEBP8Vo/ngGZX60NhA==} + dev: false + /next-themes@0.2.1(next@13.4.4)(react-dom@18.2.0)(react@18.2.0): resolution: {integrity: sha512-B+AKNfYNIzh0vqQQKqQItTS8evEouKD7H5Hj3kmuPERwddR2TxvDSFZuTj6T7Jfn1oyeUyJMydPl1Bkxkh0W7A==} peerDependencies: