react-rock 3.2.6 → 3.2.8

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.
@@ -1,10 +1,12 @@
1
1
  "use client";
2
- import { useId, useState, useEffect } from 'react';
3
- import { xv } from 'xanv';
2
+ 'use strict';
4
3
 
5
- const uid = useId;
6
- const ustate = useState;
7
- const ueffect = useEffect;
4
+ var react = require('react');
5
+ var xanv = require('xanv');
6
+
7
+ const uid = react.useId;
8
+ const ustate = react.useState;
9
+ const ueffect = react.useEffect;
8
10
  class Store {
9
11
  constructor(rowSchema, metaSchema) {
10
12
  this._rows = [];
@@ -36,7 +38,7 @@ class Store {
36
38
  });
37
39
  }, 0);
38
40
  };
39
- this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xv.number(), vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
41
+ this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xanv.xv.number(), vid: xanv.xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
40
42
  this._meta_schema = metaSchema;
41
43
  }
42
44
  rows(observe = true) {
@@ -111,105 +113,104 @@ class Store {
111
113
  return this._rows;
112
114
  }
113
115
  const rows = [];
114
- for (let key in where) {
115
- const wv = where[key];
116
- if (typeof wv === "object" && wv !== null) {
117
- // QueryValueType
118
- for (let row of this._rows) {
119
- let match = true;
120
- const rvalue = row[key];
121
- if (wv.contain !== undefined) {
122
- if (typeof rvalue === "string" && typeof wv.contain === "string") {
123
- if (!rvalue.includes(wv.contain)) {
124
- match = false;
116
+ for (const row of this._rows) {
117
+ const match = Object.keys(row).find(column => {
118
+ let _match = true;
119
+ const rvalue = row[column];
120
+ for (let wcol in where) {
121
+ const wv = where[wcol];
122
+ if (typeof wv === "object" && wv !== null) {
123
+ if (wv.contain !== undefined) {
124
+ if (typeof rvalue === "string" && typeof wv.contain === "string") {
125
+ if (!rvalue.includes(wv.contain)) {
126
+ _match = false;
127
+ }
125
128
  }
126
- }
127
- else {
128
- match = false;
129
- }
130
- }
131
- if (wv.startWith !== undefined) {
132
- if (typeof rvalue === "string" && typeof wv.startWith === "string") {
133
- if (!rvalue.startsWith(wv.startWith)) {
134
- match = false;
129
+ else {
130
+ _match = false;
135
131
  }
136
132
  }
137
- else {
138
- match = false;
139
- }
140
- }
141
- if (wv.endWith !== undefined) {
142
- if (typeof rvalue === "string" && typeof wv.endWith === "string") {
143
- if (!rvalue.endsWith(wv.endWith)) {
144
- match = false;
133
+ if (wv.startWith !== undefined) {
134
+ if (typeof rvalue === "string" && typeof wv.startWith === "string") {
135
+ if (!rvalue.startsWith(wv.startWith)) {
136
+ _match = false;
137
+ }
145
138
  }
146
- }
147
- else {
148
- match = false;
149
- }
150
- }
151
- if (wv.equalWith !== undefined) {
152
- if (rvalue !== wv.equalWith) {
153
- match = false;
154
- }
155
- }
156
- if (wv.notEqualWith !== undefined) {
157
- if (rvalue === wv.notEqualWith) {
158
- match = false;
159
- }
160
- }
161
- if (wv.gt !== undefined) {
162
- if (typeof rvalue === "number") {
163
- if (!(rvalue > wv.gt)) {
164
- match = false;
139
+ else {
140
+ _match = false;
165
141
  }
166
142
  }
167
- else {
168
- match = false;
143
+ if (wv.endWith !== undefined) {
144
+ if (typeof rvalue === "string" && typeof wv.endWith === "string") {
145
+ if (!rvalue.endsWith(wv.endWith)) {
146
+ _match = false;
147
+ }
148
+ }
149
+ else {
150
+ _match = false;
151
+ }
169
152
  }
170
- }
171
- if (wv.lt !== undefined) {
172
- if (typeof rvalue === "number") {
173
- if (!(rvalue < wv.lt)) {
174
- match = false;
153
+ if (wv.equalWith !== undefined) {
154
+ if (rvalue !== wv.equalWith) {
155
+ _match = false;
175
156
  }
176
157
  }
177
- else {
178
- match = false;
158
+ if (wv.notEqualWith !== undefined) {
159
+ if (rvalue === wv.notEqualWith) {
160
+ _match = false;
161
+ }
179
162
  }
180
- }
181
- if (wv.gte !== undefined) {
182
- if (typeof rvalue === "number") {
183
- if (!(rvalue >= wv.gte)) {
184
- match = false;
163
+ if (wv.gt !== undefined) {
164
+ if (typeof rvalue === "number") {
165
+ if (!(rvalue > wv.gt)) {
166
+ _match = false;
167
+ }
168
+ }
169
+ else {
170
+ _match = false;
185
171
  }
186
172
  }
187
- else {
188
- match = false;
173
+ if (wv.lt !== undefined) {
174
+ if (typeof rvalue === "number") {
175
+ if (!(rvalue < wv.lt)) {
176
+ _match = false;
177
+ }
178
+ }
179
+ else {
180
+ _match = false;
181
+ }
189
182
  }
190
- }
191
- if (wv.lte !== undefined) {
192
- if (typeof rvalue === "number") {
193
- if (!(rvalue <= wv.lte)) {
194
- match = false;
183
+ if (wv.gte !== undefined) {
184
+ if (typeof rvalue === "number") {
185
+ if (!(rvalue >= wv.gte)) {
186
+ _match = false;
187
+ }
188
+ }
189
+ else {
190
+ _match = false;
195
191
  }
196
192
  }
197
- else {
198
- match = false;
193
+ if (wv.lte !== undefined) {
194
+ if (typeof rvalue === "number") {
195
+ if (!(rvalue <= wv.lte)) {
196
+ _match = false;
197
+ }
198
+ }
199
+ else {
200
+ _match = false;
201
+ }
199
202
  }
203
+ continue;
200
204
  }
201
- if (match) {
202
- rows.push(row);
203
- }
204
- }
205
- }
206
- else {
207
- // RowvType
208
- for (let row of this._rows) {
209
- if (row[key] === wv) {
210
- rows.push(row);
205
+ if (wv !== rvalue) {
206
+ _match = false;
207
+ break;
211
208
  }
212
209
  }
210
+ return _match;
211
+ });
212
+ if (match) {
213
+ rows.push(row);
213
214
  }
214
215
  }
215
216
  return rows;
@@ -230,9 +231,7 @@ class Store {
230
231
  return -1;
231
232
  }
232
233
  move(fromIndex, toIndex, observe = true) {
233
- if (fromIndex < 0 || fromIndex >= this._rows.length)
234
- return false;
235
- if (toIndex < 0 || toIndex >= this._rows.length)
234
+ if (fromIndex < 0 || toIndex < 0)
236
235
  return false;
237
236
  const [movedRow] = this._rows.splice(fromIndex, 1);
238
237
  this._rows.splice(toIndex, 0, movedRow);
@@ -258,5 +257,5 @@ class Store {
258
257
  }
259
258
  }
260
259
 
261
- export { Store as default };
262
- //# sourceMappingURL=Store.mjs.map
260
+ module.exports = Store;
261
+ //# sourceMappingURL=Store.cjs.map
package/Store.cjs.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Store.cjs","sources":["../src/Store.ts"],"sourcesContent":["\"use client\"\r\nimport { useEffect, useId, useState } from \"react\"\r\nimport { MakeMetaType, MakeRowType, MetaSchema, RowSchema, WhereType } from \"./types\"\r\nimport { Infer, xv } from \"xanv\"\r\n\r\nconst uid = useId as any\r\nconst ustate = useState as any\r\nconst ueffect = useEffect as any\r\n\r\n\r\nclass Store<RS extends RowSchema, MS extends MetaSchema | undefined = undefined> {\r\n private _rows: MakeRowType<RS>[] = []\r\n private _meta: Map<keyof MakeMetaType<MS>, MakeMetaType<MS>[keyof MakeMetaType<MS>]> = new Map()\r\n private _hooks: Map<string, Function> = new Map()\r\n private _timer: any = null\r\n private _row_schema: RS\r\n private _meta_schema?: MS\r\n private _last_id = 0\r\n\r\n constructor(rowSchema: RS, metaSchema?: MS) {\r\n this._row_schema = {\r\n ...rowSchema,\r\n rid: xv.number(),\r\n vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)),\r\n }\r\n this._meta_schema = metaSchema\r\n }\r\n\r\n private observe = () => {\r\n try {\r\n const hid = uid()\r\n const [, dispatch] = ustate(0)\r\n this._hooks.set(hid, () => dispatch(Math.random()))\r\n ueffect(() => () => {\r\n this._hooks.delete(hid)\r\n }, [])\r\n } catch (error) { }\r\n }\r\n\r\n private dispatch = () => {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n cb()\r\n } catch (_err) {\r\n this._hooks.delete(key)\r\n }\r\n })\r\n }, 0)\r\n }\r\n\r\n rows(observe = true) {\r\n observe && this.observe()\r\n return this._rows\r\n }\r\n\r\n metas(observe = true) {\r\n observe && this.observe()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, observe?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], observe?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], observe = true): MakeRowType<RS> | MakeRowType<RS>[] {\r\n if (Array.isArray(row)) {\r\n const rows: MakeRowType<RS>[] = []\r\n for (const r of row) {\r\n rows.push(this.create(r, false))\r\n }\r\n observe && this.dispatch()\r\n return rows\r\n }\r\n\r\n // validate and create row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in this._row_schema) {\r\n if (key === \"rid\" || key === \"vid\") continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n this._last_id = this._last_id + 1;\r\n const _row: MakeRowType<RS> = {\r\n ...r,\r\n rid: this._last_id,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n\r\n this._rows.push(_row)\r\n observe && this.dispatch()\r\n return _row\r\n }\r\n\r\n\r\n // update\r\n update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] | null {\r\n\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in row) {\r\n if (key === \"rid\" || key === 'vid') continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n const rows = this.find(where)\r\n if (rows.length > 0) {\r\n for (let index = 0; index < rows.length; index++) {\r\n const _row = rows[index];\r\n const rid = _row.rid\r\n\r\n rows[index] = {\r\n ..._row,\r\n ...r,\r\n rid,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n const rowIndex = this._rows.findIndex(r => r.rid === rid)\r\n this._rows[rowIndex] = rows[index]\r\n }\r\n observe && this.dispatch()\r\n }\r\n return this.find(where)\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, observe = true): number {\r\n const rows = this.find(where, false)\r\n\r\n let deletedCount = 0\r\n if (rows.length > 0) {\r\n this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid))\r\n deletedCount = rows.length\r\n observe && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] {\r\n observe && this.observe()\r\n\r\n if (!where) {\r\n return this._rows\r\n }\r\n const rows: MakeRowType<RS>[] = []\r\n\r\n for (const row of this._rows) {\r\n const match = Object.keys(row).find(column => {\r\n let _match = true\r\n const rvalue = row[column]\r\n\r\n for (let wcol in where) {\r\n const wv = where[wcol]\r\n if (typeof wv === \"object\" && wv !== null) {\r\n\r\n if (wv.contain !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.contain === \"string\") {\r\n if (!rvalue.includes(wv.contain)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n\r\n if (wv.startWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.startWith === \"string\") {\r\n if (!rvalue.startsWith(wv.startWith)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.endWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.endWith === \"string\") {\r\n if (!rvalue.endsWith(wv.endWith)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.equalWith !== undefined) {\r\n if (rvalue !== wv.equalWith) {\r\n _match = false\r\n }\r\n }\r\n if (wv.notEqualWith !== undefined) {\r\n if (rvalue === wv.notEqualWith) {\r\n _match = false\r\n }\r\n }\r\n if (wv.gt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue > wv.gt)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.lt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue < wv.lt)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.gte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue >= wv.gte)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.lte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue <= wv.lte)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n continue\r\n }\r\n\r\n if (wv !== rvalue) {\r\n _match = false\r\n break\r\n }\r\n }\r\n return _match\r\n })\r\n\r\n if (match) {\r\n rows.push(row)\r\n }\r\n }\r\n return rows\r\n }\r\n\r\n findOne(where: WhereType<RS>, observe = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, observe)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(rid: string, observe = true): MakeRowType<RS> | null {\r\n return this.findOne({ rid }, observe)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, observe = true): number {\r\n observe && this.observe()\r\n const row = this.findOne(where, false)\r\n if (row) {\r\n return this._rows.findIndex(r => r.rid === row.rid)\r\n }\r\n return -1\r\n }\r\n\r\n move(fromIndex: number, toIndex: number, observe = true): boolean {\r\n if (fromIndex < 0 || toIndex < 0) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n observe && this.dispatch()\r\n return true\r\n }\r\n\r\n // Meta Methods\r\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], observe = true) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema[key] as any).parse(value) : value)\r\n observe && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, observe = true): Infer<MS>[T] | undefined {\r\n observe && this.observe()\r\n return this._meta.get(key) as Infer<MS>[T] | undefined\r\n }\r\n\r\n deleteMeta<T extends keyof Infer<MS>>(key: T, observe = true) {\r\n this._meta.delete(key)\r\n observe && this.dispatch()\r\n }\r\n\r\n clearMeta(observe = true) {\r\n this._meta.clear()\r\n observe && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;;;AAKA;AACA;AACA;AAGA;;;AAEW;AACA;;;;;AAiBF;;AAEA;AACA;AACG;;AAEL;;AACJ;;AAGG;AACA;;;AAGS;AACF;AAAC;AACC;AACF;AACJ;;AAEN;;AAzBG;;;AA4BA;;;;AAKA;;;AAOH;AACG;;AAEG;AACG;AACF;AACD;AACA;AACF;;;AAID;AACG;;;AAEA;AACF;;;AASD;AACA;AACA;;;AAKH;;;AAIG;AACG;;;AAEA;AACF;;AAGD;AACG;AACG;AACA;;AAQA;;AAEF;AACD;AACF;AACD;;;AAIH;;;AAIG;AACG;AACA;AACA;AACF;AACD;;;AAIH;AACG;;;AAIC;;AAGD;AACG;;AAEG;AAEA;AACG;;AAGG;;;;AAIO;AACH;AAAM;;AAEN;AACH;AAED;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;;AAEH;;;;AAKA;AACH;AACD;AACH;AAEA;AACG;AACF;AACH;AACD;;AAGH;;AAEG;;AAGH;;;AAIA;AACG;;AAEA;AACG;AACF;;;AAIJ;AACG;AAAkC;AAClC;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
package/Store.js CHANGED
@@ -1,12 +1,10 @@
1
1
  "use client";
2
- 'use strict';
2
+ import { useId, useState, useEffect } from 'react';
3
+ import { xv } from 'xanv';
3
4
 
4
- var react = require('react');
5
- var xanv = require('xanv');
6
-
7
- const uid = react.useId;
8
- const ustate = react.useState;
9
- const ueffect = react.useEffect;
5
+ const uid = useId;
6
+ const ustate = useState;
7
+ const ueffect = useEffect;
10
8
  class Store {
11
9
  constructor(rowSchema, metaSchema) {
12
10
  this._rows = [];
@@ -38,7 +36,7 @@ class Store {
38
36
  });
39
37
  }, 0);
40
38
  };
41
- this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xanv.xv.number(), vid: xanv.xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
39
+ this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xv.number(), vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
42
40
  this._meta_schema = metaSchema;
43
41
  }
44
42
  rows(observe = true) {
@@ -113,105 +111,104 @@ class Store {
113
111
  return this._rows;
114
112
  }
115
113
  const rows = [];
116
- for (let key in where) {
117
- const wv = where[key];
118
- if (typeof wv === "object" && wv !== null) {
119
- // QueryValueType
120
- for (let row of this._rows) {
121
- let match = true;
122
- const rvalue = row[key];
123
- if (wv.contain !== undefined) {
124
- if (typeof rvalue === "string" && typeof wv.contain === "string") {
125
- if (!rvalue.includes(wv.contain)) {
126
- match = false;
114
+ for (const row of this._rows) {
115
+ const match = Object.keys(row).find(column => {
116
+ let _match = true;
117
+ const rvalue = row[column];
118
+ for (let wcol in where) {
119
+ const wv = where[wcol];
120
+ if (typeof wv === "object" && wv !== null) {
121
+ if (wv.contain !== undefined) {
122
+ if (typeof rvalue === "string" && typeof wv.contain === "string") {
123
+ if (!rvalue.includes(wv.contain)) {
124
+ _match = false;
125
+ }
127
126
  }
128
- }
129
- else {
130
- match = false;
131
- }
132
- }
133
- if (wv.startWith !== undefined) {
134
- if (typeof rvalue === "string" && typeof wv.startWith === "string") {
135
- if (!rvalue.startsWith(wv.startWith)) {
136
- match = false;
127
+ else {
128
+ _match = false;
137
129
  }
138
130
  }
139
- else {
140
- match = false;
141
- }
142
- }
143
- if (wv.endWith !== undefined) {
144
- if (typeof rvalue === "string" && typeof wv.endWith === "string") {
145
- if (!rvalue.endsWith(wv.endWith)) {
146
- match = false;
131
+ if (wv.startWith !== undefined) {
132
+ if (typeof rvalue === "string" && typeof wv.startWith === "string") {
133
+ if (!rvalue.startsWith(wv.startWith)) {
134
+ _match = false;
135
+ }
147
136
  }
148
- }
149
- else {
150
- match = false;
151
- }
152
- }
153
- if (wv.equalWith !== undefined) {
154
- if (rvalue !== wv.equalWith) {
155
- match = false;
156
- }
157
- }
158
- if (wv.notEqualWith !== undefined) {
159
- if (rvalue === wv.notEqualWith) {
160
- match = false;
161
- }
162
- }
163
- if (wv.gt !== undefined) {
164
- if (typeof rvalue === "number") {
165
- if (!(rvalue > wv.gt)) {
166
- match = false;
137
+ else {
138
+ _match = false;
167
139
  }
168
140
  }
169
- else {
170
- match = false;
141
+ if (wv.endWith !== undefined) {
142
+ if (typeof rvalue === "string" && typeof wv.endWith === "string") {
143
+ if (!rvalue.endsWith(wv.endWith)) {
144
+ _match = false;
145
+ }
146
+ }
147
+ else {
148
+ _match = false;
149
+ }
171
150
  }
172
- }
173
- if (wv.lt !== undefined) {
174
- if (typeof rvalue === "number") {
175
- if (!(rvalue < wv.lt)) {
176
- match = false;
151
+ if (wv.equalWith !== undefined) {
152
+ if (rvalue !== wv.equalWith) {
153
+ _match = false;
177
154
  }
178
155
  }
179
- else {
180
- match = false;
156
+ if (wv.notEqualWith !== undefined) {
157
+ if (rvalue === wv.notEqualWith) {
158
+ _match = false;
159
+ }
181
160
  }
182
- }
183
- if (wv.gte !== undefined) {
184
- if (typeof rvalue === "number") {
185
- if (!(rvalue >= wv.gte)) {
186
- match = false;
161
+ if (wv.gt !== undefined) {
162
+ if (typeof rvalue === "number") {
163
+ if (!(rvalue > wv.gt)) {
164
+ _match = false;
165
+ }
166
+ }
167
+ else {
168
+ _match = false;
187
169
  }
188
170
  }
189
- else {
190
- match = false;
171
+ if (wv.lt !== undefined) {
172
+ if (typeof rvalue === "number") {
173
+ if (!(rvalue < wv.lt)) {
174
+ _match = false;
175
+ }
176
+ }
177
+ else {
178
+ _match = false;
179
+ }
191
180
  }
192
- }
193
- if (wv.lte !== undefined) {
194
- if (typeof rvalue === "number") {
195
- if (!(rvalue <= wv.lte)) {
196
- match = false;
181
+ if (wv.gte !== undefined) {
182
+ if (typeof rvalue === "number") {
183
+ if (!(rvalue >= wv.gte)) {
184
+ _match = false;
185
+ }
186
+ }
187
+ else {
188
+ _match = false;
197
189
  }
198
190
  }
199
- else {
200
- match = false;
191
+ if (wv.lte !== undefined) {
192
+ if (typeof rvalue === "number") {
193
+ if (!(rvalue <= wv.lte)) {
194
+ _match = false;
195
+ }
196
+ }
197
+ else {
198
+ _match = false;
199
+ }
201
200
  }
201
+ continue;
202
202
  }
203
- if (match) {
204
- rows.push(row);
205
- }
206
- }
207
- }
208
- else {
209
- // RowvType
210
- for (let row of this._rows) {
211
- if (row[key] === wv) {
212
- rows.push(row);
203
+ if (wv !== rvalue) {
204
+ _match = false;
205
+ break;
213
206
  }
214
207
  }
208
+ return _match;
209
+ });
210
+ if (match) {
211
+ rows.push(row);
215
212
  }
216
213
  }
217
214
  return rows;
@@ -232,9 +229,7 @@ class Store {
232
229
  return -1;
233
230
  }
234
231
  move(fromIndex, toIndex, observe = true) {
235
- if (fromIndex < 0 || fromIndex >= this._rows.length)
236
- return false;
237
- if (toIndex < 0 || toIndex >= this._rows.length)
232
+ if (fromIndex < 0 || toIndex < 0)
238
233
  return false;
239
234
  const [movedRow] = this._rows.splice(fromIndex, 1);
240
235
  this._rows.splice(toIndex, 0, movedRow);
@@ -260,5 +255,5 @@ class Store {
260
255
  }
261
256
  }
262
257
 
263
- module.exports = Store;
258
+ export { Store as default };
264
259
  //# sourceMappingURL=Store.js.map
package/Store.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Store.js","sources":["../src/Store.ts"],"sourcesContent":["\"use client\"\r\nimport { useEffect, useId, useState } from \"react\"\r\nimport { MakeMetaType, MakeRowType, MetaSchema, RowSchema, WhereType } from \"./types\"\r\nimport { Infer, xv } from \"xanv\"\r\n\r\nconst uid = useId as any\r\nconst ustate = useState as any\r\nconst ueffect = useEffect as any\r\n\r\n\r\nclass Store<RS extends RowSchema, MS extends MetaSchema | undefined = undefined> {\r\n private _rows: MakeRowType<RS>[] = []\r\n private _meta: Map<keyof MakeMetaType<MS>, MakeMetaType<MS>[keyof MakeMetaType<MS>]> = new Map()\r\n private _hooks: Map<string, Function> = new Map()\r\n private _timer: any = null\r\n private _row_schema: RS\r\n private _meta_schema?: MS\r\n private _last_id = 0\r\n\r\n constructor(rowSchema: RS, metaSchema?: MS) {\r\n this._row_schema = {\r\n ...rowSchema,\r\n rid: xv.number(),\r\n vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)),\r\n }\r\n this._meta_schema = metaSchema\r\n }\r\n\r\n private observe = () => {\r\n try {\r\n const hid = uid()\r\n const [, dispatch] = ustate(0)\r\n this._hooks.set(hid, () => dispatch(Math.random()))\r\n ueffect(() => () => {\r\n this._hooks.delete(hid)\r\n }, [])\r\n } catch (error) { }\r\n }\r\n\r\n private dispatch = () => {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n cb()\r\n } catch (_err) {\r\n this._hooks.delete(key)\r\n }\r\n })\r\n }, 0)\r\n }\r\n\r\n rows(observe = true) {\r\n observe && this.observe()\r\n return this._rows\r\n }\r\n\r\n metas(observe = true) {\r\n observe && this.observe()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, observe?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], observe?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], observe = true): MakeRowType<RS> | MakeRowType<RS>[] {\r\n if (Array.isArray(row)) {\r\n const rows: MakeRowType<RS>[] = []\r\n for (const r of row) {\r\n rows.push(this.create(r, false))\r\n }\r\n observe && this.dispatch()\r\n return rows\r\n }\r\n\r\n // validate and create row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in this._row_schema) {\r\n if (key === \"rid\" || key === \"vid\") continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n this._last_id = this._last_id + 1;\r\n const _row: MakeRowType<RS> = {\r\n ...r,\r\n rid: this._last_id,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n\r\n this._rows.push(_row)\r\n observe && this.dispatch()\r\n return _row\r\n }\r\n\r\n\r\n // update\r\n update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] | null {\r\n\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in row) {\r\n if (key === \"rid\" || key === 'vid') continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n const rows = this.find(where)\r\n if (rows.length > 0) {\r\n for (let index = 0; index < rows.length; index++) {\r\n const _row = rows[index];\r\n const rid = _row.rid\r\n\r\n rows[index] = {\r\n ..._row,\r\n ...r,\r\n rid,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n const rowIndex = this._rows.findIndex(r => r.rid === rid)\r\n this._rows[rowIndex] = rows[index]\r\n }\r\n observe && this.dispatch()\r\n }\r\n return this.find(where)\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, observe = true): number {\r\n const rows = this.find(where, false)\r\n\r\n let deletedCount = 0\r\n if (rows.length > 0) {\r\n this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid))\r\n deletedCount = rows.length\r\n observe && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] {\r\n observe && this.observe()\r\n\r\n if (!where) {\r\n return this._rows\r\n }\r\n\r\n const rows: MakeRowType<RS>[] = []\r\n for (let key in where) {\r\n const wv = (where as any)[key]\r\n if (typeof wv === \"object\" && wv !== null) {\r\n // QueryValueType\r\n for (let row of this._rows) {\r\n let match = true\r\n const rvalue = (row as any)[key]\r\n if (wv.contain !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.contain === \"string\") {\r\n if (!rvalue.includes(wv.contain)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.startWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.startWith === \"string\") {\r\n if (!rvalue.startsWith(wv.startWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.endWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.endWith === \"string\") {\r\n if (!rvalue.endsWith(wv.endWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.equalWith !== undefined) {\r\n if (rvalue !== wv.equalWith) {\r\n match = false\r\n }\r\n }\r\n if (wv.notEqualWith !== undefined) {\r\n if (rvalue === wv.notEqualWith) {\r\n match = false\r\n }\r\n }\r\n if (wv.gt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue > wv.gt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.lt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue < wv.lt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.gte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue >= wv.gte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.lte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue <= wv.lte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (match) {\r\n rows.push(row)\r\n }\r\n }\r\n } else {\r\n // RowvType\r\n for (let row of this._rows) {\r\n if ((row as any)[key] === wv) {\r\n rows.push(row)\r\n }\r\n }\r\n }\r\n }\r\n return rows\r\n }\r\n\r\n findOne(where: WhereType<RS>, observe = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, observe)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(rid: string, observe = true): MakeRowType<RS> | null {\r\n return this.findOne({ rid }, observe)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, observe = true): number {\r\n observe && this.observe()\r\n const row = this.findOne(where, false)\r\n if (row) {\r\n return this._rows.findIndex(r => r.rid === row.rid)\r\n }\r\n return -1\r\n }\r\n\r\n move(fromIndex: number, toIndex: number, observe = true): boolean {\r\n if (fromIndex < 0 || fromIndex >= this._rows.length) return false\r\n if (toIndex < 0 || toIndex >= this._rows.length) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n observe && this.dispatch()\r\n return true\r\n }\r\n\r\n // Meta Methods\r\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], observe = true) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema[key] as any).parse(value) : value)\r\n observe && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, observe = true): Infer<MS>[T] | undefined {\r\n observe && this.observe()\r\n return this._meta.get(key) as Infer<MS>[T] | undefined\r\n }\r\n\r\n deleteMeta<T extends keyof Infer<MS>>(key: T, observe = true) {\r\n this._meta.delete(key)\r\n observe && this.dispatch()\r\n }\r\n\r\n clearMeta(observe = true) {\r\n this._meta.clear()\r\n observe && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;;;AAKA;AACA;AACA;AAGA;;;AAEW;AACA;;;;;AAiBF;;AAEA;AACA;AACG;;AAEL;;AACJ;;AAGG;AACA;;;AAGS;AACF;AAAC;AACC;AACF;AACJ;;AAEN;;AAzBG;;;AA4BA;;;;AAKA;;;AAOH;AACG;;AAEG;AACG;AACF;AACD;AACA;AACF;;;AAID;AACG;;;AAEA;AACF;;;AASD;AACA;AACA;;;AAKH;;;AAIG;AACG;;;AAEA;AACF;;AAGD;AACG;AACG;AACA;;AAQA;;AAEF;AACD;AACF;AACD;;;AAIH;;;AAIG;AACG;AACA;AACA;AACF;AACD;;;AAIH;AACG;;;AAIC;;AAGD;AACG;;;AAGG;;AAEG;AACA;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;AACF;AACH;AACH;AAAM;;AAEJ;AACG;AACG;AACF;AACH;AACH;AACH;AACD;;AAGH;;AAEG;;AAGH;;;AAIA;AACG;;AAEA;AACG;AACF;;;AAIJ;;AACwD;;AACJ;AACjD;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
1
+ {"version":3,"file":"Store.js","sources":["../src/Store.ts"],"sourcesContent":["\"use client\"\r\nimport { useEffect, useId, useState } from \"react\"\r\nimport { MakeMetaType, MakeRowType, MetaSchema, RowSchema, WhereType } from \"./types\"\r\nimport { Infer, xv } from \"xanv\"\r\n\r\nconst uid = useId as any\r\nconst ustate = useState as any\r\nconst ueffect = useEffect as any\r\n\r\n\r\nclass Store<RS extends RowSchema, MS extends MetaSchema | undefined = undefined> {\r\n private _rows: MakeRowType<RS>[] = []\r\n private _meta: Map<keyof MakeMetaType<MS>, MakeMetaType<MS>[keyof MakeMetaType<MS>]> = new Map()\r\n private _hooks: Map<string, Function> = new Map()\r\n private _timer: any = null\r\n private _row_schema: RS\r\n private _meta_schema?: MS\r\n private _last_id = 0\r\n\r\n constructor(rowSchema: RS, metaSchema?: MS) {\r\n this._row_schema = {\r\n ...rowSchema,\r\n rid: xv.number(),\r\n vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)),\r\n }\r\n this._meta_schema = metaSchema\r\n }\r\n\r\n private observe = () => {\r\n try {\r\n const hid = uid()\r\n const [, dispatch] = ustate(0)\r\n this._hooks.set(hid, () => dispatch(Math.random()))\r\n ueffect(() => () => {\r\n this._hooks.delete(hid)\r\n }, [])\r\n } catch (error) { }\r\n }\r\n\r\n private dispatch = () => {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n cb()\r\n } catch (_err) {\r\n this._hooks.delete(key)\r\n }\r\n })\r\n }, 0)\r\n }\r\n\r\n rows(observe = true) {\r\n observe && this.observe()\r\n return this._rows\r\n }\r\n\r\n metas(observe = true) {\r\n observe && this.observe()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, observe?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], observe?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], observe = true): MakeRowType<RS> | MakeRowType<RS>[] {\r\n if (Array.isArray(row)) {\r\n const rows: MakeRowType<RS>[] = []\r\n for (const r of row) {\r\n rows.push(this.create(r, false))\r\n }\r\n observe && this.dispatch()\r\n return rows\r\n }\r\n\r\n // validate and create row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in this._row_schema) {\r\n if (key === \"rid\" || key === \"vid\") continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n this._last_id = this._last_id + 1;\r\n const _row: MakeRowType<RS> = {\r\n ...r,\r\n rid: this._last_id,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n\r\n this._rows.push(_row)\r\n observe && this.dispatch()\r\n return _row\r\n }\r\n\r\n\r\n // update\r\n update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] | null {\r\n\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in row) {\r\n if (key === \"rid\" || key === 'vid') continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n const rows = this.find(where)\r\n if (rows.length > 0) {\r\n for (let index = 0; index < rows.length; index++) {\r\n const _row = rows[index];\r\n const rid = _row.rid\r\n\r\n rows[index] = {\r\n ..._row,\r\n ...r,\r\n rid,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n const rowIndex = this._rows.findIndex(r => r.rid === rid)\r\n this._rows[rowIndex] = rows[index]\r\n }\r\n observe && this.dispatch()\r\n }\r\n return this.find(where)\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, observe = true): number {\r\n const rows = this.find(where, false)\r\n\r\n let deletedCount = 0\r\n if (rows.length > 0) {\r\n this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid))\r\n deletedCount = rows.length\r\n observe && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] {\r\n observe && this.observe()\r\n\r\n if (!where) {\r\n return this._rows\r\n }\r\n const rows: MakeRowType<RS>[] = []\r\n\r\n for (const row of this._rows) {\r\n const match = Object.keys(row).find(column => {\r\n let _match = true\r\n const rvalue = row[column]\r\n\r\n for (let wcol in where) {\r\n const wv = where[wcol]\r\n if (typeof wv === \"object\" && wv !== null) {\r\n\r\n if (wv.contain !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.contain === \"string\") {\r\n if (!rvalue.includes(wv.contain)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n\r\n if (wv.startWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.startWith === \"string\") {\r\n if (!rvalue.startsWith(wv.startWith)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.endWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.endWith === \"string\") {\r\n if (!rvalue.endsWith(wv.endWith)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.equalWith !== undefined) {\r\n if (rvalue !== wv.equalWith) {\r\n _match = false\r\n }\r\n }\r\n if (wv.notEqualWith !== undefined) {\r\n if (rvalue === wv.notEqualWith) {\r\n _match = false\r\n }\r\n }\r\n if (wv.gt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue > wv.gt)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.lt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue < wv.lt)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.gte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue >= wv.gte)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.lte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue <= wv.lte)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n continue\r\n }\r\n\r\n if (wv !== rvalue) {\r\n _match = false\r\n break\r\n }\r\n }\r\n return _match\r\n })\r\n\r\n if (match) {\r\n rows.push(row)\r\n }\r\n }\r\n return rows\r\n }\r\n\r\n findOne(where: WhereType<RS>, observe = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, observe)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(rid: string, observe = true): MakeRowType<RS> | null {\r\n return this.findOne({ rid }, observe)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, observe = true): number {\r\n observe && this.observe()\r\n const row = this.findOne(where, false)\r\n if (row) {\r\n return this._rows.findIndex(r => r.rid === row.rid)\r\n }\r\n return -1\r\n }\r\n\r\n move(fromIndex: number, toIndex: number, observe = true): boolean {\r\n if (fromIndex < 0 || toIndex < 0) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n observe && this.dispatch()\r\n return true\r\n }\r\n\r\n // Meta Methods\r\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], observe = true) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema[key] as any).parse(value) : value)\r\n observe && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, observe = true): Infer<MS>[T] | undefined {\r\n observe && this.observe()\r\n return this._meta.get(key) as Infer<MS>[T] | undefined\r\n }\r\n\r\n deleteMeta<T extends keyof Infer<MS>>(key: T, observe = true) {\r\n this._meta.delete(key)\r\n observe && this.dispatch()\r\n }\r\n\r\n clearMeta(observe = true) {\r\n this._meta.clear()\r\n observe && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;AAKA;AACA;AACA;AAGA;;;AAEW;AACA;;;;;AAiBF;;AAEA;AACA;AACG;;AAEL;;AACJ;;AAGG;AACA;;;AAGS;AACF;AAAC;AACC;AACF;AACJ;;AAEN;;AAzBG;;;AA4BA;;;;AAKA;;;AAOH;AACG;;AAEG;AACG;AACF;AACD;AACA;AACF;;;AAID;AACG;;;AAEA;AACF;;;AASD;AACA;AACA;;;AAKH;;;AAIG;AACG;;;AAEA;AACF;;AAGD;AACG;AACG;AACA;;AAQA;;AAEF;AACD;AACF;AACD;;;AAIH;;;AAIG;AACG;AACA;AACA;AACF;AACD;;;AAIH;AACG;;;AAIC;;AAGD;AACG;;AAEG;AAEA;AACG;;AAGG;;;;AAIO;AACH;AAAM;;AAEN;AACH;AAED;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;;AAEH;;;;AAKA;AACH;AACD;AACH;AAEA;AACG;AACF;AACH;AACD;;AAGH;;AAEG;;AAGH;;;AAIA;AACG;;AAEA;AACG;AACF;;;AAIJ;AACG;AAAkC;AAClC;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
package/index.cjs ADDED
@@ -0,0 +1,14 @@
1
+ "use client";
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var Store = require('./Store.cjs');
7
+
8
+ const createStore = (rowSchema, metaSchema) => {
9
+ return new Store(rowSchema, metaSchema);
10
+ };
11
+
12
+ exports.Store = Store;
13
+ exports.default = createStore;
14
+ //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["\"use client\"\r\nimport Store from \"./Store\"\r\nimport { MetaSchema, RowSchema } from \"./types\"\r\n\r\nconst createStore = <RS extends RowSchema, MS extends MetaSchema | undefined = undefined>(rowSchema: RS, metaSchema?: MS) => {\r\n return new Store(rowSchema, metaSchema)\r\n}\r\n\r\nexport * from \"./types\"\r\nexport { Store }\r\n\r\nexport default createStore"],"names":[],"mappings":";;;AAIA;AACI;AACJ;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["\"use client\"\r\nimport Store from \"./Store\"\r\nimport { MetaSchema, RowSchema } from \"./types\"\r\n\r\nconst createStore = <RS extends RowSchema, MS extends MetaSchema | undefined = undefined>(rowSchema: RS, metaSchema?: MS) => {\r\n return new Store(rowSchema, metaSchema)\r\n}\r\n\r\nexport * from \"./types\"\r\nexport { Store }\r\n\r\nexport default createStore"],"names":[],"mappings":";;;;;;;AAIA;AACI;AACJ;;;"}
package/index.js CHANGED
@@ -1,14 +1,9 @@
1
1
  "use client";
2
- 'use strict';
3
-
4
- Object.defineProperty(exports, '__esModule', { value: true });
5
-
6
- var Store = require('./Store.js');
2
+ import Store from './Store.js';
7
3
 
8
4
  const createStore = (rowSchema, metaSchema) => {
9
5
  return new Store(rowSchema, metaSchema);
10
6
  };
11
7
 
12
- exports.Store = Store;
13
- exports.default = createStore;
8
+ export { Store, createStore as default };
14
9
  //# sourceMappingURL=index.js.map
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["\"use client\"\r\nimport Store from \"./Store\"\r\nimport { MetaSchema, RowSchema } from \"./types\"\r\n\r\nconst createStore = <RS extends RowSchema, MS extends MetaSchema | undefined = undefined>(rowSchema: RS, metaSchema?: MS) => {\r\n return new Store(rowSchema, metaSchema)\r\n}\r\n\r\nexport * from \"./types\"\r\nexport { Store }\r\n\r\nexport default createStore"],"names":[],"mappings":";;;;;;;AAIA;AACI;AACJ;;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["\"use client\"\r\nimport Store from \"./Store\"\r\nimport { MetaSchema, RowSchema } from \"./types\"\r\n\r\nconst createStore = <RS extends RowSchema, MS extends MetaSchema | undefined = undefined>(rowSchema: RS, metaSchema?: MS) => {\r\n return new Store(rowSchema, metaSchema)\r\n}\r\n\r\nexport * from \"./types\"\r\nexport { Store }\r\n\r\nexport default createStore"],"names":[],"mappings":";;;AAIA;AACI;AACJ;;"}
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "react-rock",
3
- "version": "3.2.6",
3
+ "version": "3.2.8",
4
4
  "author": "Naxrul Ahmed",
5
5
  "description": "React-Rock is a modern, lightweight state management library designed to simplify handling global state in React applications. With a minimal API and powerful features like freezing data updates for optimized re-renders, React-Rock allows you to manage state more efficiently while ensuring your app remains fast and responsive.",
6
- "main": "./index.js",
7
- "module": "./index.mjs",
6
+ "main": "./index.cjs",
7
+ "module": "./index.js",
8
8
  "types": "./index.d.ts",
9
+ "type": "module",
9
10
  "keywords": [
10
11
  "react",
11
12
  "state management",
@@ -17,13 +18,6 @@
17
18
  "url": "https://github.com/devnax/react-rock/issues"
18
19
  },
19
20
  "homepage": "https://github.com/devnax/react-rock#readme",
20
- "exports": {
21
- ".": {
22
- "import": "./index.mjs",
23
- "require": "./index.js",
24
- "types": "./index.d.ts"
25
- }
26
- },
27
21
  "dependencies": {
28
22
  "xanv": "^1.1.11"
29
23
  }
package/Store.mjs.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"Store.mjs","sources":["../src/Store.ts"],"sourcesContent":["\"use client\"\r\nimport { useEffect, useId, useState } from \"react\"\r\nimport { MakeMetaType, MakeRowType, MetaSchema, RowSchema, WhereType } from \"./types\"\r\nimport { Infer, xv } from \"xanv\"\r\n\r\nconst uid = useId as any\r\nconst ustate = useState as any\r\nconst ueffect = useEffect as any\r\n\r\n\r\nclass Store<RS extends RowSchema, MS extends MetaSchema | undefined = undefined> {\r\n private _rows: MakeRowType<RS>[] = []\r\n private _meta: Map<keyof MakeMetaType<MS>, MakeMetaType<MS>[keyof MakeMetaType<MS>]> = new Map()\r\n private _hooks: Map<string, Function> = new Map()\r\n private _timer: any = null\r\n private _row_schema: RS\r\n private _meta_schema?: MS\r\n private _last_id = 0\r\n\r\n constructor(rowSchema: RS, metaSchema?: MS) {\r\n this._row_schema = {\r\n ...rowSchema,\r\n rid: xv.number(),\r\n vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)),\r\n }\r\n this._meta_schema = metaSchema\r\n }\r\n\r\n private observe = () => {\r\n try {\r\n const hid = uid()\r\n const [, dispatch] = ustate(0)\r\n this._hooks.set(hid, () => dispatch(Math.random()))\r\n ueffect(() => () => {\r\n this._hooks.delete(hid)\r\n }, [])\r\n } catch (error) { }\r\n }\r\n\r\n private dispatch = () => {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n cb()\r\n } catch (_err) {\r\n this._hooks.delete(key)\r\n }\r\n })\r\n }, 0)\r\n }\r\n\r\n rows(observe = true) {\r\n observe && this.observe()\r\n return this._rows\r\n }\r\n\r\n metas(observe = true) {\r\n observe && this.observe()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, observe?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], observe?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], observe = true): MakeRowType<RS> | MakeRowType<RS>[] {\r\n if (Array.isArray(row)) {\r\n const rows: MakeRowType<RS>[] = []\r\n for (const r of row) {\r\n rows.push(this.create(r, false))\r\n }\r\n observe && this.dispatch()\r\n return rows\r\n }\r\n\r\n // validate and create row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in this._row_schema) {\r\n if (key === \"rid\" || key === \"vid\") continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n this._last_id = this._last_id + 1;\r\n const _row: MakeRowType<RS> = {\r\n ...r,\r\n rid: this._last_id,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n\r\n this._rows.push(_row)\r\n observe && this.dispatch()\r\n return _row\r\n }\r\n\r\n\r\n // update\r\n update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] | null {\r\n\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in row) {\r\n if (key === \"rid\" || key === 'vid') continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n const rows = this.find(where)\r\n if (rows.length > 0) {\r\n for (let index = 0; index < rows.length; index++) {\r\n const _row = rows[index];\r\n const rid = _row.rid\r\n\r\n rows[index] = {\r\n ..._row,\r\n ...r,\r\n rid,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n const rowIndex = this._rows.findIndex(r => r.rid === rid)\r\n this._rows[rowIndex] = rows[index]\r\n }\r\n observe && this.dispatch()\r\n }\r\n return this.find(where)\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, observe = true): number {\r\n const rows = this.find(where, false)\r\n\r\n let deletedCount = 0\r\n if (rows.length > 0) {\r\n this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid))\r\n deletedCount = rows.length\r\n observe && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] {\r\n observe && this.observe()\r\n\r\n if (!where) {\r\n return this._rows\r\n }\r\n\r\n const rows: MakeRowType<RS>[] = []\r\n for (let key in where) {\r\n const wv = (where as any)[key]\r\n if (typeof wv === \"object\" && wv !== null) {\r\n // QueryValueType\r\n for (let row of this._rows) {\r\n let match = true\r\n const rvalue = (row as any)[key]\r\n if (wv.contain !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.contain === \"string\") {\r\n if (!rvalue.includes(wv.contain)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.startWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.startWith === \"string\") {\r\n if (!rvalue.startsWith(wv.startWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.endWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.endWith === \"string\") {\r\n if (!rvalue.endsWith(wv.endWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.equalWith !== undefined) {\r\n if (rvalue !== wv.equalWith) {\r\n match = false\r\n }\r\n }\r\n if (wv.notEqualWith !== undefined) {\r\n if (rvalue === wv.notEqualWith) {\r\n match = false\r\n }\r\n }\r\n if (wv.gt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue > wv.gt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.lt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue < wv.lt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.gte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue >= wv.gte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.lte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue <= wv.lte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (match) {\r\n rows.push(row)\r\n }\r\n }\r\n } else {\r\n // RowvType\r\n for (let row of this._rows) {\r\n if ((row as any)[key] === wv) {\r\n rows.push(row)\r\n }\r\n }\r\n }\r\n }\r\n return rows\r\n }\r\n\r\n findOne(where: WhereType<RS>, observe = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, observe)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(rid: string, observe = true): MakeRowType<RS> | null {\r\n return this.findOne({ rid }, observe)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, observe = true): number {\r\n observe && this.observe()\r\n const row = this.findOne(where, false)\r\n if (row) {\r\n return this._rows.findIndex(r => r.rid === row.rid)\r\n }\r\n return -1\r\n }\r\n\r\n move(fromIndex: number, toIndex: number, observe = true): boolean {\r\n if (fromIndex < 0 || fromIndex >= this._rows.length) return false\r\n if (toIndex < 0 || toIndex >= this._rows.length) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n observe && this.dispatch()\r\n return true\r\n }\r\n\r\n // Meta Methods\r\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], observe = true) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema[key] as any).parse(value) : value)\r\n observe && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, observe = true): Infer<MS>[T] | undefined {\r\n observe && this.observe()\r\n return this._meta.get(key) as Infer<MS>[T] | undefined\r\n }\r\n\r\n deleteMeta<T extends keyof Infer<MS>>(key: T, observe = true) {\r\n this._meta.delete(key)\r\n observe && this.dispatch()\r\n }\r\n\r\n clearMeta(observe = true) {\r\n this._meta.clear()\r\n observe && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;AAKA;AACA;AACA;AAGA;;;AAEW;AACA;;;;;AAiBF;;AAEA;AACA;AACG;;AAEL;;AACJ;;AAGG;AACA;;;AAGS;AACF;AAAC;AACC;AACF;AACJ;;AAEN;;AAzBG;;;AA4BA;;;;AAKA;;;AAOH;AACG;;AAEG;AACG;AACF;AACD;AACA;AACF;;;AAID;AACG;;;AAEA;AACF;;;AASD;AACA;AACA;;;AAKH;;;AAIG;AACG;;;AAEA;AACF;;AAGD;AACG;AACG;AACA;;AAQA;;AAEF;AACD;AACF;AACD;;;AAIH;;;AAIG;AACG;AACA;AACA;AACF;AACD;;;AAIH;AACG;;;AAIC;;AAGD;AACG;;;AAGG;;AAEG;AACA;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;AACF;AACH;AACH;AAAM;;AAEJ;AACG;AACG;AACF;AACH;AACH;AACH;AACD;;AAGH;;AAEG;;AAGH;;;AAIA;AACG;;AAEA;AACG;AACF;;;AAIJ;;AACwD;;AACJ;AACjD;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
package/index.mjs DELETED
@@ -1,9 +0,0 @@
1
- "use client";
2
- import Store from './Store.mjs';
3
-
4
- const createStore = (rowSchema, metaSchema) => {
5
- return new Store(rowSchema, metaSchema);
6
- };
7
-
8
- export { Store, createStore as default };
9
- //# sourceMappingURL=index.mjs.map