miolo-model 3.0.0-beta.222 → 3.0.0-beta.225

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.222",
3
+ "version": "3.0.0-beta.225",
4
4
  "description": "Data models for miolo world",
5
5
  "author": "Donato Lorenzo <donato@afialapis.com>",
6
6
  "contributors": [
@@ -1,11 +1,25 @@
1
1
  export default (BaseClass = class {}) =>
2
2
  class extends BaseClass {
3
+ /**
4
+ * Reset the model's cache.
5
+ * @public
6
+ */
3
7
  reset_cache() {
4
8
  this.__cache__ = {}
5
9
  }
6
10
 
11
+ /**
12
+ * Reset the model's cache.
13
+ * @public
14
+ */
7
15
  resetCache = this.reset_cache
8
16
 
17
+ /**
18
+ * Get a value from the model's cache.
19
+ * @param {string} key - The key to get the value from.
20
+ * @returns {any} The value from the cache.
21
+ * @public
22
+ */
9
23
  get_from_cache(key) {
10
24
  if (this.__cache__ === undefined) {
11
25
  return undefined
@@ -13,8 +27,20 @@ export default (BaseClass = class {}) =>
13
27
  return this.__cache__[key]
14
28
  }
15
29
 
30
+ /**
31
+ * Get a value from the model's cache.
32
+ * @param {string} key - The key to get the value from.
33
+ * @returns {any} The value from the cache.
34
+ * @public
35
+ */
16
36
  getFromCache = this.get_from_cache
17
37
 
38
+ /**
39
+ * Set a value in the model's cache.
40
+ * @param {string} key - The key to set the value to.
41
+ * @param {any} value - The value to set.
42
+ * @public
43
+ */
18
44
  set_to_cache(key, value) {
19
45
  if (this.__cache__ === undefined) {
20
46
  this.__cache__ = {}
@@ -22,8 +48,21 @@ export default (BaseClass = class {}) =>
22
48
  this.__cache__[key] = value
23
49
  }
24
50
 
51
+ /**
52
+ * Set a value in the model's cache.
53
+ * @param {string} key - The key to set the value to.
54
+ * @param {any} value - The value to set.
55
+ * @public
56
+ */
25
57
  setToCache = this.set_to_cache
26
58
 
59
+ /**
60
+ * Get a value from the model's cache or make it if it doesn't exist.
61
+ * @param {string} cache_key - The key to get the value from.
62
+ * @param {Function} make_callback - The function to make the value if it doesn't exist.
63
+ * @returns {any} The value from the cache.
64
+ * @public
65
+ */
27
66
  get_from_cache_or_make(cache_key, make_callback) {
28
67
  if (this.__cache__ === undefined) {
29
68
  this.__cache__ = {}
@@ -35,13 +74,30 @@ export default (BaseClass = class {}) =>
35
74
  return this.__cache__[cache_key]
36
75
  }
37
76
 
77
+ /**
78
+ * Get a value from the model's cache or make it if it doesn't exist.
79
+ * @param {string} cache_key - The key to get the value from.
80
+ * @param {Function} make_callback - The function to make the value if it doesn't exist.
81
+ * @returns {any} The value from the cache.
82
+ * @public
83
+ */
38
84
  getFromCacheOrMake = this.get_from_cache_or_make
39
85
 
86
+ /**
87
+ * Invalidate a value in the model's cache.
88
+ * @param {string} cache_key - The key to invalidate.
89
+ * @public
90
+ */
40
91
  invalidate_cache(cache_key) {
41
92
  if (this.__cache__ !== undefined) {
42
93
  delete this.__cache__[cache_key]
43
94
  }
44
95
  }
45
96
 
97
+ /**
98
+ * Invalidate a value in the model's cache.
99
+ * @param {string} cache_key - The key to invalidate.
100
+ * @public
101
+ */
46
102
  invalidateCache = this.invalidate_cache
47
103
  }
@@ -1,6 +1,19 @@
1
1
  import CacheMixin from "./CacheMixin.mjs"
2
2
 
3
+ /**
4
+ * Represents an array of MioloModel instances.
5
+ * Inherits all native JS Array methods.
6
+ *
7
+ * @template T
8
+ * @extends {Array<T>}
9
+ */
3
10
  export default class MioloArray extends CacheMixin(Array) {
11
+ /**
12
+ * Create a new MioloArray.
13
+ * @param {Function} itemClass - The class of the items in the array.
14
+ * @param {Array<MioloModel> | Array<Object>} items - The items to initialize the array with.
15
+ * @param {...any} extra - Extra parameters to pass to the item class constructor.
16
+ */
4
17
  constructor(itemClass, items = [], ...extra) {
5
18
  // Arrays can be inited with a number
6
19
  if (typeof items === "number") {
@@ -25,18 +38,46 @@ export default class MioloArray extends CacheMixin(Array) {
25
38
  })
26
39
  }
27
40
 
41
+ /**
42
+ * Returns the last item in the array.
43
+ * @returns {MioloModel} The last item in the array.
44
+ * @public
45
+ */
28
46
  last() {
29
47
  return this[this.length - 1]
30
48
  }
31
49
 
50
+ /**
51
+ * Returns an Object data containing both:
52
+ * - the instance inner data
53
+ * - data from attriobutes of type MioloModel or MioloArray
54
+ * (attributes with prefix "__" are ignored).
55
+ * @returns {Object} All the data of the instance.
56
+ * @public
57
+ */
32
58
  get_data() {
33
59
  return [...this].map((i) => i.get_data())
34
60
  }
35
61
 
62
+ /**
63
+ * Returns an Object data containing both:
64
+ * - the instance inner data
65
+ * - data from attriobutes of type MioloModel or MioloArray
66
+ * (attributes with prefix "__" are ignored).
67
+ * @returns {Object} All the data of the instance.
68
+ * @public
69
+ */
36
70
  getData() {
37
71
  return [...this].map((i) => i.getData())
38
72
  }
39
73
 
74
+ /**
75
+ * Find the index of the first item in the array that has the given field and value.
76
+ * @param {string} field - The field to search for.
77
+ * @param {any} value - The value to search for.
78
+ * @returns {number} The index of the first item that has the given field and value, or -1 if not found.
79
+ * @public
80
+ */
40
81
  find_index_by_field(field, value) {
41
82
  if (this.length >= 0) {
42
83
  const fidx = this.findIndex((elem) => {
@@ -50,10 +91,24 @@ export default class MioloArray extends CacheMixin(Array) {
50
91
  return -1
51
92
  }
52
93
 
94
+ /**
95
+ * Find the index of the first item in the array that has the given field and value.
96
+ * @param {string} field - The field to search for.
97
+ * @param {any} value - The value to search for.
98
+ * @returns {number} The index of the first item that has the given field and value, or -1 if not found.
99
+ * @public
100
+ */
53
101
  findIndexByField(field, value) {
54
102
  return this.find_index_by_field(field, value)
55
103
  }
56
104
 
105
+ /**
106
+ * Find the first item in the array that has the given field and value.
107
+ * @param {string} field - The field to search for.
108
+ * @param {any} value - The value to search for.
109
+ * @returns {MioloModel} The first item in the array that has the given field and value, or undefined if not found.
110
+ * @public
111
+ */
57
112
  find_by_field(field, value) {
58
113
  if (this.length >= 0) {
59
114
  const filt = this.filter((elem) => {
@@ -67,18 +122,43 @@ export default class MioloArray extends CacheMixin(Array) {
67
122
  return undefined
68
123
  }
69
124
 
125
+ /**
126
+ * Find the first item in the array that has the given field and value.
127
+ * @param {string} field - The field to search for.
128
+ * @param {any} value - The value to search for.
129
+ * @returns {MioloModel} The first item in the array that has the given field and value, or undefined if not found.
130
+ * @public
131
+ */
70
132
  findByField(field, value) {
71
133
  return this.find_by_field(field, value)
72
134
  }
73
135
 
136
+ /**
137
+ * Find the first item in the array that has the given id.
138
+ * @param {string} id - The id to search for.
139
+ * @returns {MioloModel} The first item in the array that has the given id, or undefined if not found.
140
+ * @public
141
+ */
74
142
  find_by_id(id) {
75
143
  return this.find_by_field("id", id)
76
144
  }
77
145
 
146
+ /**
147
+ * Find the first item in the array that has the given id.
148
+ * @param {string} id - The id to search for.
149
+ * @returns {MioloModel} The first item in the array that has the given id, or undefined if not found.
150
+ * @public
151
+ */
78
152
  findById(id) {
79
153
  return this.find_by_id(id)
80
154
  }
81
155
 
156
+ /**
157
+ * Remove the first item in the array that has the given field and value.
158
+ * @param {string} field - The field to search for.
159
+ * @param {any} value - The value to search for.
160
+ * @public
161
+ */
82
162
  remove_by_field(field, value) {
83
163
  const fidx = this.find_index_by_field(field, value)
84
164
  if (fidx >= 0) {
@@ -86,10 +166,22 @@ export default class MioloArray extends CacheMixin(Array) {
86
166
  }
87
167
  }
88
168
 
169
+ /**
170
+ * Remove the first item in the array that has the given field and value.
171
+ * @param {string} field - The field to search for.
172
+ * @param {any} value - The value to search for.
173
+ * @public
174
+ */
89
175
  removeByField(field, value) {
90
176
  return this.remove_by_field(field, value)
91
177
  }
92
178
 
179
+ /**
180
+ * Push a new item to the end of the array.
181
+ * @param {Object} data - The data to push to the array.
182
+ * @returns {MioloModel | Object} The new item.
183
+ * @public
184
+ */
93
185
  push(data) {
94
186
  const item =
95
187
  data !== undefined && data instanceof this.itemClass ? data : new this.itemClass(data)
@@ -97,6 +189,11 @@ export default class MioloArray extends CacheMixin(Array) {
97
189
  return item
98
190
  }
99
191
 
192
+ /**
193
+ * Creates a shallow clone of the model.
194
+ * @returns {MioloArray} A shallow clone of the model.
195
+ * @public
196
+ */
100
197
  clone() {
101
198
  const currentData = this.getData()
102
199
  const clonedData = JSON.parse(JSON.stringify(currentData))
@@ -2,13 +2,27 @@ import CacheMixin from "./CacheMixin.mjs"
2
2
  import MioloArray from "./MioloArray.mjs"
3
3
 
4
4
  export default class MioloModel extends CacheMixin() {
5
+ /**
6
+ * Initialize a new MioloModel.
7
+ * Notice the attributes you set to your instance may be handled by MioloModel:
8
+ * - if you set an attribute of type MioloModel or MioloArray, it will be taken by get_data() / getData() methods
9
+ * - if you want an attribute of type MioloModel or MioloArray to be ignored by get_data() / getData() methods, you can prefix it with "__" (e.g. __my_attr)
10
+ * @param {Object} data - The data to initialize the model with.
11
+ */
5
12
  constructor(data) {
6
13
  super()
7
14
  this.data = data
8
15
  this.reset_cache()
9
16
  }
10
17
 
11
- _get(field, def) {
18
+ /**
19
+ * Get a value from the model's data.
20
+ * @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.
22
+ * @returns {any} The value from the model's data.
23
+ * @public
24
+ */
25
+ get_value(field, def) {
12
26
  if (this.data !== undefined) {
13
27
  if (this.data[field] !== undefined && this.data[field] !== null) {
14
28
  return this.data[field]
@@ -17,14 +31,49 @@ export default class MioloModel extends CacheMixin() {
17
31
  return def
18
32
  }
19
33
 
20
- _set(field, val) {
34
+ /**
35
+ * Get a value from the model's data.
36
+ * @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.
38
+ * @returns {any} The value from the model's data.
39
+ * @public
40
+ * @deprecated Use get_value() instead.
41
+ */
42
+ _get(field, def) {
43
+ return this.get_value(field, def)
44
+ }
45
+
46
+ /**
47
+ * Set a value in the model's data.
48
+ * @param {string} field - The field to set the value to.
49
+ * @param {any} val - The value to set.
50
+ * @public
51
+ */
52
+ set_value(field, val) {
21
53
  if (this.data === undefined) {
22
54
  this.data = {}
23
55
  }
24
56
  this.data[field] = val
25
57
  }
26
58
 
27
- get_extra_data() {
59
+ /**
60
+ * Set a value in the model's data.
61
+ * @param {string} field - The field to set the value to.
62
+ * @param {any} val - The value to set.
63
+ * @public
64
+ * @deprecated Use set_value() instead.
65
+ */
66
+ _set(field, val) {
67
+ this.set_value(field, val)
68
+ }
69
+
70
+ /**
71
+ * Returns an Object data corresponding to MioloModel and MioloArray instances attributes of this instance.
72
+ * Attributes with prefix "__" are ignored.
73
+ * @returns {Object} The extra data from the model.
74
+ * @private
75
+ */
76
+ _get_extra_data() {
28
77
  const data = {}
29
78
  for (const [key, value] of Object.entries(this)) {
30
79
  if (key.startsWith("__")) {
@@ -39,7 +88,13 @@ export default class MioloModel extends CacheMixin() {
39
88
  return data
40
89
  }
41
90
 
42
- getExtraData() {
91
+ /**
92
+ * Returns an Object data corresponding to MioloModel and MioloArray instances attributes of this instance.
93
+ * Attributes with prefix "__" are ignored.
94
+ * @returns {Object} The extra data from the model.
95
+ * @private
96
+ */
97
+ _getExtraData() {
43
98
  const data = {}
44
99
  for (const [key, value] of Object.entries(this)) {
45
100
  if (key.startsWith("__")) {
@@ -54,22 +109,46 @@ export default class MioloModel extends CacheMixin() {
54
109
  return data
55
110
  }
56
111
 
112
+ /**
113
+ * Returns an Object data containing both:
114
+ * - the instance inner data
115
+ * - data from attriobutes of type MioloModel or MioloArray
116
+ * (attributes with prefix "__" are ignored).
117
+ * @returns {Object} All the data of the instance.
118
+ * @public
119
+ */
57
120
  get_data() {
58
- const extra = this.get_extra_data() || {}
121
+ const extra = this._get_extra_data() || {}
59
122
  return {
60
123
  ...this.data,
61
124
  ...extra
62
125
  }
63
126
  }
64
127
 
128
+ /**
129
+ * Returns an Object data containing both:
130
+ * - the instance inner data
131
+ * - data from attriobutes of type MioloModel or MioloArray
132
+ * (attributes with prefix "__" are ignored).
133
+ * @returns {Object} All the data of the instance.
134
+ * @public
135
+ */
65
136
  getData() {
66
- const extra = this.getExtraData() || {}
137
+ const extra = this._getExtraData() || {}
67
138
  return {
68
139
  ...this.data,
69
140
  ...extra
70
141
  }
71
142
  }
72
143
 
144
+ /**
145
+ * Update the model's inner data with changes.
146
+ * It resetes model's inner cache.
147
+ * Does the same (updating data and resetting cache) for nested MioloModel or MioloArray attributes
148
+ * (ignoring those prefixed with "__").
149
+ * @param {Object} changes - The changes to apply to the model.
150
+ * @public
151
+ */
73
152
  update(changes) {
74
153
  this.reset_cache()
75
154
 
@@ -93,6 +172,14 @@ export default class MioloModel extends CacheMixin() {
93
172
  }
94
173
  }
95
174
 
175
+ /**
176
+ * Update the model's inner data by merging another model's data with it.
177
+ * It resetes model's inner cache.
178
+ * Does the same (updating data and resetting cache) for nested MioloModel or MioloArray attributes
179
+ * (ignoring those prefixed with "__").
180
+ * @param {Object} model - The model to merge with.
181
+ * @public
182
+ */
96
183
  merge(model) {
97
184
  this.update(model.getData())
98
185
  for (const [key, value] of Object.entries(model)) {
@@ -107,6 +194,11 @@ export default class MioloModel extends CacheMixin() {
107
194
  }
108
195
  }
109
196
 
197
+ /**
198
+ * Creates a shallow clone of the model.
199
+ * @returns {MioloModel} A shallow clone of the model.
200
+ * @public
201
+ */
110
202
  clone() {
111
203
  const currentData = this.getData()
112
204
  const clonedData = JSON.parse(JSON.stringify(currentData))