@superutils/store 0.1.14

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/LICENSE ADDED
File without changes
package/README.md ADDED
@@ -0,0 +1,249 @@
1
+ # @superutils/store
2
+
3
+ A generic, reactive, persistent and fully-typed Map-like data store with advanced search, filtering, and sorting capabilities. It supports both in-memory caching and persistent storage (LocalStorage in browsers, or JSON files in NodeJS).
4
+
5
+ Built on RxJS for reactive data handling, it is optimized for small to medium datasets and provides a seamless way to manage application state with optional persistence.
6
+
7
+ <div v-if="false">
8
+
9
+ For full API reference check out the [docs page](https://alien45.github.io/superutils/packages/@superutils/store/).
10
+
11
+ </div>
12
+
13
+ ## Table of Contents
14
+
15
+ - [Installation](#installation)
16
+ - [Basic Usage](#basic-usage)
17
+ - [Map-Based Store](#map-store)
18
+ - [Object-based Store](#object-based-store)
19
+ - [Persistent Storage (NodeJS)](#persistent-storage-nodejs)
20
+ - [Advanced Usage](#advanced-usage)
21
+ - [Reactive Updates (RxJS & Callbacks)](#reactive-updates-rxjs--callbacks)
22
+ - [Search and Filtering](#search-and-filtering)
23
+ - [Attaching Context (Business Logic)](#attaching-context-business-logic)
24
+ - [Object-based Store With Context](#object-based-store)
25
+ - [OOP: Subclassing Store](#oop-subclassing-store)
26
+
27
+ ## Installation
28
+
29
+ ### NPM
30
+
31
+ Install using your favorite package manager (e.g., `npm`, `yarn`, `pnpm`, `bun`, etc.):
32
+
33
+ ```bash
34
+ npm install @superutils/store
35
+ ```
36
+
37
+ Dependency: `@superutils/core` will be automatically installed by package manager
38
+
39
+ ### CDN / Browser
40
+
41
+ If you are not using a bundler, you can include the minified browser build directly:
42
+
43
+ ```xml
44
+ <script src="https://unpkg.com/@superutils/store@latest/dist/browser/index.min.js"></script>
45
+ ```
46
+
47
+ OR,
48
+
49
+ ```xml
50
+ <script src="https://cdn.jsdelivr.net/npm/@superutils/store/dist/browser/index.min.js"></script>
51
+ ```
52
+
53
+ ## Basic Usage
54
+
55
+ ### Map-based Store
56
+
57
+ The `Store` class can be used just like a standard JavaScript `Map`, but with the added benefit of optional persistence and reactivity.
58
+
59
+ ```javascript
60
+ import { createStore, Store } from '@superutils/store'
61
+
62
+ // Initialize a store (in-memory only if no name is provided)
63
+ const userStorage = createStore('users') // or `new Store('users')`
64
+
65
+ // Set and get values
66
+ userStorage.set('alice', { name: 'Alice', age: 30 })
67
+
68
+ // functional update
69
+ userStorage.set('alice', alice => alice ?? { name: 'Alice', age: 30 })
70
+
71
+ console.log(userStorage.get('alice')) // prints: { name: 'Alice', age: 30 }
72
+ console.log(userStorage.size) // 1
73
+ ```
74
+
75
+ ### Object-based Store
76
+
77
+ `createObjectStore` provides a type-safe way to manage a single plain object as a store, where keys of the object become keys in the store.
78
+
79
+ ```javascript
80
+ import { createObjectStore } from '@superutils/store'
81
+
82
+ const userStore = createObjectStore('user-profile', {
83
+ initialValue: {
84
+ age: 25,
85
+ name: 'Jane Doe',
86
+ roles: ['guest'],
87
+ },
88
+ })
89
+
90
+ console.log(userStore.get('name'), userStore.get('age')) // Prints: 'Jane Doe' 25
91
+
92
+ console.log(userStore.toObject()) // prints: { age: 25, name: 'Jane Doe', roles: [ 'guest' ] }
93
+ ```
94
+
95
+ ### Persistent Storage (NodeJS)
96
+
97
+ In NodeJS environments, you can use `node-localstorage` to persist your data to the file system.
98
+
99
+ ```javascript
100
+ import { createStore } from '@superutils/store'
101
+ import { LocalStorage } from 'node-localstorage'
102
+
103
+ // Provide a localStorage implementation for NodeJS that can be used throughout the application mimicking the browser LocalStorage behavior.
104
+ globalThis.localStorage = new LocalStorage(
105
+ './data', // directory to store files in
106
+ 1e7, // max file size
107
+ )
108
+
109
+ const storage = createStore('settings.json')
110
+ storage.set('theme', 'dark') // Automatically saved to ./data/settings.json
111
+
112
+ /**
113
+ * Alternatively, you can also provide a LocalStorage instance to each Store instance
114
+ */
115
+ createStore('settings.json', { storage: new LocalStorage('./data', 1e7) })
116
+ ```
117
+
118
+ ## Advanced Usage
119
+
120
+ ### Reactive Updates (RxJS & Callbacks)
121
+
122
+ You can subscribe to changes using the internal RxJS Subject or a simple `onChange` callback.
123
+
124
+ ```javascript
125
+ import { createStore } from '@superutils/store'
126
+
127
+ const storage = createStore('my-data', {
128
+ onChange: data => console.log('Data changed!', data),
129
+ })
130
+
131
+ // Or use the RxJS subject directly
132
+ const sub = storage.subject$.subscribe(data => {
133
+ console.log('Reactive update:', data)
134
+ })
135
+
136
+ storage.set('key', 'value')
137
+ ```
138
+
139
+ ### Search and Filtering
140
+
141
+ `Store` provides powerful search and filter capabilities directly on your data.
142
+
143
+ ```javascript
144
+ import { createStore } from '@superutils/store'
145
+
146
+ const storage = createStore('products', {
147
+ // Instantiate the storage with initial value
148
+ initialValue: new Map([
149
+ [1, { id: 1, name: 'Laptop', category: 'electronics', price: 1000 }],
150
+ [2, { id: 2, name: 'Chair', category: 'furniture', price: 150 }],
151
+ ]),
152
+ })
153
+
154
+ // Search for items using a query object
155
+ const searchResult = storage.search({
156
+ query: { category: 'electronics' },
157
+ })
158
+ console.log(searchResult) // [{ id: 1, name: 'Laptop', ... }]
159
+
160
+ // Filter items using a predicate
161
+ const expensiveItems = storage.filter(val => val.price > 500)
162
+ ```
163
+
164
+ ### Attaching Context (Business Logic)
165
+
166
+ Using `createStore`, you can attach custom business logic (context) to your store instance, allowing you to encapsulate operations without having to create a subclass.
167
+
168
+ ```javascript
169
+ import { createStore } from '@superutils/store'
170
+
171
+ const authStore = createStore('auth', {
172
+ context: store => ({
173
+ isAuthenticated: () => store.has('token'),
174
+ login: async () => {
175
+ store.set('token', 'some-token')
176
+ },
177
+ logout: () => store.delete('token'),
178
+ }),
179
+ initialValue: new Map(),
180
+ })
181
+
182
+ // Access your custom logic via the .context property
183
+ if (!authStore.context.isAuthenticated()) {
184
+ authStore.context.login().then(() => console.log('Logged in'))
185
+ }
186
+ ```
187
+
188
+ ### Object-based Store With Context
189
+
190
+ `createObjectStore` supports context the same way as `createStore`.
191
+
192
+ ```typescript
193
+ import { createObjectStore } from '@superutils/store'
194
+ import fetch from '@superutils/fetch'
195
+
196
+ type UserProfile = {
197
+ age: number
198
+ name: string
199
+ roles: string[]
200
+ }
201
+
202
+ const userStore = createObjectStore('user-profile', {
203
+ initialValue: {
204
+ age: 25,
205
+ name: 'Jane Doe',
206
+ roles: ['guest'],
207
+ } as UserProfile,
208
+ context: store => ({
209
+ promoteToAdmin() {
210
+ // Update properties with type safety
211
+ store.set('roles', (prev = []) => [...prev, 'admin'])
212
+ },
213
+ }),
214
+ })
215
+
216
+ userStore.context.promoteToAdmin()
217
+ console.log(userStore.get('roles')) // ['guest', 'admin']
218
+ ```
219
+
220
+ ### OOP: Subclassing Store
221
+
222
+ You can extend the `Store` class to create custom store implementations with specialized logic or default behaviors.
223
+
224
+ ```typescript
225
+ import { Store } from '@superutils/store'
226
+
227
+ interface Product {
228
+ id: number
229
+ name: string
230
+ price: number
231
+ inStock: boolean
232
+ }
233
+
234
+ class ProductStore extends Store<number, Product> {
235
+ constructor(name: string, options?: Parameters<typeof Store>[1]) {
236
+ super(name, { ...options, delay: 100 }) // Set a default delay for this store type
237
+ }
238
+
239
+ getInStockProducts(): Map<number, Product> {
240
+ return this.filter(product => product.inStock)
241
+ }
242
+ }
243
+
244
+ const products = new ProductStore('my-products')
245
+ products.set(1, { id: 1, name: 'Laptop', price: 1200, inStock: true })
246
+ products.set(2, { id: 2, name: 'Mouse', price: 25, inStock: false })
247
+
248
+ console.log(products.getInStockProducts()) // Map { 1 => { id: 1, name: 'Laptop', price: 1200, inStock: true } }
249
+ ```
@@ -0,0 +1,4 @@
1
+ this.superutils=this.superutils||{};this.superutils.store=(function(exports){'use strict';var g=(r,e=true)=>!!r&&typeof r=="object"&&(!e||[Object.prototype,null].includes(Object.getPrototypeOf(r)));var T=r=>Array.isArray(r);var re=r=>T(r)&&r.every(T),We=r=>T(r)||r instanceof Set||r instanceof Map;var Je=r=>Number.isInteger(r);var te=r=>typeof r=="number"&&!Number.isNaN(r)&&Number.isFinite(r),qe=r=>Je(r)&&r>0,ge=r=>te(r)&&r>0,xe=(r,e=false,t=false)=>{if(r==null)return true;switch(typeof r){case "number":return !te(r);case "string":return !r.replaceAll(" ","").trim().length;case "boolean":case "bigint":case "symbol":case "function":return false}if(r instanceof Date)return Number.isNaN(r.getTime());if(r instanceof Map||r instanceof Set)return !r.size;if(Array.isArray(r)||r instanceof Uint8Array)return !r.length;if(r instanceof Error)return !r.message.length;let o=typeof r=="object"&&Object.getPrototypeOf(r);return o===Object.prototype||o===null?e?!Object.getOwnPropertyNames(r).length:!Object.keys(r).length:t};var m=r=>typeof r=="function";var S=r=>r instanceof Map;var Se=r=>r!=null;var He=r=>r instanceof Promise,$=r=>r instanceof RegExp,Ge=r=>r instanceof Set,M=r=>typeof r=="string";var Ye=r=>typeof r=="symbol";var E=(r,e,t)=>{try{let o=m(r)?r(...m(e)?e():e):r;return He(o)?o.catch(n=>m(t)?t(n):t):o}catch(o){return m(t)?t(o):t}},j=E;new Date().getTime();var Xe=(r,e=true,t=true)=>j(()=>[...t&&Object.getOwnPropertySymbols(r)||[],...e?Object.keys(r).sort():Object.keys(r)],[],[]),Ze=Xe,F=(r,e="null")=>JSON.parse(j(JSON.stringify,[r],e)),we=(r,e,t,o=false,n=true)=>{let i=g(e,false)||m(e)?e:{};if(!g(r,false)&&!m(e))return i;let s=new Set(t!=null?t:[]),a=Ze(r,true,true).filter(l=>!s.has(l));for(let l of a){let c=l,u=r[c];if(i.hasOwnProperty(c)&&(o==="empty"?!xe(i[c]):m(o)?!o(c,i[c],u):true))continue;if([void 0,null,1/0,NaN].includes(u)||!g(u,false)){i[c]=u;continue}i[c]=(()=>{switch(Object.getPrototypeOf(u)){case Array.prototype:return F(u,"[]");case ArrayBuffer.prototype:return u.slice(0);case Date.prototype:return new Date(u.getTime());case Map.prototype:return new Map(F(Array.from(u),"[]"));case RegExp.prototype:return new RegExp(u);case Set.prototype:return new Set(F(Array.from(u)));case Uint8Array.prototype:return new Uint8Array([...u]);case URL.prototype:return new URL(u);}if(Ye(c)||!n)return F(u);let b=[...s].map(x=>String(x).startsWith(String(c).concat("."))&&String(x).split(String(c).concat("."))[1]).filter(Boolean);return b.length?we(u,i[c],b,o,n):F(u)})();}return i},Qe=(r=[],e=[],t)=>{g(t)||(t={});for(let o=0;o<(T(r)?r:[]).length;o++)t[r[o]]=e==null?void 0:e[o];return t};function q(r){let e=Object.entries(g(r)?r:{});return new Map(e.map(([t,o])=>[t,o]))}var ye=(r,e=true,t=false)=>T(r)?(t&&(r=[...r]),e?r.reverse():r):[];var J=(r,e=50,t={})=>{let{leading:o=J.defaults.leading,onError:n=J.defaults.onError,thisArg:i}=t,{tid:s}=t;i!==void 0&&(r=r.bind(i));let a=(...u)=>j(r,u,n),l=null,c=o==="global";return (...u)=>{clearTimeout(s),s=setTimeout(()=>{l!==u&&a(...u),l=c?true:null;},e),!(!o||l)&&(l=u,a(...u));}};J.defaults={leading:false,onError:void 0};var er=J,oe=(r,e=50,t={})=>{let{defaults:o}=oe,{onError:n=o.onError,trailing:i=o.trailing,thisArg:s}=t,{tid:a}=t,l=(...u)=>j(s!==void 0?r.bind(s):r,u,m(n)?f=>j(n,[f],void 0):void 0),c=null;return (...u)=>{if(a){c=u;return}a=setTimeout(()=>{if(a=void 0,!i)return;let f=c;c=null,f&&f!==u&&l(...f);},e),l(...u);}};oe.defaults={onError:void 0,trailing:false};var rr=oe,_e=(r,e=50,t={})=>t.throttle?rr(r,e,t):er(r,e,t),ne=(r,e,t,o,n)=>{var i;S(n)||(n=new Map),qe(t)||(t=1/0);for(let[s,a]of ((i=r==null?void 0:r.entries)==null?void 0:i.call(r))||[]){if(n.size>=t)break;j(e!=null?e:a,[a,s,r],false)&&n.set(s,a);}return o?n:[...n.values()]},tr=ne,or=r=>{if(!r)return 0;try{let e="size"in r?r.size:"length"in r?r.length:0;return te(e)?e:0}catch(e){return 0}},nr=or,ie=r=>{var e;return [...((e=r==null?void 0:r.values)==null?void 0:e.call(r))||[]]},ve=ie,D=(r,e)=>{var t;let o=!nr(r)||xe(e==null?void 0:e.query),n=S(e==null?void 0:e.result)?e.result:new Map,i=(t=e==null?void 0:e.asMap)!=null?t:D.defaults.asMap;if(o)return i?n:ve(n);e=we(D.defaults,e,[],"empty");let{ignoreCase:s,limit:a=1/0,matchAll:l,matchExact:c,ranked:u}=e,{query:f}=e,h=M(f),b=$(f),x=j(Object.keys,[f],[]);if(s&&!c&&!b&&(f=h?f.toLowerCase():Qe(x,Object.values(f).map(p=>$(p)?p:`${p}`.toLowerCase()))),e.query=f,u){let p=[];for(let[d,v]of r.entries()){let _=-1;if(h||b){_=W(e,v,void 0),_>=0&&p.push([_,d,v]);continue}let be=[];x[l?"every":"some"](N=>{let L=W(e,v,N);return be.push(L),L>=0})&&(_=be.sort((N,L)=>N-L).filter(N=>N!==-1)[0],_>=0&&p.push([_,d,v]));}p.sort((d,v)=>d[0]-v[0]).slice(0,a).forEach(([d,v,_])=>n.set(v,_));}else for(let[p,d]of r.entries()){if(n.size>=a)break;(h||b?W(e,d,void 0)>=0:x[l?"every":"some"](_=>W(e,d,_)>=0))&&n.set(p,d);}return i?n:ve(n)};D.defaults={asMap:true,ignoreCase:true,limit:1/0,matchAll:false,ranked:false,transform:true};function W({query:r,ignoreCase:e,matchExact:t,transform:o=true},n,i){var s,a;let l=M(r)||$(r)||i===void 0,c=l?r:r[i],u=l||!g(n)?n:n[i],f=j(()=>m(o)?o(n,l?void 0:u,i):t&&o===!1?u:g(u,!1)?JSON.stringify(We(u)?[...u.values()]:Object.values(u)):String(u!=null?u:""),[],"");if(f===c)return 0;if(t&&!$(c))return -1;let h=String(f);return h.trim()?$(c)?(a=(s=h.match(c))==null?void 0:s.index)!=null?a:-1:(e&&(h=h.toLowerCase()),h.indexOf(String(c))):-1}var ir=D;function Oe(r,e){return (m(e)?tr(r,e,1):ir(r,{...e,asMap:true,limit:1}))[e!=null&&e.includeKey?"entries":"values"]().next().value}var Te=r=>{var e;return [...((e=r==null?void 0:r.entries)==null?void 0:e.call(r))||[]]},Ee=r=>{var e;return [...((e=r==null?void 0:r.keys)==null?void 0:e.call(r))||[]]};function H(r,e,t){let o=T(r)?1:S(r)?2:Ge(r)?3:0;if(!o)return r;let{asString:n,ignoreCase:i,newInstance:s,reverse:a,undefinedFirst:l}={...H.defaults,...g(t)?t:g(e)?e:{}};if(o===1&&m(e))return ye(r.sort(e),a,s);let c=n?l?"":"Z".repeat(10):l?-1/0:1/0,u=m(e)?ye([...r.entries()].sort(e),a,false):(()=>{let f=1,h=p=>{let d=g(p)&&f!==0?p[e]:p;if(!n)return d;let v=`${d!=null?d:c}`;return i?v.toLowerCase():v},[b,x]=a?[-1,1]:[1,-1];return [1,3].includes(o)?(o===3||s?[...r]:r).sort((p,d)=>h(p)>h(d)?b:x):(f=e===true?0:1,[...r.entries()].sort((p,d)=>h(p==null?void 0:p[f])>h(d==null?void 0:d[f])?b:x))})();if(o===1)return u;if(s)return o===2?new Map(u):new Set(u);r.clear();for(let f of u)o===2?r.set(...f):r.add(f);return r}H.defaults={asString:true,ignoreCase:true,newInstance:false,reverse:false,undefinedFirst:false};var je=(...r)=>{var e;return new Map((e=r==null?void 0:r.flatMap)==null?void 0:e.call(r,t=>S(t)?Array.from(t.entries()):re(t)?t:[]))};var se=function(r,e){return se=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,o){t.__proto__=o;}||function(t,o){for(var n in o)Object.prototype.hasOwnProperty.call(o,n)&&(t[n]=o[n]);},se(r,e)};function O(r,e){if(typeof e!="function"&&e!==null)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");se(r,e);function t(){this.constructor=r;}r.prototype=e===null?Object.create(e):(t.prototype=e.prototype,new t);}function k(r){var e=typeof Symbol=="function"&&Symbol.iterator,t=e&&r[e],o=0;if(t)return t.call(r);if(r&&typeof r.length=="number")return {next:function(){return r&&o>=r.length&&(r=void 0),{value:r&&r[o++],done:!r}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function I(r,e){var t=typeof Symbol=="function"&&r[Symbol.iterator];if(!t)return r;var o=t.call(r),n,i=[],s;try{for(;(e===void 0||e-- >0)&&!(n=o.next()).done;)i.push(n.value);}catch(a){s={error:a};}finally{try{n&&!n.done&&(t=o.return)&&t.call(o);}finally{if(s)throw s.error}}return i}function V(r,e,t){if(arguments.length===2)for(var o=0,n=e.length,i;o<n;o++)(i||!(o in e))&&(i||(i=Array.prototype.slice.call(e,0,o)),i[o]=e[o]);return r.concat(i||Array.prototype.slice.call(e))}function y(r){return typeof r=="function"}function G(r){var e=function(o){Error.call(o),o.stack=new Error().stack;},t=r(e);return t.prototype=Object.create(Error.prototype),t.prototype.constructor=t,t}var Y=G(function(r){return function(t){r(this),this.message=t?t.length+` errors occurred during unsubscription:
2
+ `+t.map(function(o,n){return n+1+") "+o.toString()}).join(`
3
+ `):"",this.name="UnsubscriptionError",this.errors=t;}});function B(r,e){if(r){var t=r.indexOf(e);0<=t&&r.splice(t,1);}}var K=(function(){function r(e){this.initialTeardown=e,this.closed=false,this._parentage=null,this._finalizers=null;}return r.prototype.unsubscribe=function(){var e,t,o,n,i;if(!this.closed){this.closed=true;var s=this._parentage;if(s)if(this._parentage=null,Array.isArray(s))try{for(var a=k(s),l=a.next();!l.done;l=a.next()){var c=l.value;c.remove(this);}}catch(p){e={error:p};}finally{try{l&&!l.done&&(t=a.return)&&t.call(a);}finally{if(e)throw e.error}}else s.remove(this);var u=this.initialTeardown;if(y(u))try{u();}catch(p){i=p instanceof Y?p.errors:[p];}var f=this._finalizers;if(f){this._finalizers=null;try{for(var h=k(f),b=h.next();!b.done;b=h.next()){var x=b.value;try{Ce(x);}catch(p){i=i!=null?i:[],p instanceof Y?i=V(V([],I(i)),I(p.errors)):i.push(p);}}}catch(p){o={error:p};}finally{try{b&&!b.done&&(n=h.return)&&n.call(h);}finally{if(o)throw o.error}}}if(i)throw new Y(i)}},r.prototype.add=function(e){var t;if(e&&e!==this)if(this.closed)Ce(e);else {if(e instanceof r){if(e.closed||e._hasParent(this))return;e._addParent(this);}(this._finalizers=(t=this._finalizers)!==null&&t!==void 0?t:[]).push(e);}},r.prototype._hasParent=function(e){var t=this._parentage;return t===e||Array.isArray(t)&&t.includes(e)},r.prototype._addParent=function(e){var t=this._parentage;this._parentage=Array.isArray(t)?(t.push(e),t):t?[t,e]:e;},r.prototype._removeParent=function(e){var t=this._parentage;t===e?this._parentage=null:Array.isArray(t)&&B(t,e);},r.prototype.remove=function(e){var t=this._finalizers;t&&B(t,e),e instanceof r&&e._removeParent(this);},r.EMPTY=(function(){var e=new r;return e.closed=true,e})(),r})();var ae=K.EMPTY;function X(r){return r instanceof K||r&&"closed"in r&&y(r.remove)&&y(r.add)&&y(r.unsubscribe)}function Ce(r){y(r)?r():r.unsubscribe();}var w={Promise:void 0};var R={setTimeout:function(r,e){for(var t=[],o=2;o<arguments.length;o++)t[o-2]=arguments[o];return setTimeout.apply(void 0,V([r,e],I(t)))},clearTimeout:function(r){return (clearTimeout)(r)},delegate:void 0};function Ae(r){R.setTimeout(function(){throw r});}function ue(){}function U(r){r();}var z=(function(r){O(e,r);function e(t){var o=r.call(this)||this;return o.isStopped=false,t?(o.destination=t,X(t)&&t.add(o)):o.destination=lr,o}return e.create=function(t,o,n){return new Q(t,o,n)},e.prototype.next=function(t){this.isStopped?fe():this._next(t);},e.prototype.error=function(t){this.isStopped?fe():(this.isStopped=true,this._error(t));},e.prototype.complete=function(){this.isStopped?fe():(this.isStopped=true,this._complete());},e.prototype.unsubscribe=function(){this.closed||(this.isStopped=true,r.prototype.unsubscribe.call(this),this.destination=null);},e.prototype._next=function(t){this.destination.next(t);},e.prototype._error=function(t){try{this.destination.error(t);}finally{this.unsubscribe();}},e.prototype._complete=function(){try{this.destination.complete();}finally{this.unsubscribe();}},e})(K);var ar=(function(){function r(e){this.partialObserver=e;}return r.prototype.next=function(e){var t=this.partialObserver;if(t.next)try{t.next(e);}catch(o){Z(o);}},r.prototype.error=function(e){var t=this.partialObserver;if(t.error)try{t.error(e);}catch(o){Z(o);}else Z(e);},r.prototype.complete=function(){var e=this.partialObserver;if(e.complete)try{e.complete();}catch(t){Z(t);}},r})(),Q=(function(r){O(e,r);function e(t,o,n){var i=r.call(this)||this,s;if(y(t)||!t)s={next:t!=null?t:void 0,error:o!=null?o:void 0,complete:n!=null?n:void 0};else {s=t;}return i.destination=new ar(s),i}return e})(z);function Z(r){Ae(r);}function ur(r){throw r}function fe(r,e){}var lr={closed:true,next:ue,error:ur,complete:ue};var Ve=(function(){return typeof Symbol=="function"&&Symbol.observable||"@@observable"})();function Ke(r){return r}function Re(r){return r.length===0?Ke:r.length===1?r[0]:function(t){return r.reduce(function(o,n){return n(o)},t)}}var pe=(function(){function r(e){e&&(this._subscribe=e);}return r.prototype.lift=function(e){var t=new r;return t.source=this,t.operator=e,t},r.prototype.subscribe=function(e,t,o){var n=this,i=fr(e)?e:new Q(e,t,o);return U(function(){var s=n,a=s.operator,l=s.source;i.add(a?a.call(i,l):l?n._subscribe(i):n._trySubscribe(i));}),i},r.prototype._trySubscribe=function(e){try{return this._subscribe(e)}catch(t){e.error(t);}},r.prototype.forEach=function(e,t){var o=this;return t=Ue(t),new t(function(n,i){var s=new Q({next:function(a){try{e(a);}catch(l){i(l),s.unsubscribe();}},error:i,complete:n});o.subscribe(s);})},r.prototype._subscribe=function(e){var t;return (t=this.source)===null||t===void 0?void 0:t.subscribe(e)},r.prototype[Ve]=function(){return this},r.prototype.pipe=function(){for(var e=[],t=0;t<arguments.length;t++)e[t]=arguments[t];return Re(e)(this)},r.prototype.toPromise=function(e){var t=this;return e=Ue(e),new e(function(o,n){var i;t.subscribe(function(s){return i=s},function(s){return n(s)},function(){return o(i)});})},r.create=function(e){return new r(e)},r})();function Ue(r){var e;return (e=r!=null?r:w.Promise)!==null&&e!==void 0?e:Promise}function cr(r){return r&&y(r.next)&&y(r.error)&&y(r.complete)}function fr(r){return r&&r instanceof z||cr(r)&&X(r)}function pr(r){return y(r==null?void 0:r.lift)}function Ne(r){return function(e){if(pr(e))return e.lift(function(t){try{return r(t,this)}catch(o){this.error(o);}});throw new TypeError("Unable to lift unknown Observable type")}}function Fe(r,e,t,o,n){return new hr(r,e,t,o,n)}var hr=(function(r){O(e,r);function e(t,o,n,i,s,a){var l=r.call(this,t)||this;return l.onFinalize=s,l.shouldUnsubscribe=a,l._next=o?function(c){try{o(c);}catch(u){t.error(u);}}:r.prototype._next,l._error=i?function(c){try{i(c);}catch(u){t.error(u);}finally{this.unsubscribe();}}:r.prototype._error,l._complete=n?function(){try{n();}catch(c){t.error(c);}finally{this.unsubscribe();}}:r.prototype._complete,l}return e.prototype.unsubscribe=function(){var t;if(!this.shouldUnsubscribe||this.shouldUnsubscribe()){var o=this.closed;r.prototype.unsubscribe.call(this),!o&&((t=this.onFinalize)===null||t===void 0||t.call(this));}},e})(z);var $e=G(function(r){return function(){r(this),this.name="ObjectUnsubscribedError",this.message="object unsubscribed";}});var A=(function(r){O(e,r);function e(){var t=r.call(this)||this;return t.closed=false,t.currentObservers=null,t.observers=[],t.isStopped=false,t.hasError=false,t.thrownError=null,t}return e.prototype.lift=function(t){var o=new ke(this,this);return o.operator=t,o},e.prototype._throwIfClosed=function(){if(this.closed)throw new $e},e.prototype.next=function(t){var o=this;U(function(){var n,i;if(o._throwIfClosed(),!o.isStopped){o.currentObservers||(o.currentObservers=Array.from(o.observers));try{for(var s=k(o.currentObservers),a=s.next();!a.done;a=s.next()){var l=a.value;l.next(t);}}catch(c){n={error:c};}finally{try{a&&!a.done&&(i=s.return)&&i.call(s);}finally{if(n)throw n.error}}}});},e.prototype.error=function(t){var o=this;U(function(){if(o._throwIfClosed(),!o.isStopped){o.hasError=o.isStopped=true,o.thrownError=t;for(var n=o.observers;n.length;)n.shift().error(t);}});},e.prototype.complete=function(){var t=this;U(function(){if(t._throwIfClosed(),!t.isStopped){t.isStopped=true;for(var o=t.observers;o.length;)o.shift().complete();}});},e.prototype.unsubscribe=function(){this.isStopped=this.closed=true,this.observers=this.currentObservers=null;},Object.defineProperty(e.prototype,"observed",{get:function(){var t;return ((t=this.observers)===null||t===void 0?void 0:t.length)>0},enumerable:false,configurable:true}),e.prototype._trySubscribe=function(t){return this._throwIfClosed(),r.prototype._trySubscribe.call(this,t)},e.prototype._subscribe=function(t){return this._throwIfClosed(),this._checkFinalizedStatuses(t),this._innerSubscribe(t)},e.prototype._innerSubscribe=function(t){var o=this,n=this,i=n.hasError,s=n.isStopped,a=n.observers;return i||s?ae:(this.currentObservers=null,a.push(t),new K(function(){o.currentObservers=null,B(a,t);}))},e.prototype._checkFinalizedStatuses=function(t){var o=this,n=o.hasError,i=o.thrownError,s=o.isStopped;n?t.error(i):s&&t.complete();},e.prototype.asObservable=function(){var t=new pe;return t.source=this,t},e.create=function(t,o){return new ke(t,o)},e})(pe);var ke=(function(r){O(e,r);function e(t,o){var n=r.call(this)||this;return n.destination=t,n.source=o,n}return e.prototype.next=function(t){var o,n;(n=(o=this.destination)===null||o===void 0?void 0:o.next)===null||n===void 0||n.call(o,t);},e.prototype.error=function(t){var o,n;(n=(o=this.destination)===null||o===void 0?void 0:o.error)===null||n===void 0||n.call(o,t);},e.prototype.complete=function(){var t,o;(o=(t=this.destination)===null||t===void 0?void 0:t.complete)===null||o===void 0||o.call(t);},e.prototype._subscribe=function(t){var o,n;return (n=(o=this.source)===null||o===void 0?void 0:o.subscribe(t))!==null&&n!==void 0?n:ae},e})(A);var he=(function(r){O(e,r);function e(t){var o=r.call(this)||this;return o._value=t,o}return Object.defineProperty(e.prototype,"value",{get:function(){return this.getValue()},enumerable:false,configurable:true}),e.prototype._subscribe=function(t){var o=r.prototype._subscribe.call(this,t);return !o.closed&&t.next(this._value),o},e.prototype.getValue=function(){var t=this,o=t.hasError,n=t.thrownError,i=t._value;if(o)throw n;return this._throwIfClosed(),i},e.prototype.next=function(t){r.prototype.next.call(this,this._value=t);},e})(A);function Be(r,e){return Ne(function(t,o){var n=0;t.subscribe(Fe(o,function(i){return r.call(e,i,n++)&&o.next(i)}));})}function de(r){return Be(function(e,t){return r<=t})}var dr=(s=>(s.onChange="onChange",s.parse="parse",s.parse_json="parse-json",s.stringify="stringify",s.stringify_json="stringify-json",s.write="write",s))(dr||{});var ze=new A,P=class P{constructor(e,t){this.initialized=false;this.subscriptions={subject:void 0,forceUpdateCache:void 0};this.type="map";this.clear=()=>this.setAll(new Map,true);this.delete=e=>{T(e)||(e=[e]);let t=this.getAll();for(let o of e)t.delete(o);return this.setAll(t,true),this};this.filter=(...e)=>ne(this.getAll(),...e);this.find=e=>Oe(this.getAll(),e);this.get=e=>this.getAll().get(e);this.getAll=(e=false)=>{let t=this.initialized;if(t||this.init(),this.cacheDisabled||this.storage&&e){let n=this.read();return (e||!t&&n.size)&&this.subject$.next(n),n}return this.subject$.value};this.handleForceUpdateCache=e=>{if(!(this.name?T(e)?e.includes(this.name):M(e)?e===this.name:e===true:false))return;let o=this.read();this.subject$.next(o);};this.handleSubjectChange=e=>{var t;if(!S(e))return this.subject$.next(new Map);this.write(e),E((t=this.onChange)==null?void 0:t.bind(this),[e],this.triggerOnErrorCb("onChange"));};this.has=e=>this.getAll().has(e);this.init=(e,t=true)=>{var i;if(this.initialized)return false;if(this.initialized=true,t&&this.name&&!this.storage)throw new Error(P.messages.invalidOptionsStorage);let o=!!this.cacheDisabled,n=e;if(e!=null&&e.size||!this.cacheDisabled){let s=this.name?(i=this.storage)==null?void 0:i.getItem(this.name):null,a=this.read(s!=null?s:null);Se(s)&&(n=a),o=this.cacheDisabled||a.size===0;}return n!=null&&n.size&&this.subject$.next(n),this.unsubscribe(),this.cacheDisabled||(this.subscriptions.forceUpdateCache=ze.subscribe(this.handleForceUpdateCache)),this.subscriptions.subject=this.subject$.pipe(de(o?0:1)).subscribe(!this.cacheDisabled&&this.delay>0?_e(this.handleSubjectChange,this.delay,{thisArg:this,...this.delayOptions}):this.handleSubjectChange),true};this.keys=()=>Ee(this.getAll());this.map=e=>this.toArray().map(([t,o],n,i)=>e(o,t,i,n));this.read=(e=(o=>(o=this.name&&(t=>(t=this.storage)==null?void 0:t.getItem(this.name))())!=null?o:null)())=>{var i;let n=E(this.parse,[e],this.triggerOnErrorCb("parse",void 0));return S(n)||!M(e)?(i=m(this.parse)?n:this.subject$.value)!=null?i:new Map:new Map(E(()=>{let s=JSON.parse(e);if(!re(s))throw new Error(P.messages.invalidJsonEntries);return s},[],this.triggerOnErrorCb("parse-json")))};this.search=(...e)=>D(this.getAll(),...e);this.set=(e,t)=>{let o=this.getAll();return o.set(e,m(t)?t(o.get(e)):t),this.setAll(o,true)};this.setAll=(e,t=false)=>S(e)?(e=t?e:je(this.getAll(),e),this.subject$.next(new Map(e)),this):this;this.sort=(...e)=>{var o;let t=H(this.getAll(),e[0],e[1]);return (o=e[1])!=null&&o.save&&this.setAll(t,true),t};this.toArray=()=>Te(this.getAll());this.toJSON=(e,t=this.spaces,o=this.getAll())=>{let n=E(this.stringify,[o],this.triggerOnErrorCb("stringify",""));return M(n)?n:E(()=>JSON.stringify(Array.from(o),e,t),[],this.triggerOnErrorCb("stringify-json",""))};this.toObject=(e=this.getAll())=>{let t={};if(!S(e))return t;for(let[o,n]of e)t[o]=n;return t};this.toString=(e=this.getAll())=>this.toJSON(void 0,void 0,e);this.triggerOnErrorCb=(e,t=void 0)=>o=>{var n;return E((n=this.onError)==null?void 0:n.bind(this),[o,e],""),t};this.unsubscribe=()=>{var e,t;(e=this.subscriptions.forceUpdateCache)==null||e.unsubscribe(),(t=this.subscriptions.subject)==null||t.unsubscribe(),this.subscriptions={};};this.values=()=>ie(this.getAll());this.write=e=>{var t;try{if(!this.initialized&&this.init(),e!=null||(e=(t=this.subject$)==null?void 0:t.value),!S(e))return !1;let o=this.toString(e);return !this.name||!this.storage?!1:(this.storage.setItem(this.name,o),!0)}catch(o){return this.triggerOnErrorCb("write")(o),false}};let{cacheDisabled:o=false,delay:n,delayOptions:i,initialValue:s,onError:a,onChange:l,parse:c,spaces:u,storage:f=E(()=>globalThis.localStorage,[],void 0),stringify:h}=t!=null?t:{};if(this.name=`${e!=null?e:""}`.trim()||null,this.name&&!f&&!(t!=null&&t.checkStorageOnInit))throw new Error(P.messages.invalidOptionsStorage);this.cacheDisabled=!!f&&o,this.delay=n===0||ge(n)?n:300,this.delayOptions=i,this.onError=a,this.onChange=l,this.parse=c==null?void 0:c.bind(this),this.storage=f,this.stringify=h==null?void 0:h.bind(this),this.spaces=u,this.subject$=this.cacheDisabled?new A:new he(new Map),S(s)&&s.size&&this.init(s,false);}get size(){return this.getAll().size}};P.messages=Object.seal({invalidJsonEntries:"Invalid JSON format. Parsed value must be a 2D array representing key-value pairs.",invalidOptionsStorage:"options.storage: LocalStorage instance or equivalent required"}),P.forceUpdateCache=e=>{ze.next(e);};var me=P,ee=me;function mr(r,e){let t=new ee(r,e);return Object.defineProperty(t,"context",{configurable:false,enumerable:false,value:m(e==null?void 0:e.context)?e.context(t):e==null?void 0:e.context,writable:false}),t}var Le=mr;function qt(r,e){let t=Le(r,{parse:o=>q(JSON.parse(o!=null?o:"{}")),stringify:function(o){return JSON.stringify(this.toObject(o))},...e,initialValue:g(e==null?void 0:e.initialValue,true)?q(e.initialValue):e==null?void 0:e.initialValue});return t.type="object",t}var Yt=ee;exports.Store=me;exports.Store_OnErrorType=dr;exports.createObjectStore=qt;exports.createStore=mr;exports.default=Yt;exports.forceUpdateCache$=ze;exports.objToMap=q;Object.defineProperty(exports,'__esModule',{value:true});return exports;})({});//# sourceMappingURL=index.min.js.map
4
+ //# sourceMappingURL=index.min.js.map