miolo-model 3.0.0-beta.224 → 3.0.0-beta.226

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "miolo-model",
3
- "version": "3.0.0-beta.224",
3
+ "version": "3.0.0-beta.226",
4
4
  "description": "Data models for miolo world",
5
5
  "author": "Donato Lorenzo <donato@afialapis.com>",
6
6
  "contributors": [
@@ -27,6 +27,7 @@
27
27
  "reset": "rm -fr package-lock.json npm-lock.yaml node_modules && npm i",
28
28
  "lint": "biome check ./src --reporter=github",
29
29
  "lint:fix": "biome check --write ./src --reporter=github",
30
+ "lint:types": "tsc -p jsconfig.json --noEmit",
30
31
  "test": "node --test ./test/index.mjs",
31
32
  "prepublishOnly": "npm run lint && npm run test"
32
33
  },
@@ -1,28 +1,38 @@
1
- import CacheMixin from "./CacheMixin.mjs"
1
+ /**
2
+ * @typedef {import("./MioloModel.mjs").default} MioloModel
3
+ */
2
4
 
3
- export default class MioloArray extends CacheMixin(Array) {
5
+ /**
6
+ * Represents an array of MioloModel instances.
7
+ * Inherits all native JS Array methods.
8
+ *
9
+ * @template {MioloModel} T
10
+ * @extends {Array<T>}
11
+ */
12
+ export default class MioloArray extends Array {
4
13
  /**
5
14
  * Create a new MioloArray.
6
- * @param {Function} itemClass - The class of the items in the array.
7
- * @param {Array<MioloModel> | Array<Object>} items - The items to initialize the array with.
15
+ * @param {new (...args: any[]) => T} itemClass - The class of the items in the array.
16
+ * @param {Array<T> | Array<Object>} items - The items to initialize the array with.
8
17
  * @param {...any} extra - Extra parameters to pass to the item class constructor.
9
18
  */
10
19
  constructor(itemClass, items = [], ...extra) {
11
- // Arrays can be inited with a number
20
+ // Arrays can be inited with a number of elements or an array of items.
21
+ // TypeScript requires a single top-level super() call when the class has class fields.
22
+ super(typeof items === "number" ? items : 0)
23
+
24
+ this.reset_cache()
25
+
12
26
  if (typeof items === "number") {
13
- super(items)
14
- this.reset_cache()
15
27
  return
16
28
  }
17
- //
18
- super()
19
- this.reset_cache()
20
29
 
21
30
  // Check properties
22
31
  if (!itemClass) {
23
32
  console.error(`[${this.constructor.name}] MioloArray received no itemClass`)
24
33
  return
25
34
  }
35
+ /** @type {new (...args: any[]) => T} */
26
36
  this.itemClass = itemClass
27
37
 
28
38
  items.forEach((r, index) => {
@@ -31,9 +41,110 @@ export default class MioloArray extends CacheMixin(Array) {
31
41
  })
32
42
  }
33
43
 
44
+ /**
45
+ * Reset the model's cache.
46
+ * @public
47
+ */
48
+ reset_cache() {
49
+ this.__cache__ = {}
50
+ }
51
+
52
+ /**
53
+ * Reset the model's cache.
54
+ * @public
55
+ */
56
+ resetCache = this.reset_cache
57
+
58
+ /**
59
+ * Get a value from the model's cache.
60
+ * @param {string} key - The key to get the value from.
61
+ * @returns {any} The value from the cache.
62
+ * @public
63
+ */
64
+ get_from_cache(key) {
65
+ if (this.__cache__ === undefined) {
66
+ return undefined
67
+ }
68
+ return this.__cache__[key]
69
+ }
70
+
71
+ /**
72
+ * Get a value from the model's cache.
73
+ * @param {string} key - The key to get the value from.
74
+ * @returns {any} The value from the cache.
75
+ * @public
76
+ */
77
+ getFromCache = this.get_from_cache
78
+
79
+ /**
80
+ * Set a value in the model's cache.
81
+ * @param {string} key - The key to set the value to.
82
+ * @param {any} value - The value to set.
83
+ * @public
84
+ */
85
+ set_to_cache(key, value) {
86
+ if (this.__cache__ === undefined) {
87
+ this.__cache__ = {}
88
+ }
89
+ this.__cache__[key] = value
90
+ }
91
+
92
+ /**
93
+ * Set a value in the model's cache.
94
+ * @param {string} key - The key to set the value to.
95
+ * @param {any} value - The value to set.
96
+ * @public
97
+ */
98
+ setToCache = this.set_to_cache
99
+
100
+ /**
101
+ * Get a value from the model's cache or make it if it doesn't exist.
102
+ * @param {string} cache_key - The key to get the value from.
103
+ * @param {Function} make_callback - The function to make the value if it doesn't exist.
104
+ * @returns {any} The value from the cache.
105
+ * @public
106
+ */
107
+ get_from_cache_or_make(cache_key, make_callback) {
108
+ if (this.__cache__ === undefined) {
109
+ this.__cache__ = {}
110
+ }
111
+ if (this.__cache__[cache_key] === undefined) {
112
+ const bound_callback = make_callback.bind(this)
113
+ this.__cache__[cache_key] = bound_callback()
114
+ }
115
+ return this.__cache__[cache_key]
116
+ }
117
+
118
+ /**
119
+ * Get a value from the model's cache or make it if it doesn't exist.
120
+ * @param {string} cache_key - The key to get the value from.
121
+ * @param {Function} make_callback - The function to make the value if it doesn't exist.
122
+ * @returns {any} The value from the cache.
123
+ * @public
124
+ */
125
+ getFromCacheOrMake = this.get_from_cache_or_make
126
+
127
+ /**
128
+ * Invalidate a value in the model's cache.
129
+ * @param {string} cache_key - The key to invalidate.
130
+ * @public
131
+ */
132
+ invalidate_cache(cache_key) {
133
+ if (this.__cache__ !== undefined) {
134
+ delete this.__cache__[cache_key]
135
+ }
136
+ }
137
+
138
+ /**
139
+ * Invalidate a value in the model's cache.
140
+ * @param {string} cache_key - The key to invalidate.
141
+ * @public
142
+ */
143
+ invalidateCache = this.invalidate_cache
144
+
34
145
  /**
35
146
  * Returns the last item in the array.
36
- * @returns {MioloModel} The last item in the array.
147
+ * @returns {T} The last item in the array.
37
148
  * @public
38
149
  */
39
150
  last() {
@@ -67,14 +178,14 @@ export default class MioloArray extends CacheMixin(Array) {
67
178
  /**
68
179
  * Find the index of the first item in the array that has the given field and value.
69
180
  * @param {string} field - The field to search for.
70
- * @param {any} value - The value to search for.
181
+ * @param {MioloModel} value - The value to search for.
71
182
  * @returns {number} The index of the first item that has the given field and value, or -1 if not found.
72
183
  * @public
73
184
  */
74
185
  find_index_by_field(field, value) {
75
186
  if (this.length >= 0) {
76
187
  const fidx = this.findIndex((elem) => {
77
- const val = typeof elem._get === "function" ? elem._get(field) : elem[field]
188
+ const val = typeof elem.get_value === "function" ? elem.get_value(field) : elem[field]
78
189
  return val === value
79
190
  })
80
191
  if (fidx >= 0) {
@@ -99,13 +210,13 @@ export default class MioloArray extends CacheMixin(Array) {
99
210
  * Find the first item in the array that has the given field and value.
100
211
  * @param {string} field - The field to search for.
101
212
  * @param {any} value - The value to search for.
102
- * @returns {MioloModel} The first item in the array that has the given field and value, or undefined if not found.
213
+ * @returns {T | undefined} The first item in the array that has the given field and value, or undefined if not found.
103
214
  * @public
104
215
  */
105
216
  find_by_field(field, value) {
106
217
  if (this.length >= 0) {
107
218
  const filt = this.filter((elem) => {
108
- const val = typeof elem._get === "function" ? elem._get(field) : elem[field]
219
+ const val = typeof elem.get_value === "function" ? elem.get_value(field) : elem[field]
109
220
  return val === value
110
221
  })
111
222
  if (filt.length > 0) {
@@ -119,7 +230,7 @@ export default class MioloArray extends CacheMixin(Array) {
119
230
  * Find the first item in the array that has the given field and value.
120
231
  * @param {string} field - The field to search for.
121
232
  * @param {any} value - The value to search for.
122
- * @returns {MioloModel} The first item in the array that has the given field and value, or undefined if not found.
233
+ * @returns {T | undefined} The first item in the array that has the given field and value, or undefined if not found.
123
234
  * @public
124
235
  */
125
236
  findByField(field, value) {
@@ -129,7 +240,7 @@ export default class MioloArray extends CacheMixin(Array) {
129
240
  /**
130
241
  * Find the first item in the array that has the given id.
131
242
  * @param {string} id - The id to search for.
132
- * @returns {MioloModel} The first item in the array that has the given id, or undefined if not found.
243
+ * @returns {T | undefined} The first item in the array that has the given id, or undefined if not found.
133
244
  * @public
134
245
  */
135
246
  find_by_id(id) {
@@ -139,7 +250,7 @@ export default class MioloArray extends CacheMixin(Array) {
139
250
  /**
140
251
  * Find the first item in the array that has the given id.
141
252
  * @param {string} id - The id to search for.
142
- * @returns {MioloModel} The first item in the array that has the given id, or undefined if not found.
253
+ * @returns {T | undefined} The first item in the array that has the given id, or undefined if not found.
143
254
  * @public
144
255
  */
145
256
  findById(id) {
@@ -171,10 +282,12 @@ export default class MioloArray extends CacheMixin(Array) {
171
282
 
172
283
  /**
173
284
  * Push a new item to the end of the array.
174
- * @param {Object} data - The data to push to the array.
175
- * @returns {MioloModel | Object} The new item.
285
+ * @param {T} data - The data to push to the array.
286
+ * @returns {T} The new item.
176
287
  * @public
177
288
  */
289
+ // biome-ignore lint/suspicious/noTsIgnore: ignore ts ignore
290
+ // @ts-ignore - Override Array.push to return the item instead of the new length
178
291
  push(data) {
179
292
  const item =
180
293
  data !== undefined && data instanceof this.itemClass ? data : new this.itemClass(data)
@@ -190,7 +303,11 @@ export default class MioloArray extends CacheMixin(Array) {
190
303
  clone() {
191
304
  const currentData = this.getData()
192
305
  const clonedData = JSON.parse(JSON.stringify(currentData))
193
- const clone = new this.constructor(clonedData)
306
+
307
+ /** @type {new (data?: any) => any} */
308
+ const Ctor = /** @type {any} */ (this.constructor)
309
+
310
+ const clone = new Ctor(clonedData)
194
311
  // Object.assign(clone, this)
195
312
  return clone
196
313
  }
@@ -1,7 +1,6 @@
1
- import CacheMixin from "./CacheMixin.mjs"
2
1
  import MioloArray from "./MioloArray.mjs"
3
2
 
4
- export default class MioloModel extends CacheMixin() {
3
+ export default class MioloModel extends class {} {
5
4
  /**
6
5
  * Initialize a new MioloModel.
7
6
  * Notice the attributes you set to your instance may be handled by MioloModel:
@@ -15,6 +14,107 @@ export default class MioloModel extends CacheMixin() {
15
14
  this.reset_cache()
16
15
  }
17
16
 
17
+ /**
18
+ * Reset the model's cache.
19
+ * @public
20
+ */
21
+ reset_cache() {
22
+ this.__cache__ = {}
23
+ }
24
+
25
+ /**
26
+ * Reset the model's cache.
27
+ * @public
28
+ */
29
+ resetCache = this.reset_cache
30
+
31
+ /**
32
+ * Get a value from the model's cache.
33
+ * @param {string} key - The key to get the value from.
34
+ * @returns {any} The value from the cache.
35
+ * @public
36
+ */
37
+ get_from_cache(key) {
38
+ if (this.__cache__ === undefined) {
39
+ return undefined
40
+ }
41
+ return this.__cache__[key]
42
+ }
43
+
44
+ /**
45
+ * Get a value from the model's cache.
46
+ * @param {string} key - The key to get the value from.
47
+ * @returns {any} The value from the cache.
48
+ * @public
49
+ */
50
+ getFromCache = this.get_from_cache
51
+
52
+ /**
53
+ * Set a value in the model's cache.
54
+ * @param {string} key - The key to set the value to.
55
+ * @param {any} value - The value to set.
56
+ * @public
57
+ */
58
+ set_to_cache(key, value) {
59
+ if (this.__cache__ === undefined) {
60
+ this.__cache__ = {}
61
+ }
62
+ this.__cache__[key] = value
63
+ }
64
+
65
+ /**
66
+ * Set a value in the model's cache.
67
+ * @param {string} key - The key to set the value to.
68
+ * @param {any} value - The value to set.
69
+ * @public
70
+ */
71
+ setToCache = this.set_to_cache
72
+
73
+ /**
74
+ * Get a value from the model's cache or make it if it doesn't exist.
75
+ * @param {string} cache_key - The key to get the value from.
76
+ * @param {Function} make_callback - The function to make the value if it doesn't exist.
77
+ * @returns {any} The value from the cache.
78
+ * @public
79
+ */
80
+ get_from_cache_or_make(cache_key, make_callback) {
81
+ if (this.__cache__ === undefined) {
82
+ this.__cache__ = {}
83
+ }
84
+ if (this.__cache__[cache_key] === undefined) {
85
+ const bound_callback = make_callback.bind(this)
86
+ this.__cache__[cache_key] = bound_callback()
87
+ }
88
+ return this.__cache__[cache_key]
89
+ }
90
+
91
+ /**
92
+ * Get a value from the model's cache or make it if it doesn't exist.
93
+ * @param {string} cache_key - The key to get the value from.
94
+ * @param {Function} make_callback - The function to make the value if it doesn't exist.
95
+ * @returns {any} The value from the cache.
96
+ * @public
97
+ */
98
+ getFromCacheOrMake = this.get_from_cache_or_make
99
+
100
+ /**
101
+ * Invalidate a value in the model's cache.
102
+ * @param {string} cache_key - The key to invalidate.
103
+ * @public
104
+ */
105
+ invalidate_cache(cache_key) {
106
+ if (this.__cache__ !== undefined) {
107
+ delete this.__cache__[cache_key]
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Invalidate a value in the model's cache.
113
+ * @param {string} cache_key - The key to invalidate.
114
+ * @public
115
+ */
116
+ invalidateCache = this.invalidate_cache
117
+
18
118
  /**
19
119
  * Get a value from the model's data.
20
120
  * @param {string} field - The field to get the value from.
@@ -203,7 +303,9 @@ export default class MioloModel extends CacheMixin() {
203
303
  const currentData = this.getData()
204
304
  const clonedData = JSON.parse(JSON.stringify(currentData))
205
305
 
206
- const clone = new this.constructor(clonedData)
306
+ /** @type {new (data?: any) => any} */
307
+ const Ctor = /** @type {any} */ (this.constructor)
308
+ const clone = new Ctor(clonedData)
207
309
 
208
310
  //Object.assign(clone, this)
209
311