tssentials 1.0.9 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1 +1,3 @@
1
- export { processIfPresent, ifPresent, apply, notEmpty, ifTruthy } from './util.js';
1
+ export { processIfPresent, ifPresent, apply, notEmpty, ifTruthy, } from './util.js';
2
+ export { useStoredData } from './use-stored-data/useStoredData.js';
3
+ export { DataVersionContextProvider as StoredDataVersionContextProvider } from './use-stored-data/version/DataVersionContextProvider.js';
package/dist/index.js CHANGED
@@ -1 +1,3 @@
1
- export { processIfPresent, ifPresent, apply, notEmpty, ifTruthy } from './util.js';
1
+ export { processIfPresent, ifPresent, apply, notEmpty, ifTruthy, } from './util.js';
2
+ export { useStoredData } from './use-stored-data/useStoredData.js';
3
+ export { DataVersionContextProvider as StoredDataVersionContextProvider } from './use-stored-data/version/DataVersionContextProvider.js';
@@ -0,0 +1,3 @@
1
+ import { z } from "zod";
2
+ import { Dispatch, SetStateAction } from "react";
3
+ export declare const useStoredData: (key?: string) => <SCHEMA extends z.ZodType>(schema: SCHEMA, defaultValue: z.infer<SCHEMA>) => [z.infer<SCHEMA>, Dispatch<SetStateAction<z.infer<SCHEMA>>>, () => void];
@@ -0,0 +1,42 @@
1
+ import { useEffect, useState } from "react";
2
+ import { apply } from "../util.js";
3
+ import { useDataVersionContext } from "./version/DataVersionContext.js";
4
+ const isUpdater = (value) => typeof value === 'function';
5
+ const load = (key, schema, defaultValue) => apply(localStorage.getItem(key), v => ({
6
+ obj: schema.parse(v !== null ? JSON.parse(v) : defaultValue),
7
+ raw: v !== null && v !== void 0 ? v : ''
8
+ }));
9
+ export const useStoredData = (key) => (schema, defaultValue) => {
10
+ var _a;
11
+ const [state, setState] = useState(key
12
+ ? load(key, schema, defaultValue)
13
+ : { obj: defaultValue, raw: '' });
14
+ const { state: versionContext, addVersion: addContextVersion } = useDataVersionContext();
15
+ const contextVersion = key ? (_a = versionContext[key]) !== null && _a !== void 0 ? _a : 0 : 0;
16
+ const [version, setVersion] = useState(contextVersion);
17
+ const addVersion = () => {
18
+ setVersion(v => v + 1);
19
+ if (key)
20
+ addContextVersion(key);
21
+ };
22
+ const loadVersion = () => {
23
+ if (key && contextVersion > version) {
24
+ setState(load(key, schema, defaultValue));
25
+ setVersion(contextVersion);
26
+ }
27
+ };
28
+ useEffect(() => {
29
+ if (key && state.raw !== localStorage.getItem(key)) {
30
+ localStorage.setItem(key, JSON.stringify(state.obj));
31
+ addVersion();
32
+ }
33
+ }, [state]);
34
+ useEffect(loadVersion, [key, contextVersion]);
35
+ return [
36
+ state.obj, // state
37
+ (a) => isUpdater(a) // setState
38
+ ? setState(s => ({ obj: a(s.obj), raw: '' }))
39
+ : setState(() => ({ obj: a, raw: '' })),
40
+ () => setState({ obj: defaultValue, raw: '' }), // reset
41
+ ];
42
+ };
@@ -0,0 +1,9 @@
1
+ export type DataVersionContextType = Record<string, number>;
2
+ export declare const DataVersionContext: import("react").Context<{
3
+ state: DataVersionContextType;
4
+ addVersion: (key: string) => void;
5
+ }>;
6
+ export declare const useDataVersionContext: () => {
7
+ state: DataVersionContextType;
8
+ addVersion: (key: string) => void;
9
+ };
@@ -0,0 +1,11 @@
1
+ import { createContext, useContext } from "react";
2
+ export const DataVersionContext = createContext(undefined);
3
+ export const useDataVersionContext = () => {
4
+ const context = useContext(DataVersionContext);
5
+ if (context) {
6
+ return context;
7
+ }
8
+ else {
9
+ throw new Error('DataVersionContext is missing');
10
+ }
11
+ };
@@ -0,0 +1,4 @@
1
+ import { ReactNode } from "react";
2
+ export declare const DataVersionContextProvider: (props: {
3
+ children: ReactNode;
4
+ }) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,8 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { useState } from "react";
3
+ import { apply } from "tssentials";
4
+ import { DataVersionContext } from "./DataVersionContext.js";
5
+ export const DataVersionContextProvider = (props) => {
6
+ const [state, setState] = useState({});
7
+ return (_jsx(DataVersionContext.Provider, { value: { state, addVersion: (k) => setState(s => (Object.assign(Object.assign({}, s), { [k]: apply(s[k], v => v ? v + 1 : 1) }))) }, children: props.children }));
8
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tssentials",
3
- "version": "1.0.9",
3
+ "version": "1.1.0",
4
4
  "description": "Essential functions and types for typescript projects.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,7 +15,8 @@
15
15
  "exports": {
16
16
  ".": {
17
17
  "import": "./dist/index.js",
18
- "types": "./dist/index.d.ts"
18
+ "types": "./dist/index.d.ts",
19
+ "default": "./dist/index.js"
19
20
  },
20
21
  "./dist/style.css": "./dist/style.css"
21
22
  },
@@ -28,8 +29,16 @@
28
29
  "license": "MIT",
29
30
  "devDependencies": {
30
31
  "@eslint/js": "^9.23.0",
32
+ "@types/react": "^18.3.12",
33
+ "@types/react-dom": "^18.3.1",
31
34
  "eslint": "^9.23.0",
35
+ "eslint-plugin-react-hooks": "^7.0.1",
32
36
  "typescript": "^5.8.2",
33
37
  "typescript-eslint": "^8.28.0"
38
+ },
39
+ "peerDependencies": {
40
+ "react": "^18.3.1",
41
+ "react-dom": "^18.3.1",
42
+ "zod": "^4.3.5"
34
43
  }
35
44
  }