react-wire-persisted 1.2.0 → 1.5.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 +11 -1
- package/dist/react-wire-persisted.mjs +231 -0
- package/dist/react-wire-persisted.umd.js +1 -0
- package/package.json +40 -45
- package/src/providers/LocalStorageProvider.js +1 -1
- package/dist/react-wire-persisted.js +0 -717
- package/dist/react-wire-persisted.min.js +0 -1
- package/es/react-wire-persisted.js +0 -702
- package/es/react-wire-persisted.mjs +0 -1
- package/lib/react-wire-persisted.js +0 -802
- /package/src/{index.ts → index.js} +0 -0
- /package/src/{react-wire-persisted.ts → react-wire-persisted.js} +0 -0
- /package/src/utils/{fakeLocalStorage.ts → fakeLocalStorage.js} +0 -0
- /package/src/utils/{index.ts → index.js} +0 -0
- /package/src/utils/{keys.ts → keys.js} +0 -0
package/README.md
CHANGED
|
@@ -5,7 +5,17 @@
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
7
|
```shell
|
|
8
|
-
$
|
|
8
|
+
$ pnpm add -D @forminator/react-wire react-wire-persisted
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Building
|
|
12
|
+
|
|
13
|
+
```shell
|
|
14
|
+
$ pnpm build
|
|
15
|
+
$ npm version patch # patch, minor, major
|
|
16
|
+
$ git push
|
|
17
|
+
$ git push --tags
|
|
18
|
+
$ npm publish
|
|
9
19
|
```
|
|
10
20
|
|
|
11
21
|
## Usage
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { createWire as b } from "@forminator/react-wire";
|
|
2
|
+
(function() {
|
|
3
|
+
const s = {};
|
|
4
|
+
try {
|
|
5
|
+
if (process) {
|
|
6
|
+
process.env = Object.assign({}, process.env), Object.assign(process.env, s);
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
} catch {
|
|
10
|
+
}
|
|
11
|
+
globalThis.process = { env: s };
|
|
12
|
+
})();
|
|
13
|
+
const m = {}, p = (s) => {
|
|
14
|
+
m[s] = s;
|
|
15
|
+
}, I = (s) => p(s), O = () => m, S = (s, e = null) => {
|
|
16
|
+
const t = e || m;
|
|
17
|
+
return s ? Object.keys(t).reduce((r, n) => ({
|
|
18
|
+
...r,
|
|
19
|
+
[n]: `${s}.${t[n]}`
|
|
20
|
+
}), {}) : t;
|
|
21
|
+
}, g = {
|
|
22
|
+
getItem: (s) => g[s],
|
|
23
|
+
setItem: (s, e) => {
|
|
24
|
+
g[s] = e;
|
|
25
|
+
},
|
|
26
|
+
removeItem: (s) => {
|
|
27
|
+
delete g[s];
|
|
28
|
+
}
|
|
29
|
+
}, d = (s) => {
|
|
30
|
+
const e = typeof s;
|
|
31
|
+
return s === null ? !0 : Array.isArray(s) || e === "object" ? !1 : e !== "function";
|
|
32
|
+
}, j = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
33
|
+
__proto__: null,
|
|
34
|
+
addKey: p,
|
|
35
|
+
fakeLocalStorage: g,
|
|
36
|
+
getKeys: O,
|
|
37
|
+
getPrefixedKeys: S,
|
|
38
|
+
isPrimitive: d,
|
|
39
|
+
key: I
|
|
40
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
41
|
+
class h {
|
|
42
|
+
/**
|
|
43
|
+
* Initializes the class
|
|
44
|
+
* @param {String} namespace Namespace to prefix all keys with. Mostly used for the logging & reset functions
|
|
45
|
+
* @param {Object} registry (Optional) Initialize the storage provider with an existing registry
|
|
46
|
+
*/
|
|
47
|
+
constructor(e, t) {
|
|
48
|
+
if (new.target === h)
|
|
49
|
+
throw TypeError("StorageProvider is abstract. Extend this class to implement it");
|
|
50
|
+
this.namespace = e || null, this.registry = t || /* istanbul ignore next */
|
|
51
|
+
{};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Sets the namespace for this storage provider, and migrates
|
|
55
|
+
* all stored values to the new namespace
|
|
56
|
+
* @param {String} namespace New namespace for this storage provider
|
|
57
|
+
*/
|
|
58
|
+
/* istanbul ignore next */
|
|
59
|
+
setNamespace(e) {
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Registers an item with it's initial value. This is used for logging, resetting, etc.
|
|
63
|
+
* @param {String} key Storage item's key
|
|
64
|
+
* @param {*} initialValue Storage item's initial value
|
|
65
|
+
*/
|
|
66
|
+
register(e, t) {
|
|
67
|
+
this.registry[e] = t;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Reads an item from storage
|
|
71
|
+
* @param {String} key Key for the item to retrieve
|
|
72
|
+
*/
|
|
73
|
+
/* istanbul ignore next */
|
|
74
|
+
getItem(e) {
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Stores a value
|
|
78
|
+
* @param {String} key Item's storage key
|
|
79
|
+
* @param {String} value Item's value to store
|
|
80
|
+
*/
|
|
81
|
+
/* istanbul ignore next */
|
|
82
|
+
setItem(e, t) {
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Removes an item from storage
|
|
86
|
+
* @param {String} key Item's storage key
|
|
87
|
+
* @param {Boolean} fromRegistry (Optional) If the item should also be removed from the registry
|
|
88
|
+
*/
|
|
89
|
+
/* istanbul ignore next */
|
|
90
|
+
removeItem(e, t = !1) {
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Gets all stored keys & values
|
|
94
|
+
* If a `namespace` was set, only keys prefixed with the namespace will be returned
|
|
95
|
+
*/
|
|
96
|
+
/* istanbul ignore next */
|
|
97
|
+
getAll() {
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
*
|
|
101
|
+
* @param {Boolean} useInitialValues If values should be replaced with their initial values. If false, keys are removed
|
|
102
|
+
* @param {String[]} excludedKeys (Optional) List of keys to exclude
|
|
103
|
+
* @param {Boolean} clearRegistry (Optional) If the registry should also be cleared
|
|
104
|
+
*/
|
|
105
|
+
/* istanbul ignore next */
|
|
106
|
+
_resetAll(e = !0, t = [], r = !1) {
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Resets all values to their initial values
|
|
110
|
+
* If a `namespace` is set, only keys prefixed with the namespace will be reset
|
|
111
|
+
* @param {String[]} excludedKeys (Optional) List of keys to exclude
|
|
112
|
+
*/
|
|
113
|
+
/* istanbul ignore next */
|
|
114
|
+
resetAll(e = []) {
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Removes all items from local storage.
|
|
118
|
+
* If a `namespace` is set, only keys prefixed with the namespace will be removed
|
|
119
|
+
* @param {String[]} excludedKeys (Optional) List of keys to exclude
|
|
120
|
+
*/
|
|
121
|
+
/* istanbul ignore next */
|
|
122
|
+
removeAll(e = []) {
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
class v extends h {
|
|
126
|
+
constructor(e = null, t = {}) {
|
|
127
|
+
super(e, t), this.storage = this.getStorage();
|
|
128
|
+
}
|
|
129
|
+
getStorage() {
|
|
130
|
+
try {
|
|
131
|
+
return window.localStorage;
|
|
132
|
+
} catch {
|
|
133
|
+
return console.warn(new Error("LocalStorageProvider: localStorage not supported")), g;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
setNamespace(e) {
|
|
137
|
+
if (!this.namespace) {
|
|
138
|
+
this.namespace = e;
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
if (this.namespace === e)
|
|
142
|
+
return;
|
|
143
|
+
const t = JSON.parse(JSON.stringify(this.getAll()));
|
|
144
|
+
this.removeAll();
|
|
145
|
+
for (const [r, n] of Object.entries(t)) {
|
|
146
|
+
const o = r.replace(this.namespace, e);
|
|
147
|
+
this.setItem(o, n);
|
|
148
|
+
}
|
|
149
|
+
this.namespace = e;
|
|
150
|
+
}
|
|
151
|
+
getItem(e) {
|
|
152
|
+
const t = this.storage.getItem(e);
|
|
153
|
+
if (t == null)
|
|
154
|
+
return null;
|
|
155
|
+
try {
|
|
156
|
+
return JSON.parse(t);
|
|
157
|
+
} catch {
|
|
158
|
+
return t;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
setItem(e, t) {
|
|
162
|
+
let r = t;
|
|
163
|
+
return r != null && (r = d(t) ? t : JSON.stringify(t)), this.storage.setItem(e, r);
|
|
164
|
+
}
|
|
165
|
+
removeItem(e, t = !1) {
|
|
166
|
+
return t && delete this.registry[e], this.storage.removeItem(e);
|
|
167
|
+
}
|
|
168
|
+
getAll() {
|
|
169
|
+
const e = `${this.namespace}.`;
|
|
170
|
+
return Object.keys(this.storage).reduce((t, r) => ((!this.namespace || r.startsWith(e)) && (t[r] = this.storage.getItem(r)), t), {});
|
|
171
|
+
}
|
|
172
|
+
_resetAll(e = !0, t = [], r = !1) {
|
|
173
|
+
const n = `${this.namespace}.`;
|
|
174
|
+
Object.keys(localStorage).forEach((o) => {
|
|
175
|
+
const l = this.namespace ? o.startsWith(n) : !0, a = t?.includes(o) || !1;
|
|
176
|
+
!l || a || (e ? Object.prototype.hasOwnProperty.call(this.registry, o) ? this.storage.setItem(o, this.registry[o]) : this.storage.removeItem(o) : (this.storage.removeItem(o), r && delete this.registry[o]));
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
resetAll(e = [], t = !1) {
|
|
180
|
+
this._resetAll(!0, e || [], t);
|
|
181
|
+
}
|
|
182
|
+
removeAll(e = [], t = !1) {
|
|
183
|
+
this._resetAll(!1, e || [], t);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const A = {
|
|
187
|
+
logging: {
|
|
188
|
+
enabled: !1
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
let y = v, i = new y(), u = { ...A }, f = [];
|
|
192
|
+
const w = () => i.namespace, K = () => i, P = () => u, _ = (s) => {
|
|
193
|
+
i.setNamespace(s), i = new y(s || w());
|
|
194
|
+
}, E = (s) => {
|
|
195
|
+
if (u = {
|
|
196
|
+
...u,
|
|
197
|
+
...s
|
|
198
|
+
}, u.logging.enabled)
|
|
199
|
+
for (console.info("Flushing", f.length, "pending logs"); f.length; )
|
|
200
|
+
console.log(...f.shift());
|
|
201
|
+
}, x = (...s) => {
|
|
202
|
+
u.logging.enabled ? console.log(...s) : f.push(s);
|
|
203
|
+
}, V = (s, e = null) => {
|
|
204
|
+
if (!s && typeof s != "number") throw new Error(
|
|
205
|
+
`createPersistedWire: Key cannot be a falsey value (${s}}`
|
|
206
|
+
);
|
|
207
|
+
i.register(s, e);
|
|
208
|
+
const t = b(e), r = () => t.getValue(), n = (c) => (i.setItem(s, c), t.setValue(c)), o = (c) => {
|
|
209
|
+
t.subscribe(c);
|
|
210
|
+
}, l = i.getItem(s), a = l === null ? e : l;
|
|
211
|
+
return x("react-wire-persisted: create", s, {
|
|
212
|
+
value: e,
|
|
213
|
+
storedValue: l,
|
|
214
|
+
initialValue: a
|
|
215
|
+
}), a !== e && n(a), {
|
|
216
|
+
...t,
|
|
217
|
+
getValue: r,
|
|
218
|
+
setValue: n,
|
|
219
|
+
subscribe: o
|
|
220
|
+
};
|
|
221
|
+
};
|
|
222
|
+
export {
|
|
223
|
+
V as createPersistedWire,
|
|
224
|
+
A as defaultOptions,
|
|
225
|
+
w as getNamespace,
|
|
226
|
+
P as getOptions,
|
|
227
|
+
K as getStorage,
|
|
228
|
+
_ as setNamespace,
|
|
229
|
+
E as setOptions,
|
|
230
|
+
j as utils
|
|
231
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(function(n,a){typeof exports=="object"&&typeof module<"u"?a(exports,require("@forminator/react-wire")):typeof define=="function"&&define.amd?define(["exports","@forminator/react-wire"],a):(n=typeof globalThis<"u"?globalThis:n||self,a(n["react-wire-persisted"]={},n.reactWire))})(this,function(n,a){"use strict";(function(){const s={};try{if(process){process.env=Object.assign({},process.env),Object.assign(process.env,s);return}}catch{}globalThis.process={env:s}})();const h={},y=s=>{h[s]=s},w=s=>y(s),v=()=>h,A=(s,e=null)=>{const t=e||h;return s?Object.keys(t).reduce((r,o)=>({...r,[o]:`${s}.${t[o]}`}),{}):t},c={getItem:s=>c[s],setItem:(s,e)=>{c[s]=e},removeItem:s=>{delete c[s]}},b=s=>{const e=typeof s;return s===null?!0:Array.isArray(s)||e==="object"?!1:e!=="function"},N=Object.freeze(Object.defineProperty({__proto__:null,addKey:y,fakeLocalStorage:c,getKeys:v,getPrefixedKeys:A,isPrimitive:b,key:w},Symbol.toStringTag,{value:"Module"}));class p{constructor(e,t){if(new.target===p)throw TypeError("StorageProvider is abstract. Extend this class to implement it");this.namespace=e||null,this.registry=t||{}}setNamespace(e){}register(e,t){this.registry[e]=t}getItem(e){}setItem(e,t){}removeItem(e,t=!1){}getAll(){}_resetAll(e=!0,t=[],r=!1){}resetAll(e=[]){}removeAll(e=[]){}}class j extends p{constructor(e=null,t={}){super(e,t),this.storage=this.getStorage()}getStorage(){try{return window.localStorage}catch{return console.warn(new Error("LocalStorageProvider: localStorage not supported")),c}}setNamespace(e){if(!this.namespace){this.namespace=e;return}if(this.namespace===e)return;const t=JSON.parse(JSON.stringify(this.getAll()));this.removeAll();for(const[r,o]of Object.entries(t)){const i=r.replace(this.namespace,e);this.setItem(i,o)}this.namespace=e}getItem(e){const t=this.storage.getItem(e);if(t==null)return null;try{return JSON.parse(t)}catch{return t}}setItem(e,t){let r=t;return r!=null&&(r=b(t)?t:JSON.stringify(t)),this.storage.setItem(e,r)}removeItem(e,t=!1){return t&&delete this.registry[e],this.storage.removeItem(e)}getAll(){const e=`${this.namespace}.`;return Object.keys(this.storage).reduce((t,r)=>((!this.namespace||r.startsWith(e))&&(t[r]=this.storage.getItem(r)),t),{})}_resetAll(e=!0,t=[],r=!1){const o=`${this.namespace}.`;Object.keys(localStorage).forEach(i=>{const u=this.namespace?i.startsWith(o):!0,f=t?.includes(i)||!1;!u||f||(e?Object.prototype.hasOwnProperty.call(this.registry,i)?this.storage.setItem(i,this.registry[i]):this.storage.removeItem(i):(this.storage.removeItem(i),r&&delete this.registry[i]))})}resetAll(e=[],t=!1){this._resetAll(!0,e||[],t)}removeAll(e=[],t=!1){this._resetAll(!1,e||[],t)}}const O={logging:{enabled:!1}};let I=j,l=new I,g={...O},m=[];const S=()=>l.namespace,P=()=>l,K=()=>g,W=s=>{l.setNamespace(s),l=new I(s||S())},_=s=>{if(g={...g,...s},g.logging.enabled)for(console.info("Flushing",m.length,"pending logs");m.length;)console.log(...m.shift())},E=(...s)=>{g.logging.enabled?console.log(...s):m.push(s)},T=(s,e=null)=>{if(!s&&typeof s!="number")throw new Error(`createPersistedWire: Key cannot be a falsey value (${s}}`);l.register(s,e);const t=a.createWire(e),r=()=>t.getValue(),o=d=>(l.setItem(s,d),t.setValue(d)),i=d=>{t.subscribe(d)},u=l.getItem(s),f=u===null?e:u;return E("react-wire-persisted: create",s,{value:e,storedValue:u,initialValue:f}),f!==e&&o(f),{...t,getValue:r,setValue:o,subscribe:i}};n.createPersistedWire=T,n.defaultOptions=O,n.getNamespace=S,n.getOptions=K,n.getStorage=P,n.setNamespace=W,n.setOptions=_,n.utils=N,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})});
|
package/package.json
CHANGED
|
@@ -1,57 +1,49 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-wire-persisted",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"author": "Wesley Bliss <wesley.bliss@gmail.com>",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"module": "
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/react-wire-persisted.umd.cjs",
|
|
8
|
+
"module": "./dist/react-wire-persisted.mjs",
|
|
9
9
|
"files": [
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"es",
|
|
13
|
-
"src"
|
|
10
|
+
"src",
|
|
11
|
+
"dist"
|
|
14
12
|
],
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./dist/react-wire-persisted.mjs",
|
|
16
|
+
"require": "./dist/react-wire-persisted.umd.cjs"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
15
19
|
"scripts": {
|
|
16
|
-
"dev": "
|
|
17
|
-
"build": "
|
|
18
|
-
"test": "
|
|
19
|
-
"test:
|
|
20
|
-
"test:
|
|
21
|
-
"test:watch:coverage": "jest --watch --collectCoverage",
|
|
22
|
-
"test:snapshots": "jest --updateSnapshot",
|
|
23
|
-
"test:coverage": "jest --collectCoverage",
|
|
24
|
-
"test:ci": "jest --runInBand"
|
|
20
|
+
"dev": "vite --host --config config/vite.config.development.js",
|
|
21
|
+
"build": "vite build --config config/vite.config.production.js",
|
|
22
|
+
"test:unit": "NODE_ENV=test vitest run",
|
|
23
|
+
"test:unit:only": "NODE_ENV=test vitest run -t ",
|
|
24
|
+
"test:unit:coverage": "NODE_ENV=test vitest run --no-color --reporter=junit --coverage --outputFile=coverage/report.xml"
|
|
25
25
|
},
|
|
26
|
-
"dependencies": {},
|
|
27
26
|
"devDependencies": {
|
|
28
|
-
"@
|
|
29
|
-
"@
|
|
30
|
-
"@
|
|
31
|
-
"@
|
|
32
|
-
"@
|
|
33
|
-
"@
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"snapshot-diff": "^0.9.0",
|
|
49
|
-
"style-loader": "^3.3.1",
|
|
50
|
-
"webpack": "^5.69.1",
|
|
51
|
-
"webpack-bundle-analyzer": "^4.5.0",
|
|
52
|
-
"webpack-cli": "^4.9.2",
|
|
53
|
-
"webpack-dev-server": "^4.7.4",
|
|
54
|
-
"webpack-node-externals": "^3.0.0"
|
|
27
|
+
"@forminator/react-wire": "^0.7.0",
|
|
28
|
+
"@testing-library/jest-dom": "^6.6.4",
|
|
29
|
+
"@testing-library/react": "^16.3.0",
|
|
30
|
+
"@testing-library/user-event": "^14.6.1",
|
|
31
|
+
"@vitejs/plugin-react": "^4.7.0",
|
|
32
|
+
"@vitest/coverage-c8": "^0.33.0",
|
|
33
|
+
"browserslist": "^4.25.1",
|
|
34
|
+
"c8": "^10.1.3",
|
|
35
|
+
"dotenv": "^17.2.1",
|
|
36
|
+
"esbuild": "^0.25.8",
|
|
37
|
+
"jest": "^30.0.5",
|
|
38
|
+
"jest-environment-jsdom": "^30.0.5",
|
|
39
|
+
"np": "^10.2.0",
|
|
40
|
+
"react": "^19.1.1",
|
|
41
|
+
"react-dom": "^19.1.1",
|
|
42
|
+
"rollup-plugin-inject-process-env": "^1.3.1",
|
|
43
|
+
"snapshot-diff": "^0.10.0",
|
|
44
|
+
"vite": "^7.0.6",
|
|
45
|
+
"vite-jest": "^0.1.4",
|
|
46
|
+
"vitest": "^3.2.4"
|
|
55
47
|
},
|
|
56
48
|
"peerDependencies": {
|
|
57
49
|
"@forminator/react-wire": "^0.5.0-alpha.1",
|
|
@@ -69,5 +61,8 @@
|
|
|
69
61
|
"last 1 firefox version",
|
|
70
62
|
"last 1 safari version"
|
|
71
63
|
]
|
|
64
|
+
},
|
|
65
|
+
"pnpm": {
|
|
66
|
+
"neverBuiltDependencies": []
|
|
72
67
|
}
|
|
73
68
|
}
|
|
@@ -21,7 +21,7 @@ class LocalStorageProvider extends StorageProvider {
|
|
|
21
21
|
return window.localStorage
|
|
22
22
|
} catch (e) {
|
|
23
23
|
/* istanbul ignore next */
|
|
24
|
-
console.warn('LocalStorageProvider: localStorage not supported')
|
|
24
|
+
console.warn(new Error('LocalStorageProvider: localStorage not supported'))
|
|
25
25
|
/* istanbul ignore next */
|
|
26
26
|
return fakeLocalStorage
|
|
27
27
|
}
|