@schukai/monster 3.5.0 → 3.7.0
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -1
- package/source/constants.mjs +4 -9
- package/source/constraints/abstract.mjs +4 -6
- package/source/constraints/abstractoperator.mjs +6 -12
- package/source/constraints/andoperator.mjs +3 -6
- package/source/constraints/invalid.mjs +3 -6
- package/source/constraints/isarray.mjs +6 -9
- package/source/constraints/isobject.mjs +4 -7
- package/source/constraints/namespace.mjs +1 -3
- package/source/constraints/oroperator.mjs +26 -25
- package/source/constraints/valid.mjs +3 -6
- package/source/data/buildmap.mjs +20 -36
- package/source/data/buildtree.mjs +29 -29
- package/source/data/datasource/namespace.mjs +1 -1
- package/source/data/datasource/server/restapi/writeerror.mjs +6 -6
- package/source/data/datasource/server/restapi.mjs +51 -65
- package/source/data/datasource/server/webconnect.mjs +26 -35
- package/source/data/datasource/server.mjs +16 -20
- package/source/data/datasource/storage/localstorage.mjs +8 -11
- package/source/data/datasource/storage/sessionstorage.mjs +8 -10
- package/source/data/datasource/storage.mjs +18 -22
- package/source/data/datasource.mjs +27 -43
- package/source/data/diff.mjs +20 -32
- package/source/data/extend.mjs +20 -14
- package/source/data/namespace.mjs +1 -3
- package/source/data/pathfinder.mjs +40 -67
- package/source/data/pipe.mjs +6 -10
- package/source/data/transformer.mjs +141 -157
- package/source/dom/assembler.mjs +6 -9
- package/source/dom/attributes.mjs +36 -48
- package/source/dom/constants.mjs +55 -71
- package/source/dom/customcontrol.mjs +16 -21
- package/source/dom/customelement.mjs +140 -164
- package/source/dom/events.mjs +17 -24
- package/source/dom/focusmanager.mjs +30 -48
- package/source/dom/locale.mjs +16 -19
- package/source/dom/namespace.mjs +1 -1
- package/source/dom/ready.mjs +7 -10
- package/source/dom/resource/data.mjs +38 -46
- package/source/dom/resource/link/namespace.mjs +1 -3
- package/source/dom/resource/link/stylesheet.mjs +7 -11
- package/source/dom/resource/link.mjs +36 -17
- package/source/dom/resource/namespace.mjs +1 -1
- package/source/dom/resource/script.mjs +23 -13
- package/source/dom/resource.mjs +47 -60
- package/source/dom/resourcemanager.mjs +36 -43
- package/source/dom/template.mjs +17 -24
- package/source/dom/theme.mjs +8 -12
- package/source/dom/updater.mjs +150 -196
- package/source/dom/util.mjs +11 -12
- package/source/dom/worker/factory.mjs +21 -25
- package/source/dom/worker/namespace.mjs +1 -1
- package/source/i18n/formatter.mjs +20 -24
- package/source/i18n/locale.mjs +40 -42
- package/source/i18n/namespace.mjs +1 -1
- package/source/i18n/provider.mjs +4 -7
- package/source/i18n/providers/embed.mjs +16 -26
- package/source/i18n/providers/fetch.mjs +30 -36
- package/source/i18n/translations.mjs +15 -22
- package/source/logging/handler/console.mjs +6 -10
- package/source/logging/handler.mjs +15 -18
- package/source/logging/logentry.mjs +8 -9
- package/source/logging/logger.mjs +73 -55
- package/source/logging/namespace.mjs +1 -1
- package/source/math/namespace.mjs +1 -1
- package/source/math/random.mjs +13 -14
- package/source/monster.mjs +132 -100
- package/source/net/namespace.mjs +1 -3
- package/source/net/webconnect/message.mjs +6 -8
- package/source/net/webconnect/namespace.mjs +1 -3
- package/source/net/webconnect.mjs +28 -39
- package/source/text/formatter.mjs +44 -55
- package/source/types/base.mjs +16 -26
- package/source/types/basewithoptions.mjs +8 -14
- package/source/types/binary.mjs +4 -5
- package/source/types/dataurl.mjs +19 -27
- package/source/types/global.mjs +14 -26
- package/source/types/id.mjs +4 -6
- package/source/types/is.mjs +11 -16
- package/source/types/mediatype.mjs +29 -44
- package/source/types/namespace.mjs +0 -1
- package/source/types/node.mjs +16 -22
- package/source/types/nodelist.mjs +10 -14
- package/source/types/noderecursiveiterator.mjs +11 -14
- package/source/types/observablequeue.mjs +13 -16
- package/source/types/observer.mjs +16 -23
- package/source/types/observerlist.mjs +18 -21
- package/source/types/proxyobserver.mjs +24 -37
- package/source/types/queue.mjs +8 -13
- package/source/types/randomid.mjs +10 -10
- package/source/types/regex.mjs +3 -5
- package/source/types/stack.mjs +4 -8
- package/source/types/tokenlist.mjs +24 -31
- package/source/types/typeof.mjs +5 -7
- package/source/types/uniquequeue.mjs +8 -13
- package/source/types/uuid.mjs +18 -25
- package/source/types/validate.mjs +26 -27
- package/source/types/version.mjs +17 -25
- package/source/util/clone.mjs +23 -32
- package/source/util/comparator.mjs +7 -15
- package/source/util/deadmansswitch.mjs +16 -23
- package/source/util/freeze.mjs +5 -7
- package/source/util/namespace.mjs +1 -1
- package/source/util/processing.mjs +33 -39
- package/source/util/trimspaces.mjs +17 -24
- package/test/cases/monster.mjs +1 -1
@@ -5,21 +5,21 @@
|
|
5
5
|
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
6
|
*/
|
7
7
|
|
8
|
-
import {internalSymbol,instanceSymbol} from "../../constants.mjs";
|
9
|
-
import {validateString} from "../../types/validate.mjs";
|
10
|
-
import {Datasource} from "../datasource.mjs";
|
8
|
+
import { internalSymbol, instanceSymbol } from "../../constants.mjs";
|
9
|
+
import { validateString } from "../../types/validate.mjs";
|
10
|
+
import { Datasource } from "../datasource.mjs";
|
11
11
|
|
12
|
-
export {Storage, storageObjectSymbol}
|
12
|
+
export { Storage, storageObjectSymbol };
|
13
13
|
|
14
14
|
/**
|
15
15
|
* @private
|
16
16
|
* @type {symbol}
|
17
17
|
*/
|
18
|
-
const storageObjectSymbol = Symbol.for
|
18
|
+
const storageObjectSymbol = Symbol.for("@schukai/monster/data/datasource/storage/@@storageObject");
|
19
19
|
|
20
20
|
/**
|
21
21
|
* The class represents a record.
|
22
|
-
*
|
22
|
+
*
|
23
23
|
* @license AGPLv3
|
24
24
|
* @since 1.22.0
|
25
25
|
* @copyright schukai GmbH
|
@@ -27,7 +27,6 @@ const storageObjectSymbol = Symbol.for ('@schukai/monster/data/datasource/stora
|
|
27
27
|
* @summary The Storage class encapsulates the access to data objects over WebStorageAPI.
|
28
28
|
*/
|
29
29
|
class Storage extends Datasource {
|
30
|
-
|
31
30
|
/**
|
32
31
|
*
|
33
32
|
* @param {string} key LocalStorage Key
|
@@ -35,7 +34,7 @@ class Storage extends Datasource {
|
|
35
34
|
*/
|
36
35
|
constructor(key) {
|
37
36
|
super();
|
38
|
-
this.setOption(
|
37
|
+
this.setOption("key", validateString(key));
|
39
38
|
}
|
40
39
|
|
41
40
|
/**
|
@@ -45,7 +44,7 @@ class Storage extends Datasource {
|
|
45
44
|
*/
|
46
45
|
static get [instanceSymbol]() {
|
47
46
|
return Symbol.for("@schukai/monster/data/datasource/storage");
|
48
|
-
}
|
47
|
+
}
|
49
48
|
|
50
49
|
/**
|
51
50
|
* @property {string} key=undefined LocalStorage Key
|
@@ -62,7 +61,7 @@ class Storage extends Datasource {
|
|
62
61
|
* @private
|
63
62
|
*/
|
64
63
|
[storageObjectSymbol]() {
|
65
|
-
throw new Error("this method must be implemented by derived classes")
|
64
|
+
throw new Error("this method must be implemented by derived classes");
|
66
65
|
}
|
67
66
|
|
68
67
|
/**
|
@@ -77,11 +76,10 @@ class Storage extends Datasource {
|
|
77
76
|
const storage = self[storageObjectSymbol]();
|
78
77
|
|
79
78
|
return new Promise(function (resolve) {
|
80
|
-
const data = JSON.parse(storage.getItem(self.getOption(
|
81
|
-
self.set(data??{});
|
79
|
+
const data = JSON.parse(storage.getItem(self.getOption("key")));
|
80
|
+
self.set(data ?? {});
|
82
81
|
resolve();
|
83
|
-
})
|
84
|
-
|
82
|
+
});
|
85
83
|
}
|
86
84
|
|
87
85
|
/**
|
@@ -94,24 +92,22 @@ class Storage extends Datasource {
|
|
94
92
|
const storage = self[storageObjectSymbol]();
|
95
93
|
|
96
94
|
return new Promise(function (resolve) {
|
97
|
-
|
98
95
|
const data = self.get();
|
99
96
|
if (data === undefined) {
|
100
|
-
storage.removeItem(self.getOption(
|
97
|
+
storage.removeItem(self.getOption("key"));
|
101
98
|
} else {
|
102
|
-
storage.setItem(self.getOption(
|
99
|
+
storage.setItem(self.getOption("key"), JSON.stringify(data));
|
103
100
|
}
|
104
101
|
|
105
102
|
resolve();
|
106
|
-
})
|
103
|
+
});
|
107
104
|
}
|
108
105
|
|
109
106
|
/**
|
110
107
|
* @return {Storage}
|
111
108
|
*/
|
112
109
|
getClone() {
|
113
|
-
const self=this;
|
114
|
-
return new Storage(self[internalSymbol].getRealSubject()[
|
115
|
-
}
|
116
|
-
|
110
|
+
const self = this;
|
111
|
+
return new Storage(self[internalSymbol].getRealSubject()["options"].key);
|
112
|
+
}
|
117
113
|
}
|
@@ -5,18 +5,17 @@
|
|
5
5
|
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
6
|
*/
|
7
7
|
|
8
|
-
import {internalSymbol} from "../constants.mjs";
|
9
|
-
import {instanceSymbol} from
|
10
|
-
import {Base} from "../types/base.mjs";
|
11
|
-
import {parseDataURL} from "../types/dataurl.mjs";
|
12
|
-
import {isString} from "../types/is.mjs";
|
13
|
-
import {ProxyObserver} from "../types/proxyobserver.mjs";
|
14
|
-
import {validateObject} from "../types/validate.mjs";
|
15
|
-
import {extend} from "./extend.mjs";
|
16
|
-
import {Pathfinder} from "./pathfinder.mjs";
|
17
|
-
|
18
|
-
export {Datasource}
|
19
|
-
|
8
|
+
import { internalSymbol } from "../constants.mjs";
|
9
|
+
import { instanceSymbol } from "../constants.mjs";
|
10
|
+
import { Base } from "../types/base.mjs";
|
11
|
+
import { parseDataURL } from "../types/dataurl.mjs";
|
12
|
+
import { isString } from "../types/is.mjs";
|
13
|
+
import { ProxyObserver } from "../types/proxyobserver.mjs";
|
14
|
+
import { validateObject } from "../types/validate.mjs";
|
15
|
+
import { extend } from "./extend.mjs";
|
16
|
+
import { Pathfinder } from "./pathfinder.mjs";
|
17
|
+
|
18
|
+
export { Datasource };
|
20
19
|
|
21
20
|
/**
|
22
21
|
* This callback can be passed to a datasource and is used to adapt data structures.
|
@@ -28,7 +27,6 @@ export {Datasource}
|
|
28
27
|
* @see Monster.Data.Datasource
|
29
28
|
*/
|
30
29
|
|
31
|
-
|
32
30
|
/**
|
33
31
|
* @private
|
34
32
|
* @type {symbol}
|
@@ -36,7 +34,7 @@ export {Datasource}
|
|
36
34
|
* @license AGPLv3
|
37
35
|
* @since 1.24.0
|
38
36
|
*/
|
39
|
-
const internalDataSymbol = Symbol.for(
|
37
|
+
const internalDataSymbol = Symbol.for("@schukai/monster/data/datasource/@@data");
|
40
38
|
|
41
39
|
/**
|
42
40
|
* The datasource class is the basis for dealing with different data sources.
|
@@ -49,22 +47,17 @@ const internalDataSymbol = Symbol.for('@schukai/monster/data/datasource/@@data')
|
|
49
47
|
* @summary The datasource class encapsulates the access to data objects.
|
50
48
|
*/
|
51
49
|
class Datasource extends Base {
|
52
|
-
|
53
50
|
/**
|
54
51
|
* creates a new datasource
|
55
|
-
*
|
52
|
+
*
|
56
53
|
*/
|
57
54
|
constructor() {
|
58
55
|
super();
|
59
56
|
this[internalSymbol] = new ProxyObserver({
|
60
|
-
|
57
|
+
options: extend({}, this.defaults),
|
61
58
|
});
|
62
|
-
|
63
|
-
this[internalDataSymbol] = new ProxyObserver({
|
64
59
|
|
65
|
-
});
|
66
|
-
|
67
|
-
|
60
|
+
this[internalDataSymbol] = new ProxyObserver({});
|
68
61
|
}
|
69
62
|
|
70
63
|
/**
|
@@ -74,7 +67,7 @@ class Datasource extends Base {
|
|
74
67
|
* @returns {Datasource}
|
75
68
|
*/
|
76
69
|
attachObserver(observer) {
|
77
|
-
this[internalDataSymbol].attachObserver(observer)
|
70
|
+
this[internalDataSymbol].attachObserver(observer);
|
78
71
|
return this;
|
79
72
|
}
|
80
73
|
|
@@ -85,7 +78,7 @@ class Datasource extends Base {
|
|
85
78
|
* @returns {Datasource}
|
86
79
|
*/
|
87
80
|
detachObserver(observer) {
|
88
|
-
this[internalDataSymbol].detachObserver(observer)
|
81
|
+
this[internalDataSymbol].detachObserver(observer);
|
89
82
|
return this;
|
90
83
|
}
|
91
84
|
|
@@ -120,7 +113,7 @@ class Datasource extends Base {
|
|
120
113
|
* @return {Datasource}
|
121
114
|
*/
|
122
115
|
setOption(path, value) {
|
123
|
-
new Pathfinder(this[internalSymbol].getSubject()[
|
116
|
+
new Pathfinder(this[internalSymbol].getSubject()["options"]).setVia(path, value);
|
124
117
|
return this;
|
125
118
|
}
|
126
119
|
|
@@ -130,13 +123,12 @@ class Datasource extends Base {
|
|
130
123
|
* @throws {Error} the options does not contain a valid json definition
|
131
124
|
*/
|
132
125
|
setOptions(options) {
|
133
|
-
|
134
126
|
if (isString(options)) {
|
135
|
-
options = parseOptionsJSON(options)
|
127
|
+
options = parseOptionsJSON(options);
|
136
128
|
}
|
137
129
|
|
138
130
|
const self = this;
|
139
|
-
extend(self[internalSymbol].getSubject()[
|
131
|
+
extend(self[internalSymbol].getSubject()["options"], self.defaults, options);
|
140
132
|
|
141
133
|
return self;
|
142
134
|
}
|
@@ -152,10 +144,8 @@ class Datasource extends Base {
|
|
152
144
|
let value;
|
153
145
|
|
154
146
|
try {
|
155
|
-
value = new Pathfinder(this[internalSymbol].getRealSubject()[
|
156
|
-
} catch (e) {
|
157
|
-
|
158
|
-
}
|
147
|
+
value = new Pathfinder(this[internalSymbol].getRealSubject()["options"]).getVia(path);
|
148
|
+
} catch (e) {}
|
159
149
|
|
160
150
|
if (value === undefined) return defaultValue;
|
161
151
|
return value;
|
@@ -166,7 +156,7 @@ class Datasource extends Base {
|
|
166
156
|
* @return {Promise}
|
167
157
|
*/
|
168
158
|
read() {
|
169
|
-
throw new Error("this method must be implemented by derived classes")
|
159
|
+
throw new Error("this method must be implemented by derived classes");
|
170
160
|
}
|
171
161
|
|
172
162
|
/**
|
@@ -174,13 +164,12 @@ class Datasource extends Base {
|
|
174
164
|
* @return {Promise}
|
175
165
|
*/
|
176
166
|
write() {
|
177
|
-
throw new Error("this method must be implemented by derived classes")
|
167
|
+
throw new Error("this method must be implemented by derived classes");
|
178
168
|
}
|
179
169
|
|
180
|
-
|
181
170
|
/**
|
182
171
|
* Returns real object
|
183
|
-
*
|
172
|
+
*
|
184
173
|
* @return {Object|Array}
|
185
174
|
*/
|
186
175
|
get() {
|
@@ -206,7 +195,6 @@ class Datasource extends Base {
|
|
206
195
|
static get [instanceSymbol]() {
|
207
196
|
return Symbol.for("@schukai/monster/data/datasource");
|
208
197
|
}
|
209
|
-
|
210
198
|
}
|
211
199
|
|
212
200
|
/**
|
@@ -217,22 +205,18 @@ class Datasource extends Base {
|
|
217
205
|
*/
|
218
206
|
function parseOptionsJSON(data) {
|
219
207
|
if (isString(data)) {
|
220
|
-
|
221
208
|
// the configuration can be specified as a data url.
|
222
209
|
try {
|
223
210
|
let dataUrl = parseDataURL(data);
|
224
211
|
data = dataUrl.content;
|
225
|
-
} catch (e) {
|
226
|
-
|
227
|
-
}
|
228
|
-
|
212
|
+
} catch (e) {}
|
229
213
|
|
230
214
|
try {
|
231
215
|
let obj = JSON.parse(data);
|
232
216
|
validateObject(obj);
|
233
217
|
return obj;
|
234
218
|
} catch (e) {
|
235
|
-
throw new Error(
|
219
|
+
throw new Error(`the options does not contain a valid json definition (actual: ${data}).`);
|
236
220
|
}
|
237
221
|
}
|
238
222
|
|
package/source/data/diff.mjs
CHANGED
@@ -5,10 +5,10 @@
|
|
5
5
|
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
6
|
*/
|
7
7
|
|
8
|
-
import {isArray, isObject} from "../types/is.mjs";
|
9
|
-
import {typeOf} from "../types/typeof.mjs";
|
8
|
+
import { isArray, isObject } from "../types/is.mjs";
|
9
|
+
import { typeOf } from "../types/typeof.mjs";
|
10
10
|
|
11
|
-
export {diff}
|
11
|
+
export { diff };
|
12
12
|
|
13
13
|
/**
|
14
14
|
* With the diff function you can perform the change of one object to another. The result shows the changes of the second object to the first object.
|
@@ -25,7 +25,7 @@ export {diff}
|
|
25
25
|
* @memberOf Monster.Data
|
26
26
|
*/
|
27
27
|
function diff(first, second) {
|
28
|
-
return doDiff(first, second)
|
28
|
+
return doDiff(first, second);
|
29
29
|
}
|
30
30
|
|
31
31
|
/**
|
@@ -54,37 +54,30 @@ function getKeys(a, b, type) {
|
|
54
54
|
* @return {array}
|
55
55
|
*/
|
56
56
|
function doDiff(a, b, path, diff) {
|
57
|
-
|
58
|
-
let
|
59
|
-
let typeB = typeOf(b)
|
57
|
+
let typeA = typeOf(a);
|
58
|
+
let typeB = typeOf(b);
|
60
59
|
|
61
60
|
const currPath = path || [];
|
62
61
|
const currDiff = diff || [];
|
63
62
|
|
64
|
-
if (typeA === typeB && (typeA ===
|
65
|
-
|
63
|
+
if (typeA === typeB && (typeA === "object" || typeA === "array")) {
|
66
64
|
getKeys(a, b, typeA).forEach((v) => {
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
currDiff.push(buildResult(a[v], b[v], 'delete', currPath.concat(v)));
|
65
|
+
if (!Object.prototype.hasOwnProperty.call(a, v)) {
|
66
|
+
currDiff.push(buildResult(a[v], b[v], "add", currPath.concat(v)));
|
67
|
+
} else if (!Object.prototype.hasOwnProperty.call(b, v)) {
|
68
|
+
currDiff.push(buildResult(a[v], b[v], "delete", currPath.concat(v)));
|
72
69
|
} else {
|
73
70
|
doDiff(a[v], b[v], currPath.concat(v), currDiff);
|
74
71
|
}
|
75
72
|
});
|
76
|
-
|
77
73
|
} else {
|
78
|
-
|
79
74
|
const o = getOperator(a, b, typeA, typeB);
|
80
75
|
if (o !== undefined) {
|
81
76
|
currDiff.push(buildResult(a, b, o, path));
|
82
77
|
}
|
83
|
-
|
84
78
|
}
|
85
79
|
|
86
80
|
return currDiff;
|
87
|
-
|
88
81
|
}
|
89
82
|
|
90
83
|
/**
|
@@ -97,16 +90,15 @@ function doDiff(a, b, path, diff) {
|
|
97
90
|
* @private
|
98
91
|
*/
|
99
92
|
function buildResult(a, b, operator, path) {
|
100
|
-
|
101
93
|
const result = {
|
102
94
|
operator,
|
103
95
|
path,
|
104
96
|
};
|
105
97
|
|
106
|
-
if (operator !==
|
98
|
+
if (operator !== "add") {
|
107
99
|
result.first = {
|
108
100
|
value: a,
|
109
|
-
type: typeof a
|
101
|
+
type: typeof a,
|
110
102
|
};
|
111
103
|
|
112
104
|
if (isObject(a)) {
|
@@ -117,10 +109,10 @@ function buildResult(a, b, operator, path) {
|
|
117
109
|
}
|
118
110
|
}
|
119
111
|
|
120
|
-
if (operator ===
|
112
|
+
if (operator === "add" || operator === "update") {
|
121
113
|
result.second = {
|
122
114
|
value: b,
|
123
|
-
type: typeof b
|
115
|
+
type: typeof b,
|
124
116
|
};
|
125
117
|
|
126
118
|
if (isObject(b)) {
|
@@ -129,7 +121,6 @@ function buildResult(a, b, operator, path) {
|
|
129
121
|
result.second.instance = name;
|
130
122
|
}
|
131
123
|
}
|
132
|
-
|
133
124
|
}
|
134
125
|
|
135
126
|
return result;
|
@@ -142,7 +133,6 @@ function buildResult(a, b, operator, path) {
|
|
142
133
|
* @return {boolean}
|
143
134
|
*/
|
144
135
|
function isNotEqual(a, b) {
|
145
|
-
|
146
136
|
if (typeof a !== typeof b) {
|
147
137
|
return true;
|
148
138
|
}
|
@@ -161,7 +151,6 @@ function isNotEqual(a, b) {
|
|
161
151
|
* @return {string|undefined}
|
162
152
|
*/
|
163
153
|
function getOperator(a, b) {
|
164
|
-
|
165
154
|
/**
|
166
155
|
* @type {string|undefined}
|
167
156
|
*/
|
@@ -177,14 +166,13 @@ function getOperator(a, b) {
|
|
177
166
|
*/
|
178
167
|
let typeB = typeof b;
|
179
168
|
|
180
|
-
if (typeA ===
|
181
|
-
operator =
|
182
|
-
} else if (typeA !==
|
183
|
-
operator =
|
169
|
+
if (typeA === "undefined" && typeB !== "undefined") {
|
170
|
+
operator = "add";
|
171
|
+
} else if (typeA !== "undefined" && typeB === "undefined") {
|
172
|
+
operator = "delete";
|
184
173
|
} else if (isNotEqual(a, b)) {
|
185
|
-
operator =
|
174
|
+
operator = "update";
|
186
175
|
}
|
187
176
|
|
188
177
|
return operator;
|
189
|
-
|
190
178
|
}
|
package/source/data/extend.mjs
CHANGED
@@ -5,10 +5,10 @@
|
|
5
5
|
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
|
6
6
|
*/
|
7
7
|
|
8
|
-
import {isArray, isObject} from "../types/is.mjs";
|
9
|
-
import {typeOf} from "../types/typeof.mjs";
|
8
|
+
import { isArray, isObject } from "../types/is.mjs";
|
9
|
+
import { typeOf } from "../types/typeof.mjs";
|
10
10
|
|
11
|
-
export {extend}
|
11
|
+
export { extend };
|
12
12
|
|
13
13
|
/**
|
14
14
|
* Extend copies all enumerable own properties from one or
|
@@ -23,15 +23,21 @@ export {extend}
|
|
23
23
|
* @memberOf Monster.Data
|
24
24
|
* @throws {Error} unsupported argument
|
25
25
|
* @throws {Error} type mismatch
|
26
|
+
* @throws {Error} unsupported argument
|
26
27
|
*/
|
27
|
-
function extend() {
|
28
|
-
let o
|
28
|
+
function extend(...args) {
|
29
|
+
let o;
|
30
|
+
let i;
|
31
|
+
|
32
|
+
if (typeof args !== "object" || args[0] === null) {
|
33
|
+
throw new Error(`unsupported argument ${JSON.stringify(args[0])}`);
|
34
|
+
}
|
29
35
|
|
30
|
-
for (i = 0; i <
|
31
|
-
let a =
|
36
|
+
for (i = 0; i < args.length; i++) {
|
37
|
+
let a = args[i];
|
32
38
|
|
33
39
|
if (!(isObject(a) || isArray(a))) {
|
34
|
-
throw new Error(
|
40
|
+
throw new Error(`unsupported argument ${JSON.stringify(a)}`);
|
35
41
|
}
|
36
42
|
|
37
43
|
if (o === undefined) {
|
@@ -40,15 +46,13 @@ function extend() {
|
|
40
46
|
}
|
41
47
|
|
42
48
|
for (let k in a) {
|
43
|
-
|
44
49
|
let v = a?.[k];
|
45
50
|
|
46
51
|
if (v === o?.[k]) {
|
47
52
|
continue;
|
48
53
|
}
|
49
54
|
|
50
|
-
if ((isObject(v)&&typeOf(v)===
|
51
|
-
|
55
|
+
if ((isObject(v) && typeOf(v) === "object") || isArray(v)) {
|
52
56
|
if (o[k] === undefined) {
|
53
57
|
if (isArray(v)) {
|
54
58
|
o[k] = [];
|
@@ -57,16 +61,18 @@ function extend() {
|
|
57
61
|
}
|
58
62
|
} else {
|
59
63
|
if (typeOf(o[k]) !== typeOf(v)) {
|
60
|
-
throw new Error(
|
64
|
+
throw new Error(
|
65
|
+
`type mismatch: ${JSON.stringify(o[k])}(${typeOf(o[k])}) != ${JSON.stringify(v)}(${typeOf(
|
66
|
+
v,
|
67
|
+
)})`,
|
68
|
+
);
|
61
69
|
}
|
62
70
|
}
|
63
71
|
|
64
72
|
o[k] = extend(o[k], v);
|
65
|
-
|
66
73
|
} else {
|
67
74
|
o[k] = v;
|
68
75
|
}
|
69
|
-
|
70
76
|
}
|
71
77
|
}
|
72
78
|
|