miolo-model 3.0.0-beta.225 → 3.0.0-beta.227
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 +2 -1
- package/src/MioloArray.mjs +132 -22
- package/src/MioloModel.mjs +109 -7
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "miolo-model",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.227",
|
|
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
|
},
|
package/src/MioloArray.mjs
CHANGED
|
@@ -1,35 +1,38 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import("./MioloModel.mjs").default} MioloModel
|
|
3
|
+
*/
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* Represents an array of MioloModel instances.
|
|
5
7
|
* Inherits all native JS Array methods.
|
|
6
8
|
*
|
|
7
|
-
* @template T
|
|
9
|
+
* @template {MioloModel} T
|
|
8
10
|
* @extends {Array<T>}
|
|
9
11
|
*/
|
|
10
|
-
export default class MioloArray extends
|
|
12
|
+
export default class MioloArray extends Array {
|
|
11
13
|
/**
|
|
12
14
|
* Create a new MioloArray.
|
|
13
|
-
* @param {
|
|
14
|
-
* @param {Array<
|
|
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.
|
|
15
17
|
* @param {...any} extra - Extra parameters to pass to the item class constructor.
|
|
16
18
|
*/
|
|
17
19
|
constructor(itemClass, items = [], ...extra) {
|
|
18
|
-
// 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
|
+
|
|
19
26
|
if (typeof items === "number") {
|
|
20
|
-
super(items)
|
|
21
|
-
this.reset_cache()
|
|
22
27
|
return
|
|
23
28
|
}
|
|
24
|
-
//
|
|
25
|
-
super()
|
|
26
|
-
this.reset_cache()
|
|
27
29
|
|
|
28
30
|
// Check properties
|
|
29
31
|
if (!itemClass) {
|
|
30
32
|
console.error(`[${this.constructor.name}] MioloArray received no itemClass`)
|
|
31
33
|
return
|
|
32
34
|
}
|
|
35
|
+
/** @type {new (...args: any[]) => T} */
|
|
33
36
|
this.itemClass = itemClass
|
|
34
37
|
|
|
35
38
|
items.forEach((r, index) => {
|
|
@@ -38,9 +41,110 @@ export default class MioloArray extends CacheMixin(Array) {
|
|
|
38
41
|
})
|
|
39
42
|
}
|
|
40
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
|
+
|
|
41
145
|
/**
|
|
42
146
|
* Returns the last item in the array.
|
|
43
|
-
* @returns {
|
|
147
|
+
* @returns {T} The last item in the array.
|
|
44
148
|
* @public
|
|
45
149
|
*/
|
|
46
150
|
last() {
|
|
@@ -74,14 +178,14 @@ export default class MioloArray extends CacheMixin(Array) {
|
|
|
74
178
|
/**
|
|
75
179
|
* Find the index of the first item in the array that has the given field and value.
|
|
76
180
|
* @param {string} field - The field to search for.
|
|
77
|
-
* @param {
|
|
181
|
+
* @param {MioloModel} value - The value to search for.
|
|
78
182
|
* @returns {number} The index of the first item that has the given field and value, or -1 if not found.
|
|
79
183
|
* @public
|
|
80
184
|
*/
|
|
81
185
|
find_index_by_field(field, value) {
|
|
82
186
|
if (this.length >= 0) {
|
|
83
187
|
const fidx = this.findIndex((elem) => {
|
|
84
|
-
const val = typeof elem.
|
|
188
|
+
const val = typeof elem.get_value === "function" ? elem.get_value(field) : elem[field]
|
|
85
189
|
return val === value
|
|
86
190
|
})
|
|
87
191
|
if (fidx >= 0) {
|
|
@@ -106,13 +210,13 @@ export default class MioloArray extends CacheMixin(Array) {
|
|
|
106
210
|
* Find the first item in the array that has the given field and value.
|
|
107
211
|
* @param {string} field - The field to search for.
|
|
108
212
|
* @param {any} value - The value to search for.
|
|
109
|
-
* @returns {
|
|
213
|
+
* @returns {T | undefined} The first item in the array that has the given field and value, or undefined if not found.
|
|
110
214
|
* @public
|
|
111
215
|
*/
|
|
112
216
|
find_by_field(field, value) {
|
|
113
217
|
if (this.length >= 0) {
|
|
114
218
|
const filt = this.filter((elem) => {
|
|
115
|
-
const val = typeof elem.
|
|
219
|
+
const val = typeof elem.get_value === "function" ? elem.get_value(field) : elem[field]
|
|
116
220
|
return val === value
|
|
117
221
|
})
|
|
118
222
|
if (filt.length > 0) {
|
|
@@ -126,7 +230,7 @@ export default class MioloArray extends CacheMixin(Array) {
|
|
|
126
230
|
* Find the first item in the array that has the given field and value.
|
|
127
231
|
* @param {string} field - The field to search for.
|
|
128
232
|
* @param {any} value - The value to search for.
|
|
129
|
-
* @returns {
|
|
233
|
+
* @returns {T | undefined} The first item in the array that has the given field and value, or undefined if not found.
|
|
130
234
|
* @public
|
|
131
235
|
*/
|
|
132
236
|
findByField(field, value) {
|
|
@@ -136,7 +240,7 @@ export default class MioloArray extends CacheMixin(Array) {
|
|
|
136
240
|
/**
|
|
137
241
|
* Find the first item in the array that has the given id.
|
|
138
242
|
* @param {string} id - The id to search for.
|
|
139
|
-
* @returns {
|
|
243
|
+
* @returns {T | undefined} The first item in the array that has the given id, or undefined if not found.
|
|
140
244
|
* @public
|
|
141
245
|
*/
|
|
142
246
|
find_by_id(id) {
|
|
@@ -146,7 +250,7 @@ export default class MioloArray extends CacheMixin(Array) {
|
|
|
146
250
|
/**
|
|
147
251
|
* Find the first item in the array that has the given id.
|
|
148
252
|
* @param {string} id - The id to search for.
|
|
149
|
-
* @returns {
|
|
253
|
+
* @returns {T | undefined} The first item in the array that has the given id, or undefined if not found.
|
|
150
254
|
* @public
|
|
151
255
|
*/
|
|
152
256
|
findById(id) {
|
|
@@ -178,10 +282,12 @@ export default class MioloArray extends CacheMixin(Array) {
|
|
|
178
282
|
|
|
179
283
|
/**
|
|
180
284
|
* Push a new item to the end of the array.
|
|
181
|
-
* @param {
|
|
182
|
-
* @returns {
|
|
285
|
+
* @param {T} data - The data to push to the array.
|
|
286
|
+
* @returns {T} The new item.
|
|
183
287
|
* @public
|
|
184
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
|
|
185
291
|
push(data) {
|
|
186
292
|
const item =
|
|
187
293
|
data !== undefined && data instanceof this.itemClass ? data : new this.itemClass(data)
|
|
@@ -197,7 +303,11 @@ export default class MioloArray extends CacheMixin(Array) {
|
|
|
197
303
|
clone() {
|
|
198
304
|
const currentData = this.getData()
|
|
199
305
|
const clonedData = JSON.parse(JSON.stringify(currentData))
|
|
200
|
-
|
|
306
|
+
|
|
307
|
+
/** @type {new (data?: any) => any} */
|
|
308
|
+
const Ctor = /** @type {any} */ (this.constructor)
|
|
309
|
+
|
|
310
|
+
const clone = new Ctor(clonedData)
|
|
201
311
|
// Object.assign(clone, this)
|
|
202
312
|
return clone
|
|
203
313
|
}
|
package/src/MioloModel.mjs
CHANGED
|
@@ -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
|
|
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,14 +14,115 @@ 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.
|
|
21
|
-
* @param {any} def - The default value to return if the field is not found.
|
|
121
|
+
* @param {any} [def=undefined] - The default value to return if the field is not found.
|
|
22
122
|
* @returns {any} The value from the model's data.
|
|
23
123
|
* @public
|
|
24
124
|
*/
|
|
25
|
-
get_value(field, def) {
|
|
125
|
+
get_value(field, def = undefined) {
|
|
26
126
|
if (this.data !== undefined) {
|
|
27
127
|
if (this.data[field] !== undefined && this.data[field] !== null) {
|
|
28
128
|
return this.data[field]
|
|
@@ -34,12 +134,12 @@ export default class MioloModel extends CacheMixin() {
|
|
|
34
134
|
/**
|
|
35
135
|
* Get a value from the model's data.
|
|
36
136
|
* @param {string} field - The field to get the value from.
|
|
37
|
-
* @param {any} def - The default value to return if the field is not found.
|
|
137
|
+
* @param {any} [def=undefined] - The default value to return if the field is not found.
|
|
38
138
|
* @returns {any} The value from the model's data.
|
|
39
139
|
* @public
|
|
40
140
|
* @deprecated Use get_value() instead.
|
|
41
141
|
*/
|
|
42
|
-
_get(field, def) {
|
|
142
|
+
_get(field, def = undefined) {
|
|
43
143
|
return this.get_value(field, def)
|
|
44
144
|
}
|
|
45
145
|
|
|
@@ -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
|
-
|
|
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
|
|