miolo-model 3.0.0-beta.158

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 ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "miolo-model",
3
+ "version": "3.0.0-beta.158",
4
+ "description": "Data models for miolo world",
5
+ "author": "Donato Lorenzo <donato@afialapis.com>",
6
+ "contributors": [
7
+ "Donato Lorenzo <donato@afialapis.com>"
8
+ ],
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "https://github.com/afialapis/miolo.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/afialapis/miolo/issues"
15
+ },
16
+ "homepage": "https://www.afialapis.com/os/miolo",
17
+ "license": "MIT",
18
+ "type": "module",
19
+ "exports": {
20
+ ".": "./src/index.mjs"
21
+ },
22
+ "files": [
23
+ "src",
24
+ "package.json"
25
+ ],
26
+ "scripts": {
27
+ "reset": "rm -fr package-lock.json npm-lock.yaml node_modules && npm i",
28
+ "lint": "biome check ./src --reporter=github",
29
+ "lint:fix": "biome check --write ./src --reporter=github",
30
+ "test": "node --test ./test/index.mjs",
31
+ "prepublishOnly": "npm run lint && npm run test"
32
+ },
33
+ "engines": {
34
+ "node": ">=24"
35
+ }
36
+ }
@@ -0,0 +1,22 @@
1
+ export default (BaseClass = class {}) =>
2
+ class extends BaseClass {
3
+ reset_cache() {
4
+ this.__cache__ = {}
5
+ }
6
+ get_from_cache_or_make(cache_key, make_callback) {
7
+ if (this.__cache__ === undefined) {
8
+ this.__cache__ = {}
9
+ }
10
+ if (this.__cache__[cache_key] === undefined) {
11
+ const bound_callback = make_callback.bind(this)
12
+ this.__cache__[cache_key] = bound_callback()
13
+ }
14
+ return this.__cache__[cache_key]
15
+ }
16
+
17
+ invalidate_cache(cache_key) {
18
+ if (this.__cache__ !== undefined) {
19
+ delete this.__cache__[cache_key]
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,78 @@
1
+ import CacheMixin from "./CacheMixin.mjs"
2
+
3
+ export default class MioloArray extends CacheMixin(Array) {
4
+ constructor(itemClass, items = [], ...extra) {
5
+ // Arrays can be inited with a number
6
+ if (typeof items === "number") {
7
+ super(items)
8
+ this.reset_cache()
9
+ return
10
+ }
11
+ //
12
+ super()
13
+ this.reset_cache()
14
+
15
+ // Check properties
16
+ if (!itemClass) {
17
+ console.error(`[${this.constructor.name}] MioloArray received no itemClass`)
18
+ return
19
+ }
20
+ this.itemClass = itemClass
21
+
22
+ items.forEach((r, index) => {
23
+ this[index] =
24
+ r !== undefined && r instanceof this.itemClass ? r : new this.itemClass(r || {}, ...extra)
25
+ })
26
+ }
27
+
28
+ last() {
29
+ return this[this.length - 1]
30
+ }
31
+
32
+ get_data() {
33
+ return [...this].map((i) => i.get_data())
34
+ }
35
+
36
+ find_index_by_field(field, value) {
37
+ if (this.length >= 0) {
38
+ const fidx = this.findIndex((elem) => {
39
+ const val = typeof elem._get === "function" ? elem._get(field) : elem[field]
40
+ return val === value
41
+ })
42
+ if (fidx >= 0) {
43
+ return fidx
44
+ }
45
+ }
46
+ return -1
47
+ }
48
+
49
+ find_by_field(field, value) {
50
+ if (this.length >= 0) {
51
+ const filt = this.filter((elem) => {
52
+ const val = typeof elem._get === "function" ? elem._get(field) : elem[field]
53
+ return val === value
54
+ })
55
+ if (filt.length > 0) {
56
+ return filt[0]
57
+ }
58
+ }
59
+ return undefined
60
+ }
61
+
62
+ find_by_id(id) {
63
+ return this.find_by_field("id", id)
64
+ }
65
+
66
+ remove_by_field(field, value) {
67
+ const fidx = this.find_index_by_field(field, value)
68
+ if (fidx >= 0) {
69
+ this.splice(fidx, 1)
70
+ }
71
+ }
72
+
73
+ append(data) {
74
+ const item = new this.itemClass(data)
75
+ this[this.length] = item
76
+ return item
77
+ }
78
+ }
@@ -0,0 +1,54 @@
1
+ import CacheMixin from "./CacheMixin.mjs"
2
+
3
+ export default class MioloModel extends CacheMixin() {
4
+ constructor(data) {
5
+ super()
6
+ this.data = data
7
+ }
8
+
9
+ _get(field, def) {
10
+ if (this.data !== undefined) {
11
+ if (this.data[field] !== undefined && this.data[field] !== null) {
12
+ return this.data[field]
13
+ }
14
+ }
15
+ return def
16
+ }
17
+
18
+ _set(field, val) {
19
+ if (this.data === undefined) {
20
+ this.data = {}
21
+ }
22
+ this.data[field] = val
23
+ }
24
+
25
+ get_extra_data() {
26
+ const data = {}
27
+ for (const [key, value] of Object.entries(this)) {
28
+ if (value instanceof MioloModel) {
29
+ data[key] = value.get_data()
30
+ } else if (Array.isArray(value) && value.every((v) => v instanceof MioloModel)) {
31
+ data[key] = value.map((v) => v.get_data())
32
+ }
33
+ }
34
+ return data
35
+ }
36
+
37
+ get_data() {
38
+ const extra = this.get_extra_data() || {}
39
+ return {
40
+ ...this.data,
41
+ ...extra
42
+ }
43
+ }
44
+
45
+ update(changes) {
46
+ if (this.data === undefined) {
47
+ this.data = {}
48
+ }
49
+ this.data = {
50
+ ...this.data,
51
+ ...changes
52
+ }
53
+ }
54
+ }
package/src/index.mjs ADDED
@@ -0,0 +1,4 @@
1
+ import MioloArray from "./MioloArray.mjs"
2
+ import MioloModel from "./MioloModel.mjs"
3
+
4
+ export { MioloModel, MioloArray }