Skip to content

Commit

Permalink
Merge pull request #19 from gritzkoo/master
Browse files Browse the repository at this point in the history
Adjust build steps to complience to npm
  • Loading branch information
joaomantovani committed Dec 29, 2021
2 parents 33161eb + d6bb139 commit ece1354
Show file tree
Hide file tree
Showing 13 changed files with 8,794 additions and 3,144 deletions.
5,786 changes: 5,685 additions & 101 deletions package-lock.json

Large diffs are not rendered by default.

46 changes: 24 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
{
"name": "@penseapp/uselocalstoragereducer",
"version": "1.1.8",
"version": "2.0.0",
"description": "A react hooks to use useReducer with localStorage persist",
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"private": false,
"repository": {
"type": "git",
"url": "git+https://github.com/penseapp/useLocalStorageReducer.git"
},
"homepage": "https://penseapp-uselocalstoragereduce.web.app",
"keywords": [
"react",
"hooks",
"localStorage",
"useReducer"
],
"files": [
"dist/**/*"
],
"author": "@joaomantovani",
"license": "MIT",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "FORCE_COLOR=true eslint ./**/*.{ts,tsx} --max-warnings=0 --format=codeframe",
"prettier": "FORCE_COLOR=true prettier --write './**/*.{jsx,tsx,ts,js,md}' && prettier --write './README.md'",
"i-all": "npm i && cd playground && npm i",
"build": "rollup -c",
"build2": "tsc",
"build-watch": "rollup -c -w",
"build:all": "npm-run-all build build:playground",
"build:all": "npm-run-all build build:playground build2",
"start-playground": "cd playground && npm run start",
"build:playground": "cd playground && npm run build",
"install:playground": "cd playground && npm i-all",
Expand All @@ -20,19 +39,6 @@
"clean": "rm -rf ./node_modules && rm package-lock.json",
"clean:all": "npm-run-all --parallel clean clean:playground"
},
"repository": {
"type": "git",
"url": "git+https://github.com/penseapp/useLocalStorageReducer.git"
},
"homepage": "https://penseapp-uselocalstoragereduce.web.app",
"keywords": [
"react",
"hooks",
"localStorage",
"useReducer"
],
"author": "@joaomantovani",
"license": "MIT",
"devDependencies": {
"@commitlint/cli": "^11.0.0",
"@commitlint/config-conventional": "^11.0.0",
Expand All @@ -54,17 +60,13 @@
"rollup-plugin-delete": "^2.0.0",
"rollup-plugin-prettier": "^2.1.0",
"rollup-plugin-typescript2": "^0.30.0",
"rollup-plugin-uglify": "^6.0.4",
"rollup-plugin-uglify": "^5.0.2",
"typescript": "^4.2.2"
},
"peerDependencies": {
"react": "^17.0.1",
"react-dom": "^17.0.1"
},
"files": [
"dist",
"package.json"
],
"dependencies": {
"expired-storage": "^1.0.2"
},
Expand Down
10 changes: 8 additions & 2 deletions playground/src/component-lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
export { useLocalStorageReducer } from "./useLocalStorageReducer";
//# sourceMappingURL=index.d.ts.map
import { Reducer, ReducerState, Dispatch, ReducerAction } from "react";
export declare function useLocalStorageReducer<I, A>(
key: string,
reducer: Reducer<I, A>,
initialState: I,
expire?: number | boolean
): [ReducerState<Reducer<I, A>>, Dispatch<ReducerAction<Reducer<I, A>>>];
//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion playground/src/component-lib/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

56 changes: 55 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
export { useLocalStorageReducer } from "./useLocalStorageReducer";
import {
useEffect,
useReducer,
Reducer,
ReducerState,
Dispatch,
ReducerAction,
} from "react";
import ExpiredStorage from "expired-storage";

export function useLocalStorageReducer<I, A>(
key: string,
reducer: Reducer<I, A>,
initialState: I,
expire: number | boolean = 60 * 30
): [ReducerState<Reducer<I, A>>, Dispatch<ReducerAction<Reducer<I, A>>>] {
const [state, dispatch] = useReducer(
reducer,
initialState,
(initialState) => {
try {
const expiredStorage = new ExpiredStorage();

// Get from local storage by key
const item = expiredStorage.getItem(key);

const parsedInitialValue = JSON.stringify(initialState);

// Parse stored json or if none return initialValue
return item ? JSON.parse(item) : JSON.parse(parsedInitialValue);
} catch (error) {
// If error also return initialValue
console.error(error);
return initialState;
}
}
);

useEffect(() => {
try {
const expiredStorage = new ExpiredStorage();

expire !== false && typeof expire === "number"
? expiredStorage.setItem(key, JSON.stringify(state), expire)
: window.localStorage.setItem(key, JSON.stringify(state));
} catch (error) {
// TODO: A more advanced implementation would handle the error case
console.error(error);
}

localStorage.setItem(key, JSON.stringify(state));
}, [expire, key, state]);

return [state, dispatch];
}
1 change: 0 additions & 1 deletion src/useLocalStorageReducer/index.ts

This file was deleted.

57 changes: 0 additions & 57 deletions src/useLocalStorageReducer/useLocalStorageReducer.tsx

This file was deleted.

7 changes: 4 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"declarationMap": true /* Generates a sourcemap for each corresponding '.d.ts' file. */,
"sourceMap": true /* Generates corresponding '.map' file. */,
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "." /* Redirect output structure to the directory. */,
"outDir": "./dist" /* Redirect output structure to the directory. */,
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
Expand Down Expand Up @@ -44,7 +44,7 @@
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */

/* Module Resolution Options */
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
// "moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
Expand All @@ -69,5 +69,6 @@
"skipLibCheck": true /* Skip type checking of declaration files. */,
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": ["src/**/*"]
"include": ["src/**/*", "README.md"],
"exclude": ["node_modules", "**/*.test.ts"]
}
Loading

0 comments on commit ece1354

Please sign in to comment.