@plq/use-persisted-state 1.1.5 → 1.2.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/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # usePersistedState
2
+ [![npm version](https://badge.fury.io/js/@plq%2Fuse-persisted-state.svg)](https://www.npmjs.com/package/@plq/use-persisted-state)
3
+ [![Tests](https://github.com/Akurganow/use-persisted-state/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/Akurganow/use-persisted-state/actions/workflows/main.yml)
2
4
 
3
5
  Persists the state to localStorage, sessionStorage or any custom storage
4
6
 
@@ -7,7 +9,7 @@ Persists the state to localStorage, sessionStorage or any custom storage
7
9
  - Persist the state to `localStorage`, `sessionStorage` or almost anything else implements [storage API](https://github.com/Akurganow/use-persisted-state/blob/master/docs/storage-api.md)
8
10
  - The state factory takes as many keys as needed, so you don't have to call the factory for each variable
9
11
  - Written with the TypeScript, the definitions go with the library
10
- - No dependencies
12
+ - No third-party dependencies
11
13
 
12
14
  ## Example
13
15
 
@@ -1,12 +1,9 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const is_function_1 = __importDefault(require("./is-function"));
3
+ const is_1 = require("@plq/is");
7
4
  function default_1(newState, state) {
8
5
  let newValue;
9
- if ((0, is_function_1.default)(newState)) {
6
+ if ((0, is_1.isFunction)(newState)) {
10
7
  newValue = newState(state);
11
8
  }
12
9
  else {
@@ -1 +1 @@
1
- {"version":3,"file":"get-new-value.js","sourceRoot":"","sources":["../../src/utils/get-new-value.ts"],"names":[],"mappings":";;;;;AACA,gEAAsC;AAEtC,mBAA4B,QAAiC,EAAE,KAAQ;IACrE,IAAI,QAAW,CAAA;IAEf,IAAI,IAAA,qBAAU,EAAC,QAAQ,CAAC,EAAE;QACxB,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;KAC3B;SAAM;QACL,QAAQ,GAAG,QAAQ,CAAA;KACpB;IAED,OAAO,QAAQ,CAAA;AACjB,CAAC;AAVD,4BAUC"}
1
+ {"version":3,"file":"get-new-value.js","sourceRoot":"","sources":["../../src/utils/get-new-value.ts"],"names":[],"mappings":";;AACA,gCAAoC;AAEpC,mBAA4B,QAAiC,EAAE,KAAQ;IACrE,IAAI,QAAW,CAAA;IAEf,IAAI,IAAA,eAAU,EAAC,QAAQ,CAAC,EAAE;QACxB,QAAQ,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAA;KAC3B;SAAM;QACL,QAAQ,GAAG,QAAQ,CAAA;KACpB;IAED,OAAO,QAAQ,CAAA;AACjB,CAAC;AAVD,4BAUC"}
@@ -1,9 +1,6 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const is_function_1 = __importDefault(require("./is-function"));
3
+ const is_1 = require("@plq/is");
7
4
  function default_1(key, initialValue, persist) {
8
5
  let initialPersist;
9
6
  try {
@@ -12,7 +9,7 @@ function default_1(key, initialValue, persist) {
12
9
  catch (ignore) {
13
10
  initialPersist = {};
14
11
  } // eslint-disable-line no-empty
15
- let initialOrPersistedValue = (0, is_function_1.default)(initialValue) ? initialValue() : initialValue;
12
+ let initialOrPersistedValue = (0, is_1.isFunction)(initialValue) ? initialValue() : initialValue;
16
13
  if (initialPersist && key in initialPersist) {
17
14
  initialOrPersistedValue = initialPersist[key] || initialOrPersistedValue;
18
15
  }
@@ -1 +1 @@
1
- {"version":3,"file":"get-persisted-value.js","sourceRoot":"","sources":["../../src/utils/get-persisted-value.ts"],"names":[],"mappings":";;;;;AAAA,gEAAsC;AAEtC,mBAA2B,GAAU,EAAE,YAA2B,EAAE,OAAgB;IAClF,IAAI,cAAwC,CAAA;IAE5C,IAAI;QACF,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;KACpD;IAAC,OAAO,MAAM,EAAE;QACf,cAAc,GAAG,EAAE,CAAA;KACpB,CAAC,+BAA+B;IAEjC,IAAI,uBAAuB,GAAG,IAAA,qBAAU,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,EAAE,CAAE,CAAC,CAAC,YAAY,CAAA;IAEvF,IAAI,cAAc,IAAI,GAAG,IAAI,cAAc,EAAE;QAC3C,uBAAuB,GAAI,cAAc,CAAC,GAAG,CAAO,IAAI,uBAAuB,CAAA;KAChF;IAED,OAAO,uBAAuB,CAAA;AAChC,CAAC;AAhBD,4BAgBC"}
1
+ {"version":3,"file":"get-persisted-value.js","sourceRoot":"","sources":["../../src/utils/get-persisted-value.ts"],"names":[],"mappings":";;AAAA,gCAAoC;AAEpC,mBAA2B,GAAU,EAAE,YAA2B,EAAE,OAAgB;IAClF,IAAI,cAAwC,CAAA;IAE5C,IAAI;QACF,cAAc,GAAG,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;KACpD;IAAC,OAAO,MAAM,EAAE;QACf,cAAc,GAAG,EAAE,CAAA;KACpB,CAAC,+BAA+B;IAEjC,IAAI,uBAAuB,GAAG,IAAA,eAAU,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,EAAE,CAAE,CAAC,CAAC,YAAY,CAAA;IAEvF,IAAI,cAAc,IAAI,GAAG,IAAI,cAAc,EAAE;QAC3C,uBAAuB,GAAI,cAAc,CAAC,GAAG,CAAO,IAAI,uBAAuB,CAAA;KAChF;IAED,OAAO,uBAAuB,CAAA;AAChC,CAAC;AAhBD,4BAgBC"}
@@ -1,3 +1,3 @@
1
- import { AsyncStorage } from '../@types/storage';
1
+ import type { AsyncStorage } from '../@types/storage';
2
2
  export default function isAsyncStorage(storage: unknown): storage is AsyncStorage;
3
3
  //# sourceMappingURL=is-async-storage.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"is-async-storage.d.ts","sourceRoot":"","sources":["../../src/utils/is-async-storage.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEhD,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,IAAI,YAAY,CAUhF"}
1
+ {"version":3,"file":"is-async-storage.d.ts","sourceRoot":"","sources":["../../src/utils/is-async-storage.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,mBAAmB,CAAA;AAEnD,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,IAAI,YAAY,CAoChF"}
@@ -1,16 +1,38 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const is_promise_1 = __importDefault(require("./is-promise"));
7
- const is_function_1 = __importDefault(require("./is-function"));
3
+ const is_1 = require("@plq/is");
8
4
  function isAsyncStorage(storage) {
9
- return !!storage
10
- && typeof storage.get !== 'undefined'
11
- && ((0, is_promise_1.default)(storage.get)
12
- || ((0, is_function_1.default)(storage.get)
13
- && (0, is_promise_1.default)(storage.get(''))));
5
+ const hasGet = Boolean(storage) && typeof storage?.get !== 'undefined';
6
+ const hasSet = Boolean(storage) && typeof storage?.set !== 'undefined';
7
+ const hasRemove = Boolean(storage) && typeof storage?.remove !== 'undefined';
8
+ if (!hasGet || !hasSet || !hasRemove) {
9
+ return false;
10
+ }
11
+ const hasGetPromise = (0, is_1.isPromise)(storage.get)
12
+ || (0, is_1.isFunction)(storage.get) && (0, is_1.isPromise)(storage.get(''))
13
+ || (0, is_1.isAsyncFunction)(storage.get);
14
+ const hasSetPromise = (0, is_1.isPromise)(storage.set)
15
+ || (0, is_1.isFunction)(storage.set) && (0, is_1.isPromise)(storage.set({}))
16
+ || (0, is_1.isAsyncFunction)(storage.set);
17
+ const hasRemovePromise = (0, is_1.isPromise)(storage.remove)
18
+ || (0, is_1.isFunction)(storage.remove) && (0, is_1.isPromise)(storage.remove(''))
19
+ || (0, is_1.isAsyncFunction)(storage.remove);
20
+ console.log('isAsyncStorage', {
21
+ storage,
22
+ hasGet,
23
+ hasSet,
24
+ hasRemove,
25
+ hasGetPromise,
26
+ hasSetPromise,
27
+ hasRemovePromise,
28
+ });
29
+ return Boolean(storage)
30
+ && hasGet
31
+ && hasSet
32
+ && hasRemove
33
+ && hasGetPromise
34
+ && hasSetPromise
35
+ && hasRemovePromise;
14
36
  }
15
37
  exports.default = isAsyncStorage;
16
38
  //# sourceMappingURL=is-async-storage.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"is-async-storage.js","sourceRoot":"","sources":["../../src/utils/is-async-storage.ts"],"names":[],"mappings":";;;;;AAAA,8DAAoC;AACpC,gEAAsC;AAItC,SAAwB,cAAc,CAAC,OAAgB;IACrD,OAAO,CAAC,CAAC,OAAO;WACb,OAAQ,OAAe,CAAC,GAAG,KAAK,WAAW;WAC3C,CACD,IAAA,oBAAS,EAAE,OAAe,CAAC,GAAG,CAAC;eAC5B,CACD,IAAA,qBAAU,EAAE,OAAe,CAAC,GAAG,CAAC;mBAC7B,IAAA,oBAAS,EAAE,OAAe,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CACvC,CACF,CAAA;AACH,CAAC;AAVD,iCAUC"}
1
+ {"version":3,"file":"is-async-storage.js","sourceRoot":"","sources":["../../src/utils/is-async-storage.ts"],"names":[],"mappings":";;AAAA,gCAA8D;AAG9D,SAAwB,cAAc,CAAC,OAAgB;IACnD,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,OAAQ,OAAe,EAAE,GAAG,KAAK,WAAW,CAAA;IAC/E,MAAM,M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,gBAAgB,CAAA;AAC3B,CAAC;AApCD,iCAoCC"}
@@ -1,10 +1,7 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  const react_1 = require("react");
7
- const is_function_1 = __importDefault(require("./is-function"));
4
+ const is_1 = require("@plq/is");
8
5
  function getValue(key, value) {
9
6
  let newState = null;
10
7
  try {
@@ -24,7 +21,7 @@ function useStorageHandler(itemKey, storageKey, setState, initialValue) {
24
21
  && change.oldValue !== undefined) {
25
22
  const oldValue = getValue(itemKey, change.oldValue);
26
23
  if (oldValue !== initialValue)
27
- setState((0, is_function_1.default)(initialValue) ? initialValue() : initialValue);
24
+ setState((0, is_1.isFunction)(initialValue) ? initialValue() : initialValue);
28
25
  }
29
26
  if (key === storageKey
30
27
  && change.newValue !== null
@@ -1 +1 @@
1
- {"version":3,"file":"use-storage-handler.js","sourceRoot":"","sources":["../../src/utils/use-storage-handler.ts"],"names":[],"mappings":";;;;;AAAA,iCAAsC;AAEtC,gEAAsC;AAEtC,SAAS,QAAQ,CAAI,GAAW,EAAE,KAAa;IAC7C,IAAI,QAAQ,GAAG,IAAI,CAAA;IAEnB,IAAI;QACF,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;KAC7B;IAAC,OAAO,GAAG,EAAE;QACZ,OAAO,CAAC,KAAK,CAAC,sDAAsD,EAAE,GAAG,CAAC,CAAA;KAC3E;IAED,OAAO,QAAQ,IAAI,GAAG,IAAI,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAM,CAAC,CAAC,CAAC,IAAI,CAAA;AAChE,CAAC;AAED,SAAS,iBAAiB,CACxB,OAAe,EACf,UAAkB,EAClB,QAAiD,EACjD,YAA2B;IAE3B,OAAO,CAAC,OAAyC,EAAQ,EAAE;QACzD,MAAM,CAAC,OAAO,CAAC,OAAO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eAAU,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,YAAY,CAAC,CAAA;aAClG;YAED,IACE,GAAG,KAAK,UAAU;mBACf,MAAM,CAAC,QAAQ,KAAK,IAAI;mBACxB,MAAM,CAAC,QAAQ,KAAK,SAAS,EAChC;gBACA,MAAM,QAAQ,GAAG,QAAQ,CAAI,OAAO,EAAE,MAAM,CAAC,QAAQ,CAAC,CAAA;gBAEtD,IAAI,QAAQ,KAAK,IAAI;oBAAE,QAAQ,CAAC,QAAQ,CAAC,CAAA;aAC1C;QACH,CAAC,CAAC,CAAA;IACJ,CAAC,CAAA;AACH,CAAC;AAED,mBACE,GAAW,EACX,UAAkB,EAClB,QAAiD,EACjD,OAA+B,EAC/B,YAA2B;IAE3B,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,MAAM,aAAa,GAAG,iBAAiB,CAAI,GAAG,EAAE,UAAU,EAAE,QAAQ,EAAE,YAAY,CAAC,CAAA;QAEnF,OAAO,CAAC,SAAS,CAAC,WAAW,CAAC,aAAa,CAAC,CAAA;QAE5C,OAAO,GAAG,EAAE;YACV,IAAI,OAAO,CAAC,SAAS,CAAC,WAAW,CAAC,aAAa,CAAC,EAAE;gBAChD,OAAO,CAAC,SAAS,CAAC,cAAc,CAAC,aAAa,CAAC,CAAA;aAChD;QACH,CAAC,CAAA;IACH,CAAC,EAAE,CAAC,YAAY,EAAE,GAAG,EAAE,OAAO,CAAC,SAAS,EAAE,UAAU,EAAE,QAAQ,CAAC,CAAC,CAAA;AAClE,CAAC;AAlBD,4BAkBC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plq/use-persisted-state",
3
- "version": "1.1.5",
3
+ "version": "1.2.0",
4
4
  "description": "useState hook with persistence in storage",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -19,13 +19,29 @@
19
19
  "test": "jest --verbose",
20
20
  "test:watch": "npm test -- --watchAll",
21
21
  "lint": "eslint ./src/**/*.ts",
22
- "release": "npm run lint && npm run test && npm run build && dotenv release-it",
22
+ "release": "dotenv release-it",
23
23
  "examples": "npm run build:watch & parcel examples/index.html"
24
24
  },
25
25
  "repository": {
26
26
  "type": "git",
27
27
  "url": "git+https://github.com/Akurganow/use-persisted-state.git"
28
28
  },
29
+ "release-it": {
30
+ "hooks": {
31
+ "before:init": [
32
+ "npm run lint",
33
+ "npm test",
34
+ "npm run build"
35
+ ],
36
+ "after:release": "echo Successfully released ${name} v${version} to ${repo.repository}."
37
+ },
38
+ "plugins": {
39
+ "@release-it/conventional-changelog": {
40
+ "preset": "conventionalcommits",
41
+ "infile": "CHANGELOG.md"
42
+ }
43
+ }
44
+ },
29
45
  "keywords": [
30
46
  "react",
31
47
  "hook",
@@ -75,5 +91,8 @@
75
91
  "peerDependencies": {
76
92
  "react": ">=16.8.0",
77
93
  "react-dom": ">=16.8.0"
94
+ },
95
+ "dependencies": {
96
+ "@plq/is": "^1.2.0"
78
97
  }
79
98
  }
@@ -1,5 +1,5 @@
1
1
  import React from 'react'
2
- import isFunction from './is-function'
2
+ import { isFunction } from '@plq/is'
3
3
 
4
4
  export default function <T>(newState: React.SetStateAction<T>, state: T): T {
5
5
  let newValue: T
@@ -1,4 +1,4 @@
1
- import isFunction from './is-function'
1
+ import { isFunction } from '@plq/is'
2
2
 
3
3
  export default function<T>(key:string, initialValue: T | (() => T), persist?: string): T {
4
4
  let initialPersist: { [x: string]: unknown }
@@ -1,16 +1,40 @@
1
- import isPromise from './is-promise'
2
- import isFunction from './is-function'
3
-
4
- import { AsyncStorage } from '../@types/storage'
1
+ import {isFunction, isAsyncFunction, isPromise} from '@plq/is'
2
+ import type {AsyncStorage} from '../@types/storage'
5
3
 
6
4
  export default function isAsyncStorage(storage: unknown): storage is AsyncStorage {
7
- return !!storage
8
- && typeof (storage as any).get !== 'undefined'
9
- && (
10
- isPromise((storage as any).get)
11
- || (
12
- isFunction((storage as any).get)
13
- && isPromise((storage as any).get(''))
14
- )
15
- )
5
+ const hasGet = Boolean(storage) && typeof (storage as any)?.get !== 'undefined'
6
+ const hasSet = Boolean(storage) && typeof (storage as any)?.set !== 'undefined'
7
+ const hasRemove = Boolean(storage) && typeof (storage as any)?.remove !== 'undefined'
8
+
9
+ if (!hasGet || !hasSet || !hasRemove) {
10
+ return false
11
+ }
12
+
13
+ const hasGetPromise = isPromise((storage as any).get)
14
+ || isFunction((storage as any).get) && isPromise((storage as any).get(''))
15
+ || isAsyncFunction((storage as any).get)
16
+ const hasSetPromise = isPromise((storage as any).set)
17
+ || isFunction((storage as any).set) && isPromise((storage as any).set({}))
18
+ || isAsyncFunction((storage as any).set)
19
+ const hasRemovePromise = isPromise((storage as any).remove)
20
+ || isFunction((storage as any).remove) && isPromise((storage as any).remove(''))
21
+ || isAsyncFunction((storage as any).remove)
22
+
23
+ console.log('isAsyncStorage', {
24
+ storage,
25
+ hasGet,
26
+ hasSet,
27
+ hasRemove,
28
+ hasGetPromise,
29
+ hasSetPromise,
30
+ hasRemovePromise,
31
+ })
32
+
33
+ return Boolean(storage)
34
+ && hasGet
35
+ && hasSet
36
+ && hasRemove
37
+ && hasGetPromise
38
+ && hasSetPromise
39
+ && hasRemovePromise
16
40
  }
@@ -1,6 +1,6 @@
1
1
  import React, {useEffect} from 'react'
2
2
  import { AsyncStorage, Storage, StorageChange } from '../@types/storage'
3
- import isFunction from './is-function'
3
+ import { isFunction } from '@plq/is'
4
4
 
5
5
  function getValue<T>(key: string, value: string) {
6
6
  let newState = null
@@ -1,7 +0,0 @@
1
- export default function isFunction(value: unknown): value is Function {
2
- return !!value
3
- && (
4
- Object.prototype.toString.call(value) === '[object Function]'
5
- || typeof value === 'function'
6
- )
7
- }
@@ -1,8 +0,0 @@
1
- export default function isPromise<T = unknown>(value: unknown): value is PromiseLike<T> {
2
- return !!value
3
- && (
4
- Object.prototype.toString.call(value) === '[object Promise]'
5
- || typeof value === 'function'
6
- )
7
- && typeof (value as PromiseLike<unknown>).then === 'function'
8
- }