@superutils/store 0.1.14 → 0.1.17
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 +49 -10
- package/dist/browser/index.min.js +2 -2
- package/dist/browser/index.min.js.map +1 -1
- package/dist/index.cjs +62 -22
- package/dist/index.d.cts +264 -126
- package/dist/index.d.ts +264 -126
- package/dist/index.js +65 -25
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -61,9 +61,15 @@ var _Store = class _Store {
|
|
|
61
61
|
forceUpdateCache: void 0
|
|
62
62
|
};
|
|
63
63
|
this.type = "map";
|
|
64
|
-
this.clear = () =>
|
|
64
|
+
this.clear = () => {
|
|
65
|
+
var _a, _b;
|
|
66
|
+
(_b = (_a = this.validate) == null ? void 0 : _a.clear) == null ? void 0 : _b.call(this, [], "clear");
|
|
67
|
+
return this.setAll(/* @__PURE__ */ new Map(), true);
|
|
68
|
+
};
|
|
65
69
|
this.delete = (keys) => {
|
|
70
|
+
var _a, _b;
|
|
66
71
|
if (!(0, import_core.isArr)(keys)) keys = [keys];
|
|
72
|
+
(_b = (_a = this.validate) == null ? void 0 : _a.delete) == null ? void 0 : _b.call(this, [keys], "delete");
|
|
67
73
|
const data = this.getAll();
|
|
68
74
|
for (const k of keys) data.delete(k);
|
|
69
75
|
this.setAll(data, true);
|
|
@@ -93,7 +99,7 @@ var _Store = class _Store {
|
|
|
93
99
|
this.handleSubjectChange = (data) => {
|
|
94
100
|
var _a;
|
|
95
101
|
if (!(0, import_core.isMap)(data)) return this.subject$.next(/* @__PURE__ */ new Map());
|
|
96
|
-
this.write
|
|
102
|
+
(0, import_core.fallbackIfFails)(this.write, [data], null);
|
|
97
103
|
(0, import_core.fallbackIfFails)(
|
|
98
104
|
(_a = this.onChange) == null ? void 0 : _a.bind(this),
|
|
99
105
|
[data],
|
|
@@ -104,16 +110,26 @@ var _Store = class _Store {
|
|
|
104
110
|
this.init = (initialValue, checkStorage = true) => {
|
|
105
111
|
var _a;
|
|
106
112
|
if (this.initialized) return false;
|
|
107
|
-
this.
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
113
|
+
if (this.name && checkStorage) {
|
|
114
|
+
Object.defineProperty(this, "storage", {
|
|
115
|
+
value: (0, import_core.fallbackIfFails)(
|
|
116
|
+
() => {
|
|
117
|
+
var _a2;
|
|
118
|
+
return (_a2 = this.storage) != null ? _a2 : globalThis.localStorage;
|
|
119
|
+
},
|
|
120
|
+
[],
|
|
121
|
+
void 0
|
|
122
|
+
)
|
|
123
|
+
});
|
|
124
|
+
if (!this.storage)
|
|
125
|
+
throw new Error(_Store.messages.invalidStorageOptions);
|
|
126
|
+
}
|
|
127
|
+
Object.defineProperty(this, "initialized", { value: true });
|
|
111
128
|
let firstValue = initialValue;
|
|
112
129
|
if (!!(initialValue == null ? void 0 : initialValue.size) || !this.cacheDisabled) {
|
|
113
130
|
const dataStr = this.name ? (_a = this.storage) == null ? void 0 : _a.getItem(this.name) : null;
|
|
114
131
|
const existingValue = this.read(dataStr != null ? dataStr : null);
|
|
115
132
|
if ((0, import_core.isDefined)(dataStr)) firstValue = existingValue;
|
|
116
|
-
isEmpty = this.cacheDisabled || existingValue.size === 0;
|
|
117
133
|
}
|
|
118
134
|
(firstValue == null ? void 0 : firstValue.size) && this.subject$.next(firstValue);
|
|
119
135
|
this.unsubscribe();
|
|
@@ -122,7 +138,8 @@ var _Store = class _Store {
|
|
|
122
138
|
this.handleForceUpdateCache
|
|
123
139
|
);
|
|
124
140
|
}
|
|
125
|
-
|
|
141
|
+
const skipNum = this.subject$ instanceof import_rxjs.BehaviorSubject && (firstValue == null ? void 0 : firstValue.size) && firstValue !== initialValue ? 1 : 0;
|
|
142
|
+
this.subscriptions.subject = this.subject$.pipe((0, import_rxjs.skip)(skipNum)).subscribe(
|
|
126
143
|
!this.cacheDisabled && this.delay > 0 ? (0, import_core.deferred)(this.handleSubjectChange, this.delay, {
|
|
127
144
|
thisArg: this,
|
|
128
145
|
...this.delayOptions
|
|
@@ -158,12 +175,17 @@ var _Store = class _Store {
|
|
|
158
175
|
};
|
|
159
176
|
this.search = (...args) => (0, import_core.search)(this.getAll(), ...args);
|
|
160
177
|
this.set = (key, value) => {
|
|
178
|
+
var _a, _b;
|
|
161
179
|
const data = this.getAll();
|
|
162
|
-
|
|
180
|
+
const _value = (0, import_core.isFn)(value) ? value(data.get(key)) : value;
|
|
181
|
+
(_b = (_a = this.validate) == null ? void 0 : _a.set) == null ? void 0 : _b.call(this, [key, _value], "set");
|
|
182
|
+
data.set(key, _value);
|
|
163
183
|
return this.setAll(data, true);
|
|
164
184
|
};
|
|
165
185
|
this.setAll = (data, replace = false) => {
|
|
186
|
+
var _a, _b;
|
|
166
187
|
if (!(0, import_core.isMap)(data)) return this;
|
|
188
|
+
(_b = (_a = this.validate) == null ? void 0 : _a.setAll) == null ? void 0 : _b.call(this, [data, replace], "setAll");
|
|
167
189
|
data = replace ? data : (0, import_core.mapJoin)(this.getAll(), data);
|
|
168
190
|
this.subject$.next(new Map(data));
|
|
169
191
|
return this;
|
|
@@ -218,17 +240,20 @@ var _Store = class _Store {
|
|
|
218
240
|
var _a, _b;
|
|
219
241
|
(_a = this.subscriptions.forceUpdateCache) == null ? void 0 : _a.unsubscribe();
|
|
220
242
|
(_b = this.subscriptions.subject) == null ? void 0 : _b.unsubscribe();
|
|
221
|
-
this.subscriptions = {
|
|
243
|
+
this.subscriptions = {
|
|
244
|
+
forceUpdateCache: void 0,
|
|
245
|
+
subject: void 0
|
|
246
|
+
};
|
|
222
247
|
};
|
|
223
248
|
this.values = () => (0, import_core.getValues)(this.getAll());
|
|
224
249
|
this.write = (data) => {
|
|
225
|
-
var _a;
|
|
250
|
+
var _a, _b, _c;
|
|
251
|
+
this.init();
|
|
252
|
+
data != null ? data : data = (_a = this.subject$) == null ? void 0 : _a.value;
|
|
253
|
+
if (!(0, import_core.isMap)(data) || !this.name || !this.storage) return false;
|
|
254
|
+
(_c = (_b = this.validate) == null ? void 0 : _b.write) == null ? void 0 : _c.call(this, [data], "write");
|
|
226
255
|
try {
|
|
227
|
-
!this.initialized && this.init();
|
|
228
|
-
data != null ? data : data = (_a = this.subject$) == null ? void 0 : _a.value;
|
|
229
|
-
if (!(0, import_core.isMap)(data)) return false;
|
|
230
256
|
const jsonStr = this.toString(data);
|
|
231
|
-
if (!this.name || !this.storage) return false;
|
|
232
257
|
this.storage.setItem(this.name, jsonStr);
|
|
233
258
|
return true;
|
|
234
259
|
} catch (err) {
|
|
@@ -236,6 +261,7 @@ var _Store = class _Store {
|
|
|
236
261
|
return false;
|
|
237
262
|
}
|
|
238
263
|
};
|
|
264
|
+
this.name = `${name != null ? name : ""}`.trim() || null;
|
|
239
265
|
const {
|
|
240
266
|
cacheDisabled = false,
|
|
241
267
|
delay,
|
|
@@ -246,25 +272,30 @@ var _Store = class _Store {
|
|
|
246
272
|
parse,
|
|
247
273
|
spaces,
|
|
248
274
|
storage = (0, import_core.fallbackIfFails)(
|
|
249
|
-
() => globalThis.localStorage,
|
|
275
|
+
() => !name ? void 0 : globalThis.localStorage,
|
|
250
276
|
[],
|
|
251
277
|
void 0
|
|
252
278
|
),
|
|
253
|
-
stringify
|
|
279
|
+
stringify,
|
|
280
|
+
validate = {}
|
|
254
281
|
} = options != null ? options : {};
|
|
255
|
-
this.name
|
|
256
|
-
|
|
257
|
-
throw new Error(_Store.messages.invalidOptionsStorage);
|
|
282
|
+
if (this.name && !storage && storage !== null)
|
|
283
|
+
throw new Error(_Store.messages.invalidStorageOptions);
|
|
258
284
|
this.cacheDisabled = !!storage && cacheDisabled;
|
|
259
285
|
this.delay = delay === 0 || (0, import_core.isPositiveNumber)(delay) ? delay : 300;
|
|
260
286
|
this.delayOptions = delayOptions;
|
|
261
287
|
this.onError = onError;
|
|
262
288
|
this.onChange = onChange;
|
|
263
289
|
this.parse = parse == null ? void 0 : parse.bind(this);
|
|
290
|
+
this.spaces = spaces;
|
|
264
291
|
this.storage = storage;
|
|
265
292
|
this.stringify = stringify == null ? void 0 : stringify.bind(this);
|
|
266
|
-
this.spaces = spaces;
|
|
267
293
|
this.subject$ = this.cacheDisabled ? new import_rxjs.Subject() : new import_rxjs.BehaviorSubject(/* @__PURE__ */ new Map());
|
|
294
|
+
const _validate = { ...validate };
|
|
295
|
+
for (const [key, value] of Object.entries(_validate)) {
|
|
296
|
+
_validate[key] = value;
|
|
297
|
+
}
|
|
298
|
+
this.validate = _validate;
|
|
268
299
|
(0, import_core.isMap)(initialValue) && initialValue.size && this.init(initialValue, false);
|
|
269
300
|
}
|
|
270
301
|
get size() {
|
|
@@ -273,7 +304,8 @@ var _Store = class _Store {
|
|
|
273
304
|
};
|
|
274
305
|
_Store.messages = Object.seal({
|
|
275
306
|
invalidJsonEntries: "Invalid JSON format. Parsed value must be a 2D array representing key-value pairs.",
|
|
276
|
-
|
|
307
|
+
invalidStorageOptions: "options.storage: LocalStorage instance or equivalent required. For NodeJS, use `node-localstorage` NPM module.",
|
|
308
|
+
validationError: "Validation failed. Action: "
|
|
277
309
|
});
|
|
278
310
|
/**
|
|
279
311
|
* Trigger forced update of cached data from storage.
|
|
@@ -292,6 +324,10 @@ var Store_default = Store;
|
|
|
292
324
|
|
|
293
325
|
// src/createStore.ts
|
|
294
326
|
function createStore(name, options) {
|
|
327
|
+
if ((0, import_core2.isObj)(name)) {
|
|
328
|
+
options = name;
|
|
329
|
+
name = null;
|
|
330
|
+
}
|
|
295
331
|
const store = new Store_default(name, options);
|
|
296
332
|
Object.defineProperty(store, "context", {
|
|
297
333
|
configurable: false,
|
|
@@ -305,6 +341,10 @@ var createStore_default = createStore;
|
|
|
305
341
|
|
|
306
342
|
// src/createObjectStore.ts
|
|
307
343
|
function createObjectStore(name, options) {
|
|
344
|
+
if ((0, import_core3.isObj)(name)) {
|
|
345
|
+
options = name;
|
|
346
|
+
name = null;
|
|
347
|
+
}
|
|
308
348
|
const store = createStore_default(name, {
|
|
309
349
|
parse: (str) => (0, import_core3.objToMap)(JSON.parse(str != null ? str : "{}")),
|
|
310
350
|
stringify: function(data) {
|