@superutils/store 0.1.15 → 0.1.18

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.js CHANGED
@@ -1,8 +1,8 @@
1
1
  // src/createObjectStore.ts
2
- import { objToMap, isObj } from "@superutils/core";
2
+ import { objToMap, isObj as isObj2 } from "@superutils/core";
3
3
 
4
4
  // src/createStore.ts
5
- import { isFn as isFn2 } from "@superutils/core";
5
+ import { isFn as isFn2, isObj } from "@superutils/core";
6
6
 
7
7
  // src/Store.ts
8
8
  import {
@@ -47,9 +47,15 @@ var _Store = class _Store {
47
47
  forceUpdateCache: void 0
48
48
  };
49
49
  this.type = "map";
50
- this.clear = () => this.setAll(/* @__PURE__ */ new Map(), true);
50
+ this.clear = () => {
51
+ var _a, _b;
52
+ (_b = (_a = this.validate) == null ? void 0 : _a.clear) == null ? void 0 : _b.call(this, [], "clear");
53
+ return this.setAll(/* @__PURE__ */ new Map(), true);
54
+ };
51
55
  this.delete = (keys) => {
56
+ var _a, _b;
52
57
  if (!isArr(keys)) keys = [keys];
58
+ (_b = (_a = this.validate) == null ? void 0 : _a.delete) == null ? void 0 : _b.call(this, [keys], "delete");
53
59
  const data = this.getAll();
54
60
  for (const k of keys) data.delete(k);
55
61
  this.setAll(data, true);
@@ -79,7 +85,7 @@ var _Store = class _Store {
79
85
  this.handleSubjectChange = (data) => {
80
86
  var _a;
81
87
  if (!isMap(data)) return this.subject$.next(/* @__PURE__ */ new Map());
82
- this.write(data);
88
+ fallbackIfFails(this.write, [data], null);
83
89
  fallbackIfFails(
84
90
  (_a = this.onChange) == null ? void 0 : _a.bind(this),
85
91
  [data],
@@ -90,16 +96,26 @@ var _Store = class _Store {
90
96
  this.init = (initialValue, checkStorage = true) => {
91
97
  var _a;
92
98
  if (this.initialized) return false;
93
- this.initialized = true;
94
- if (checkStorage && this.name && !this.storage)
95
- throw new Error(_Store.messages.invalidOptionsStorage);
96
- let isEmpty = !!this.cacheDisabled;
99
+ if (this.name && checkStorage) {
100
+ Object.defineProperty(this, "storage", {
101
+ value: fallbackIfFails(
102
+ () => {
103
+ var _a2;
104
+ return (_a2 = this.storage) != null ? _a2 : globalThis.localStorage;
105
+ },
106
+ [],
107
+ void 0
108
+ )
109
+ });
110
+ if (!this.storage)
111
+ throw new Error(_Store.messages.invalidStorageOptions);
112
+ }
113
+ Object.defineProperty(this, "initialized", { value: true });
97
114
  let firstValue = initialValue;
98
115
  if (!!(initialValue == null ? void 0 : initialValue.size) || !this.cacheDisabled) {
99
116
  const dataStr = this.name ? (_a = this.storage) == null ? void 0 : _a.getItem(this.name) : null;
100
117
  const existingValue = this.read(dataStr != null ? dataStr : null);
101
118
  if (isDefined(dataStr)) firstValue = existingValue;
102
- isEmpty = this.cacheDisabled || existingValue.size === 0;
103
119
  }
104
120
  (firstValue == null ? void 0 : firstValue.size) && this.subject$.next(firstValue);
105
121
  this.unsubscribe();
@@ -108,7 +124,8 @@ var _Store = class _Store {
108
124
  this.handleForceUpdateCache
109
125
  );
110
126
  }
111
- this.subscriptions.subject = this.subject$.pipe(skip(isEmpty ? 0 : 1)).subscribe(
127
+ const skipNum = this.subject$ instanceof BehaviorSubject && (firstValue == null ? void 0 : firstValue.size) && firstValue !== initialValue ? 1 : 0;
128
+ this.subscriptions.subject = this.subject$.pipe(skip(skipNum)).subscribe(
112
129
  !this.cacheDisabled && this.delay > 0 ? deferred(this.handleSubjectChange, this.delay, {
113
130
  thisArg: this,
114
131
  ...this.delayOptions
@@ -144,12 +161,17 @@ var _Store = class _Store {
144
161
  };
145
162
  this.search = (...args) => search(this.getAll(), ...args);
146
163
  this.set = (key, value) => {
164
+ var _a, _b;
147
165
  const data = this.getAll();
148
- data.set(key, isFn(value) ? value(data.get(key)) : value);
166
+ const _value = isFn(value) ? value(data.get(key)) : value;
167
+ (_b = (_a = this.validate) == null ? void 0 : _a.set) == null ? void 0 : _b.call(this, [key, _value], "set");
168
+ data.set(key, _value);
149
169
  return this.setAll(data, true);
150
170
  };
151
171
  this.setAll = (data, replace = false) => {
172
+ var _a, _b;
152
173
  if (!isMap(data)) return this;
174
+ (_b = (_a = this.validate) == null ? void 0 : _a.setAll) == null ? void 0 : _b.call(this, [data, replace], "setAll");
153
175
  data = replace ? data : mapJoin(this.getAll(), data);
154
176
  this.subject$.next(new Map(data));
155
177
  return this;
@@ -204,17 +226,20 @@ var _Store = class _Store {
204
226
  var _a, _b;
205
227
  (_a = this.subscriptions.forceUpdateCache) == null ? void 0 : _a.unsubscribe();
206
228
  (_b = this.subscriptions.subject) == null ? void 0 : _b.unsubscribe();
207
- this.subscriptions = {};
229
+ this.subscriptions = {
230
+ forceUpdateCache: void 0,
231
+ subject: void 0
232
+ };
208
233
  };
209
234
  this.values = () => getValues(this.getAll());
210
235
  this.write = (data) => {
211
- var _a;
236
+ var _a, _b, _c;
237
+ this.init();
238
+ data != null ? data : data = (_a = this.subject$) == null ? void 0 : _a.value;
239
+ if (!isMap(data) || !this.name || !this.storage) return false;
240
+ (_c = (_b = this.validate) == null ? void 0 : _b.write) == null ? void 0 : _c.call(this, [data], "write");
212
241
  try {
213
- !this.initialized && this.init();
214
- data != null ? data : data = (_a = this.subject$) == null ? void 0 : _a.value;
215
- if (!isMap(data)) return false;
216
242
  const jsonStr = this.toString(data);
217
- if (!this.name || !this.storage) return false;
218
243
  this.storage.setItem(this.name, jsonStr);
219
244
  return true;
220
245
  } catch (err) {
@@ -222,6 +247,7 @@ var _Store = class _Store {
222
247
  return false;
223
248
  }
224
249
  };
250
+ this.name = `${name != null ? name : ""}`.trim() || null;
225
251
  const {
226
252
  cacheDisabled = false,
227
253
  delay,
@@ -232,25 +258,30 @@ var _Store = class _Store {
232
258
  parse,
233
259
  spaces,
234
260
  storage = fallbackIfFails(
235
- () => globalThis.localStorage,
261
+ () => !name ? void 0 : globalThis.localStorage,
236
262
  [],
237
263
  void 0
238
264
  ),
239
- stringify
265
+ stringify,
266
+ validate = {}
240
267
  } = options != null ? options : {};
241
- this.name = `${name != null ? name : ""}`.trim() || null;
242
- if (this.name && !storage && !(options == null ? void 0 : options.checkStorageOnInit))
243
- throw new Error(_Store.messages.invalidOptionsStorage);
268
+ if (this.name && !storage && storage !== null)
269
+ throw new Error(_Store.messages.invalidStorageOptions);
244
270
  this.cacheDisabled = !!storage && cacheDisabled;
245
271
  this.delay = delay === 0 || isPositiveNumber(delay) ? delay : 300;
246
272
  this.delayOptions = delayOptions;
247
273
  this.onError = onError;
248
274
  this.onChange = onChange;
249
275
  this.parse = parse == null ? void 0 : parse.bind(this);
276
+ this.spaces = spaces;
250
277
  this.storage = storage;
251
278
  this.stringify = stringify == null ? void 0 : stringify.bind(this);
252
- this.spaces = spaces;
253
279
  this.subject$ = this.cacheDisabled ? new Subject() : new BehaviorSubject(/* @__PURE__ */ new Map());
280
+ const _validate = { ...validate };
281
+ for (const [key, value] of Object.entries(_validate)) {
282
+ _validate[key] = value;
283
+ }
284
+ this.validate = _validate;
254
285
  isMap(initialValue) && initialValue.size && this.init(initialValue, false);
255
286
  }
256
287
  get size() {
@@ -259,7 +290,8 @@ var _Store = class _Store {
259
290
  };
260
291
  _Store.messages = Object.seal({
261
292
  invalidJsonEntries: "Invalid JSON format. Parsed value must be a 2D array representing key-value pairs.",
262
- invalidOptionsStorage: "options.storage: LocalStorage instance or equivalent required"
293
+ invalidStorageOptions: "options.storage: LocalStorage instance or equivalent required. For NodeJS, use `node-localstorage` NPM module.",
294
+ validationError: "Validation failed. Action: "
263
295
  });
264
296
  /**
265
297
  * Trigger forced update of cached data from storage.
@@ -278,6 +310,10 @@ var Store_default = Store;
278
310
 
279
311
  // src/createStore.ts
280
312
  function createStore(name, options) {
313
+ if (isObj(name)) {
314
+ options = name;
315
+ name = null;
316
+ }
281
317
  const store = new Store_default(name, options);
282
318
  Object.defineProperty(store, "context", {
283
319
  configurable: false,
@@ -291,13 +327,17 @@ var createStore_default = createStore;
291
327
 
292
328
  // src/createObjectStore.ts
293
329
  function createObjectStore(name, options) {
330
+ if (isObj2(name)) {
331
+ options = name;
332
+ name = null;
333
+ }
294
334
  const store = createStore_default(name, {
295
335
  parse: (str) => objToMap(JSON.parse(str != null ? str : "{}")),
296
336
  stringify: function(data) {
297
337
  return JSON.stringify(this.toObject(data));
298
338
  },
299
339
  ...options,
300
- initialValue: !isObj(options == null ? void 0 : options.initialValue, true) ? options == null ? void 0 : options.initialValue : objToMap(options.initialValue)
340
+ initialValue: !isObj2(options == null ? void 0 : options.initialValue, true) ? options == null ? void 0 : options.initialValue : objToMap(options.initialValue)
301
341
  });
302
342
  store.type = "object";
303
343
  return store;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Toufiqur Rahaman Chowdhury",
3
3
  "description": "Generic, reactive, persistent and fully-typed Map-like data store with advanced search, filtering, and sorting capabilities.",
4
4
  "dependencies": {
5
- "@superutils/core": "^1.2.19",
5
+ "@superutils/core": "^1.2.21",
6
6
  "rxjs": "^7.8.2"
7
7
  },
8
8
  "exports": {
@@ -45,6 +45,6 @@
45
45
  "sideEffects": false,
46
46
  "type": "module",
47
47
  "types": "./dist/index.d.ts",
48
- "version": "0.1.15",
49
- "gitHead": "cd4a22d77abb8c5cf30845facb2f1afc43d0397f"
48
+ "version": "0.1.18",
49
+ "gitHead": "54a29f73ae09335819d591df271d9f57c92da457"
50
50
  }