From 87b981d511c0c31a3a91424f110ffe389576fec1 Mon Sep 17 00:00:00 2001 From: dimaslz Date: Sun, 10 Mar 2024 18:58:47 +0100 Subject: [PATCH] fix(hooks/utils): move utils to hooks --- src/hooks/useDebounceFn/useDebounceFn.test.ts | 2 +- src/hooks/useDocumentTitle/useDocumentTitle.test.ts | 2 +- src/hooks/useLocalStorage/useLocalStorage.ts | 2 +- src/hooks/useSessionStorage/useSessionStorage.ts | 2 +- src/hooks/useThrottleFn/useThrottleFn.test.ts | 2 +- src/{ => hooks}/utils/index.d.ts | 0 src/{ => hooks}/utils/index.ts | 0 src/{ => hooks}/utils/parse-json.d.ts | 0 src/{ => hooks}/utils/parse-json.test.ts | 13 ++++++++++++- src/{ => hooks}/utils/parse-json.ts | 0 src/{ => hooks}/utils/watch.d.ts | 0 src/{ => hooks}/utils/watch.ts | 0 12 files changed, 17 insertions(+), 6 deletions(-) rename src/{ => hooks}/utils/index.d.ts (100%) rename src/{ => hooks}/utils/index.ts (100%) rename src/{ => hooks}/utils/parse-json.d.ts (100%) rename src/{ => hooks}/utils/parse-json.test.ts (65%) rename src/{ => hooks}/utils/parse-json.ts (100%) rename src/{ => hooks}/utils/watch.d.ts (100%) rename src/{ => hooks}/utils/watch.ts (100%) diff --git a/src/hooks/useDebounceFn/useDebounceFn.test.ts b/src/hooks/useDebounceFn/useDebounceFn.test.ts index 8119fa7..b733eac 100644 --- a/src/hooks/useDebounceFn/useDebounceFn.test.ts +++ b/src/hooks/useDebounceFn/useDebounceFn.test.ts @@ -1,4 +1,4 @@ -import { useDebounceFn } from ".."; +import { useDebounceFn } from "@/hooks"; vi.useFakeTimers(); diff --git a/src/hooks/useDocumentTitle/useDocumentTitle.test.ts b/src/hooks/useDocumentTitle/useDocumentTitle.test.ts index 96b53f4..14ecc89 100644 --- a/src/hooks/useDocumentTitle/useDocumentTitle.test.ts +++ b/src/hooks/useDocumentTitle/useDocumentTitle.test.ts @@ -1,6 +1,6 @@ import { get } from "svelte/store"; -import { useDocumentTitle } from ".."; +import { useDocumentTitle } from "@/hooks"; describe("Hooks - useDocumentTitle", () => { test("update document title", () => { diff --git a/src/hooks/useLocalStorage/useLocalStorage.ts b/src/hooks/useLocalStorage/useLocalStorage.ts index dbe022a..d9d310d 100644 --- a/src/hooks/useLocalStorage/useLocalStorage.ts +++ b/src/hooks/useLocalStorage/useLocalStorage.ts @@ -1,7 +1,7 @@ import { get } from "svelte/store"; import { type TsetValue, useEventListener, useState } from "@/hooks"; -import { parseJSON } from "@/utils"; +import { parseJSON } from "@/hooks/utils"; declare global { interface WindowEventMap { diff --git a/src/hooks/useSessionStorage/useSessionStorage.ts b/src/hooks/useSessionStorage/useSessionStorage.ts index bc689a4..7a40060 100644 --- a/src/hooks/useSessionStorage/useSessionStorage.ts +++ b/src/hooks/useSessionStorage/useSessionStorage.ts @@ -1,7 +1,7 @@ import { get } from "svelte/store"; import { type TsetValue, useEventListener, useState } from "@/hooks"; -import { parseJSON } from "@/utils"; +import { parseJSON } from "@/hooks/utils"; declare global { interface WindowEventMap { diff --git a/src/hooks/useThrottleFn/useThrottleFn.test.ts b/src/hooks/useThrottleFn/useThrottleFn.test.ts index b23acf0..9a48c86 100644 --- a/src/hooks/useThrottleFn/useThrottleFn.test.ts +++ b/src/hooks/useThrottleFn/useThrottleFn.test.ts @@ -1,4 +1,4 @@ -import { useThrottleFn } from ".."; +import { useThrottleFn } from "@/hooks"; vi.useFakeTimers(); diff --git a/src/utils/index.d.ts b/src/hooks/utils/index.d.ts similarity index 100% rename from src/utils/index.d.ts rename to src/hooks/utils/index.d.ts diff --git a/src/utils/index.ts b/src/hooks/utils/index.ts similarity index 100% rename from src/utils/index.ts rename to src/hooks/utils/index.ts diff --git a/src/utils/parse-json.d.ts b/src/hooks/utils/parse-json.d.ts similarity index 100% rename from src/utils/parse-json.d.ts rename to src/hooks/utils/parse-json.d.ts diff --git a/src/utils/parse-json.test.ts b/src/hooks/utils/parse-json.test.ts similarity index 65% rename from src/utils/parse-json.test.ts rename to src/hooks/utils/parse-json.test.ts index 4221046..14270d4 100644 --- a/src/utils/parse-json.test.ts +++ b/src/hooks/utils/parse-json.test.ts @@ -1,4 +1,15 @@ -import { parseJSON } from "@/utils"; +export function parseJSON(value: string | null | undefined): T | undefined { + try { + return value === "undefined" + ? undefined + : typeof value === "string" && value !== "true" && value !== "false" + ? value + : JSON.parse(value ?? ""); + } catch { + console.log("parsing error on", { value }); + return undefined; + } +} describe("Utils - parseJSON", () => { test("parse JSON", () => { diff --git a/src/utils/parse-json.ts b/src/hooks/utils/parse-json.ts similarity index 100% rename from src/utils/parse-json.ts rename to src/hooks/utils/parse-json.ts diff --git a/src/utils/watch.d.ts b/src/hooks/utils/watch.d.ts similarity index 100% rename from src/utils/watch.d.ts rename to src/hooks/utils/watch.d.ts diff --git a/src/utils/watch.ts b/src/hooks/utils/watch.ts similarity index 100% rename from src/utils/watch.ts rename to src/hooks/utils/watch.ts