objectmodel 4.4.4 → 4.4.6
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/.eslintignore +8 -8
- package/.eslintrc.json +25 -25
- package/LICENSE +22 -22
- package/README.md +67 -67
- package/build/add-banner.js +13 -13
- package/build/bundle-entry.dev.js +2 -2
- package/build/bundle-entry.js +11 -11
- package/build/update-docs.js +1 -1
- package/dist/object-model.cjs +111 -110
- package/dist/object-model.js +86 -83
- package/dist/object-model.js.map +1 -1
- package/dist/object-model.min.js +4 -2
- package/dist/object-model.min.js.map +1 -1
- package/index.html +4 -4
- package/package.json +74 -70
- package/rollup.config.js +13 -13
- package/src/helpers.js +43 -43
- package/src/list-model.js +43 -43
- package/src/object-model.d.ts +11 -10
- package/src/object-model.js +1 -0
- package/test/basic-model.spec.cjs +272 -272
- package/test/basic-model.test-d.ts +1 -0
- package/test/bench/array.html +51 -51
- package/test/bench/bench-lib.js +49 -49
- package/test/bench/map-no-cast.html +53 -53
- package/test/bench/map-set.html +52 -52
- package/test/bench/map.html +51 -51
- package/test/bench/object-models.html +87 -87
- package/test/index.cjs +13 -13
- package/test/map-model.spec.cjs +236 -236
- package/test/model.spec.cjs +30 -30
- package/test/object-model.spec.cjs +2 -1
- package/test/object-model.test-d.ts +9 -2
- package/test/set-model.spec.cjs +222 -222
- package/test/umd.html +25 -25
- package/types/index.d.ts +3 -1
package/test/set-model.spec.cjs
CHANGED
|
@@ -1,223 +1,223 @@
|
|
|
1
|
-
/* global QUnit, SetModel, ObjectModel */
|
|
2
|
-
|
|
3
|
-
QUnit.module("Set Models");
|
|
4
|
-
|
|
5
|
-
QUnit.test("constructor && proto", function (assert) {
|
|
6
|
-
|
|
7
|
-
assert.ok(SetModel instanceof Function, "SetModel instanceof Function");
|
|
8
|
-
|
|
9
|
-
const MySet = SetModel(String);
|
|
10
|
-
|
|
11
|
-
assert.ok(MySet instanceof SetModel, "Set models can be declared");
|
|
12
|
-
|
|
13
|
-
assert.ok(typeof MySet.extend === "function", "test Set model method extend");
|
|
14
|
-
assert.ok(typeof MySet.assert === "function", "test Set model method assert");
|
|
15
|
-
assert.ok(typeof MySet.test === "function", "test Set model method test");
|
|
16
|
-
assert.ok(MySet.definition === String, "test Set model prop definition");
|
|
17
|
-
assert.ok(typeof MySet.assertions === "object", "test Set model prop assertions");
|
|
18
|
-
|
|
19
|
-
assert.ok(SetModel(undefined) instanceof SetModel, "SetModel can receive undefined as argument");
|
|
20
|
-
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
QUnit.test("instanciation && mutation methods watchers", function (assert) {
|
|
24
|
-
|
|
25
|
-
const S = SetModel(Number).assert(s => s.size >= 2, "minsize assert");
|
|
26
|
-
const s = S([1, 2]);
|
|
27
|
-
|
|
28
|
-
assert.ok(s instanceof S && s instanceof Set, "Set models can be instanciated");
|
|
29
|
-
|
|
30
|
-
s.add(3);
|
|
31
|
-
|
|
32
|
-
assert.equal(s.add.name, "add", "proxyfied methods keep original properties");
|
|
33
|
-
|
|
34
|
-
assert.throws(function () {
|
|
35
|
-
s.add("four")
|
|
36
|
-
}, /TypeError.*four/, "add calls are catched");
|
|
37
|
-
|
|
38
|
-
assert.equal(s.size, 3, "set size change is ok 1/2");
|
|
39
|
-
|
|
40
|
-
s.delete(3);
|
|
41
|
-
assert.throws(function () {
|
|
42
|
-
s.delete(2);
|
|
43
|
-
}, /TypeError.*minsize assert/, "delete calls are catched");
|
|
44
|
-
|
|
45
|
-
assert.throws(function () {
|
|
46
|
-
s.clear();
|
|
47
|
-
}, /TypeError.*minsize assert/, "clear calls are catched");
|
|
48
|
-
|
|
49
|
-
assert.equal(s.size, 2, "set size change is ok 2/2");
|
|
50
|
-
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
QUnit.test("validation in constructor", function (assert) {
|
|
54
|
-
|
|
55
|
-
const S = SetModel(String)
|
|
56
|
-
const s = S(["one", "two"]);
|
|
57
|
-
assert.equal(s.size, 2, "set size is ok");
|
|
58
|
-
|
|
59
|
-
assert.throws(function () {
|
|
60
|
-
S(["one", 2])
|
|
61
|
-
}, /TypeError/, "validation in set model constructor 1/2");
|
|
62
|
-
|
|
63
|
-
assert.throws(function () {
|
|
64
|
-
S([1, "two"])
|
|
65
|
-
}, /TypeError/, "validation in set model constructor 2/2");
|
|
66
|
-
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
QUnit.test("union types & submodels", function (assert) {
|
|
70
|
-
|
|
71
|
-
const Question = ObjectModel({
|
|
72
|
-
answer: Number
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
const Quiz = SetModel([Question, String, Boolean]);
|
|
76
|
-
const s = Quiz(["test", true, { answer: 42 }]);
|
|
77
|
-
s.add("is it real life ?");
|
|
78
|
-
s.add(true);
|
|
79
|
-
s.add({ answer: 43 });
|
|
80
|
-
assert.throws(function () {
|
|
81
|
-
s.add(42);
|
|
82
|
-
}, /TypeError[\s\S]*got Number 42/m, "set invalid type on union type");
|
|
83
|
-
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
QUnit.test("union types & fixed values", function (assert) {
|
|
87
|
-
|
|
88
|
-
const S = SetModel([true, 2, "3"]);
|
|
89
|
-
assert.throws(function () {
|
|
90
|
-
S(["3", 4]);
|
|
91
|
-
}, /TypeError[\s\S]*Set.*Number 4/, "SetModel fixed values");
|
|
92
|
-
|
|
93
|
-
S([2, true]);
|
|
94
|
-
const S2 = S.extend().assert(s => s.size === 2);
|
|
95
|
-
const s2 = new S2([2, '3']);
|
|
96
|
-
|
|
97
|
-
assert.ok(Object.getPrototypeOf(S2.prototype) === S.prototype, "extension respect prototypal chain");
|
|
98
|
-
assert.ok(s2 instanceof S2 && s2 instanceof S, "set model inheritance");
|
|
99
|
-
S([2, true]).add("3");
|
|
100
|
-
assert.throws(function () {
|
|
101
|
-
S2([2, true]).add("3")
|
|
102
|
-
}, /TypeError/, "min/max of inherit set model");
|
|
103
|
-
|
|
104
|
-
const S3 = S2.extend("new", "val");
|
|
105
|
-
S3(["val", true]);
|
|
106
|
-
S3([true, "new"]);
|
|
107
|
-
assert.throws(function () {
|
|
108
|
-
S2(["val", true]);
|
|
109
|
-
}, /TypeError/, "set model type extension");
|
|
110
|
-
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
QUnit.test("Child set models in object models", function (assert) {
|
|
114
|
-
|
|
115
|
-
const Child = ObjectModel({ set: SetModel(Number) });
|
|
116
|
-
const Parent = ObjectModel({ child: Child });
|
|
117
|
-
|
|
118
|
-
const childO = Child({ set: new Set([1, 2, 3, 5, 8]) });
|
|
119
|
-
assert.ok(childO.set instanceof Set, "child set model is instanceof Set");
|
|
120
|
-
const parentO = Parent({ child: childO });
|
|
121
|
-
assert.ok(parentO.child.set instanceof Set, "child set model from parent is Set");
|
|
122
|
-
|
|
123
|
-
childO.set.add(13);
|
|
124
|
-
assert.throws(function () {
|
|
125
|
-
childO.set.add("21");
|
|
126
|
-
}, /TypeError/, "child array model catches invalid set value");
|
|
127
|
-
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
QUnit.test("defaults values", function (assert) {
|
|
131
|
-
|
|
132
|
-
const S = SetModel(Number).defaultTo(new Set([1, 2]));
|
|
133
|
-
const a = S();
|
|
134
|
-
|
|
135
|
-
assert.ok(a instanceof Set && a.size === 2, "Set model default value");
|
|
136
|
-
|
|
137
|
-
S.default.add(3);
|
|
138
|
-
|
|
139
|
-
const b = S();
|
|
140
|
-
|
|
141
|
-
assert.ok(b.size === 3 && Array.from(b.values()).sort().join(";") === "1;2;3", "set model default value is mutable");
|
|
142
|
-
|
|
143
|
-
S.default = "nope";
|
|
144
|
-
|
|
145
|
-
assert.throws(function () {
|
|
146
|
-
S()
|
|
147
|
-
}, /TypeError/, "invalid default property still throws TypeError for set models");
|
|
148
|
-
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
QUnit.test("validate defaut values provided", function (assert) {
|
|
152
|
-
assert.throws(function () {
|
|
153
|
-
SetModel(Number).defaultTo()
|
|
154
|
-
}, /TypeError.*expecting Set of Number, got undefined*/, "invalid default value provided: undefined");
|
|
155
|
-
assert.throws(function () {
|
|
156
|
-
SetModel(Number).defaultTo(new Set(["foo", "bar"]))
|
|
157
|
-
}, /TypeError.*expecting Set value to be Number, got String \"foo\"*/, "invalid default value provided: wrong type");
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
QUnit.test("assertions", function (assert) {
|
|
161
|
-
|
|
162
|
-
const SetMax3 = SetModel(String).assert(function maxEntries(set) {
|
|
163
|
-
return set.size <= 3;
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
let set = SetMax3(["one", "two"]);
|
|
167
|
-
|
|
168
|
-
set.add("three");
|
|
169
|
-
assert.throws(function () {
|
|
170
|
-
set.add("four");
|
|
171
|
-
}, /TypeError[\s\S]*maxEntries/, "test assertion after set method");
|
|
172
|
-
|
|
173
|
-
const AssertSet = SetModel(Number).assert(s => s.size > 0, "may throw exception");
|
|
174
|
-
|
|
175
|
-
new AssertSet([1, 2]);
|
|
176
|
-
|
|
177
|
-
assert.throws(function () { new AssertSet([]); },
|
|
178
|
-
/assertion "may throw exception" returned false.*for value \[]/,
|
|
179
|
-
"assertions catch exceptions on Set models"
|
|
180
|
-
);
|
|
181
|
-
|
|
182
|
-
})
|
|
183
|
-
|
|
184
|
-
QUnit.test("Automatic model casting", function (assert) {
|
|
185
|
-
|
|
186
|
-
const N = ObjectModel({ x: Number, y: [Number] }).defaultTo({ x: 5, y: 7 });
|
|
187
|
-
const S = SetModel(N);
|
|
188
|
-
const s = S([{ x: 9 }]);
|
|
189
|
-
|
|
190
|
-
let n = Array.from(s.values())[0];
|
|
191
|
-
assert.ok(n instanceof N, "test automatic model casting with set init 1/2")
|
|
192
|
-
assert.equal(n.x * n.y, 63, "test automatic model casting with set init 2/2")
|
|
193
|
-
|
|
194
|
-
s.add({ x: 3 });
|
|
195
|
-
n = Array.from(s.values())[1];
|
|
196
|
-
|
|
197
|
-
assert.ok(n instanceof N, "test automatic model casting with array mutator method 1/2")
|
|
198
|
-
assert.equal(n.x * n.y, 21, "test automatic model casting with array mutator method 2/2")
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
QUnit.test("toString", function (assert) {
|
|
202
|
-
assert.equal(SetModel(Number).toString(), "Set of Number", "SetModel toString for basic elements")
|
|
203
|
-
assert.equal(SetModel([String, 42]).toString(), "Set of String or 42", "SetModel toString for union type elements")
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
QUnit.test("dynamic definition", function (assert) {
|
|
207
|
-
let S = SetModel(String);
|
|
208
|
-
let s1 = S(["hello", "world"])
|
|
209
|
-
S.definition = Number;
|
|
210
|
-
let s2 = S([1, 2, 3])
|
|
211
|
-
assert.equal(S.test(s1), false, "definition can be dynamically changed 1/4")
|
|
212
|
-
assert.equal(S.test(s2), true, "definition can be dynamically changed 2/4")
|
|
213
|
-
s1.clear();
|
|
214
|
-
assert.throws(() => s1.add("hello"), /TypeError/, "definition can be dynamically changed 3/4")
|
|
215
|
-
s1.add(42);
|
|
216
|
-
assert.ok(s1.has(42), "definition can be dynamically changed 4/4")
|
|
217
|
-
|
|
218
|
-
let OM = ObjectModel({ n: Number });
|
|
219
|
-
S.definition = OM;
|
|
220
|
-
s1.clear();
|
|
221
|
-
s1.add({ n: 42 });
|
|
222
|
-
assert.ok([...s1][0] instanceof OM, "autocast still works after definition dynamically changed")
|
|
1
|
+
/* global QUnit, SetModel, ObjectModel */
|
|
2
|
+
|
|
3
|
+
QUnit.module("Set Models");
|
|
4
|
+
|
|
5
|
+
QUnit.test("constructor && proto", function (assert) {
|
|
6
|
+
|
|
7
|
+
assert.ok(SetModel instanceof Function, "SetModel instanceof Function");
|
|
8
|
+
|
|
9
|
+
const MySet = SetModel(String);
|
|
10
|
+
|
|
11
|
+
assert.ok(MySet instanceof SetModel, "Set models can be declared");
|
|
12
|
+
|
|
13
|
+
assert.ok(typeof MySet.extend === "function", "test Set model method extend");
|
|
14
|
+
assert.ok(typeof MySet.assert === "function", "test Set model method assert");
|
|
15
|
+
assert.ok(typeof MySet.test === "function", "test Set model method test");
|
|
16
|
+
assert.ok(MySet.definition === String, "test Set model prop definition");
|
|
17
|
+
assert.ok(typeof MySet.assertions === "object", "test Set model prop assertions");
|
|
18
|
+
|
|
19
|
+
assert.ok(SetModel(undefined) instanceof SetModel, "SetModel can receive undefined as argument");
|
|
20
|
+
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
QUnit.test("instanciation && mutation methods watchers", function (assert) {
|
|
24
|
+
|
|
25
|
+
const S = SetModel(Number).assert(s => s.size >= 2, "minsize assert");
|
|
26
|
+
const s = S([1, 2]);
|
|
27
|
+
|
|
28
|
+
assert.ok(s instanceof S && s instanceof Set, "Set models can be instanciated");
|
|
29
|
+
|
|
30
|
+
s.add(3);
|
|
31
|
+
|
|
32
|
+
assert.equal(s.add.name, "add", "proxyfied methods keep original properties");
|
|
33
|
+
|
|
34
|
+
assert.throws(function () {
|
|
35
|
+
s.add("four")
|
|
36
|
+
}, /TypeError.*four/, "add calls are catched");
|
|
37
|
+
|
|
38
|
+
assert.equal(s.size, 3, "set size change is ok 1/2");
|
|
39
|
+
|
|
40
|
+
s.delete(3);
|
|
41
|
+
assert.throws(function () {
|
|
42
|
+
s.delete(2);
|
|
43
|
+
}, /TypeError.*minsize assert/, "delete calls are catched");
|
|
44
|
+
|
|
45
|
+
assert.throws(function () {
|
|
46
|
+
s.clear();
|
|
47
|
+
}, /TypeError.*minsize assert/, "clear calls are catched");
|
|
48
|
+
|
|
49
|
+
assert.equal(s.size, 2, "set size change is ok 2/2");
|
|
50
|
+
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
QUnit.test("validation in constructor", function (assert) {
|
|
54
|
+
|
|
55
|
+
const S = SetModel(String)
|
|
56
|
+
const s = S(["one", "two"]);
|
|
57
|
+
assert.equal(s.size, 2, "set size is ok");
|
|
58
|
+
|
|
59
|
+
assert.throws(function () {
|
|
60
|
+
S(["one", 2])
|
|
61
|
+
}, /TypeError/, "validation in set model constructor 1/2");
|
|
62
|
+
|
|
63
|
+
assert.throws(function () {
|
|
64
|
+
S([1, "two"])
|
|
65
|
+
}, /TypeError/, "validation in set model constructor 2/2");
|
|
66
|
+
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
QUnit.test("union types & submodels", function (assert) {
|
|
70
|
+
|
|
71
|
+
const Question = ObjectModel({
|
|
72
|
+
answer: Number
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
const Quiz = SetModel([Question, String, Boolean]);
|
|
76
|
+
const s = Quiz(["test", true, { answer: 42 }]);
|
|
77
|
+
s.add("is it real life ?");
|
|
78
|
+
s.add(true);
|
|
79
|
+
s.add({ answer: 43 });
|
|
80
|
+
assert.throws(function () {
|
|
81
|
+
s.add(42);
|
|
82
|
+
}, /TypeError[\s\S]*got Number 42/m, "set invalid type on union type");
|
|
83
|
+
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
QUnit.test("union types & fixed values", function (assert) {
|
|
87
|
+
|
|
88
|
+
const S = SetModel([true, 2, "3"]);
|
|
89
|
+
assert.throws(function () {
|
|
90
|
+
S(["3", 4]);
|
|
91
|
+
}, /TypeError[\s\S]*Set.*Number 4/, "SetModel fixed values");
|
|
92
|
+
|
|
93
|
+
S([2, true]);
|
|
94
|
+
const S2 = S.extend().assert(s => s.size === 2);
|
|
95
|
+
const s2 = new S2([2, '3']);
|
|
96
|
+
|
|
97
|
+
assert.ok(Object.getPrototypeOf(S2.prototype) === S.prototype, "extension respect prototypal chain");
|
|
98
|
+
assert.ok(s2 instanceof S2 && s2 instanceof S, "set model inheritance");
|
|
99
|
+
S([2, true]).add("3");
|
|
100
|
+
assert.throws(function () {
|
|
101
|
+
S2([2, true]).add("3")
|
|
102
|
+
}, /TypeError/, "min/max of inherit set model");
|
|
103
|
+
|
|
104
|
+
const S3 = S2.extend("new", "val");
|
|
105
|
+
S3(["val", true]);
|
|
106
|
+
S3([true, "new"]);
|
|
107
|
+
assert.throws(function () {
|
|
108
|
+
S2(["val", true]);
|
|
109
|
+
}, /TypeError/, "set model type extension");
|
|
110
|
+
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
QUnit.test("Child set models in object models", function (assert) {
|
|
114
|
+
|
|
115
|
+
const Child = ObjectModel({ set: SetModel(Number) });
|
|
116
|
+
const Parent = ObjectModel({ child: Child });
|
|
117
|
+
|
|
118
|
+
const childO = Child({ set: new Set([1, 2, 3, 5, 8]) });
|
|
119
|
+
assert.ok(childO.set instanceof Set, "child set model is instanceof Set");
|
|
120
|
+
const parentO = Parent({ child: childO });
|
|
121
|
+
assert.ok(parentO.child.set instanceof Set, "child set model from parent is Set");
|
|
122
|
+
|
|
123
|
+
childO.set.add(13);
|
|
124
|
+
assert.throws(function () {
|
|
125
|
+
childO.set.add("21");
|
|
126
|
+
}, /TypeError/, "child array model catches invalid set value");
|
|
127
|
+
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
QUnit.test("defaults values", function (assert) {
|
|
131
|
+
|
|
132
|
+
const S = SetModel(Number).defaultTo(new Set([1, 2]));
|
|
133
|
+
const a = S();
|
|
134
|
+
|
|
135
|
+
assert.ok(a instanceof Set && a.size === 2, "Set model default value");
|
|
136
|
+
|
|
137
|
+
S.default.add(3);
|
|
138
|
+
|
|
139
|
+
const b = S();
|
|
140
|
+
|
|
141
|
+
assert.ok(b.size === 3 && Array.from(b.values()).sort().join(";") === "1;2;3", "set model default value is mutable");
|
|
142
|
+
|
|
143
|
+
S.default = "nope";
|
|
144
|
+
|
|
145
|
+
assert.throws(function () {
|
|
146
|
+
S()
|
|
147
|
+
}, /TypeError/, "invalid default property still throws TypeError for set models");
|
|
148
|
+
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
QUnit.test("validate defaut values provided", function (assert) {
|
|
152
|
+
assert.throws(function () {
|
|
153
|
+
SetModel(Number).defaultTo()
|
|
154
|
+
}, /TypeError.*expecting Set of Number, got undefined*/, "invalid default value provided: undefined");
|
|
155
|
+
assert.throws(function () {
|
|
156
|
+
SetModel(Number).defaultTo(new Set(["foo", "bar"]))
|
|
157
|
+
}, /TypeError.*expecting Set value to be Number, got String \"foo\"*/, "invalid default value provided: wrong type");
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
QUnit.test("assertions", function (assert) {
|
|
161
|
+
|
|
162
|
+
const SetMax3 = SetModel(String).assert(function maxEntries(set) {
|
|
163
|
+
return set.size <= 3;
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
let set = SetMax3(["one", "two"]);
|
|
167
|
+
|
|
168
|
+
set.add("three");
|
|
169
|
+
assert.throws(function () {
|
|
170
|
+
set.add("four");
|
|
171
|
+
}, /TypeError[\s\S]*maxEntries/, "test assertion after set method");
|
|
172
|
+
|
|
173
|
+
const AssertSet = SetModel(Number).assert(s => s.size > 0, "may throw exception");
|
|
174
|
+
|
|
175
|
+
new AssertSet([1, 2]);
|
|
176
|
+
|
|
177
|
+
assert.throws(function () { new AssertSet([]); },
|
|
178
|
+
/assertion "may throw exception" returned false.*for value \[]/,
|
|
179
|
+
"assertions catch exceptions on Set models"
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
})
|
|
183
|
+
|
|
184
|
+
QUnit.test("Automatic model casting", function (assert) {
|
|
185
|
+
|
|
186
|
+
const N = ObjectModel({ x: Number, y: [Number] }).defaultTo({ x: 5, y: 7 });
|
|
187
|
+
const S = SetModel(N);
|
|
188
|
+
const s = S([{ x: 9 }]);
|
|
189
|
+
|
|
190
|
+
let n = Array.from(s.values())[0];
|
|
191
|
+
assert.ok(n instanceof N, "test automatic model casting with set init 1/2")
|
|
192
|
+
assert.equal(n.x * n.y, 63, "test automatic model casting with set init 2/2")
|
|
193
|
+
|
|
194
|
+
s.add({ x: 3 });
|
|
195
|
+
n = Array.from(s.values())[1];
|
|
196
|
+
|
|
197
|
+
assert.ok(n instanceof N, "test automatic model casting with array mutator method 1/2")
|
|
198
|
+
assert.equal(n.x * n.y, 21, "test automatic model casting with array mutator method 2/2")
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
QUnit.test("toString", function (assert) {
|
|
202
|
+
assert.equal(SetModel(Number).toString(), "Set of Number", "SetModel toString for basic elements")
|
|
203
|
+
assert.equal(SetModel([String, 42]).toString(), "Set of String or 42", "SetModel toString for union type elements")
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
QUnit.test("dynamic definition", function (assert) {
|
|
207
|
+
let S = SetModel(String);
|
|
208
|
+
let s1 = S(["hello", "world"])
|
|
209
|
+
S.definition = Number;
|
|
210
|
+
let s2 = S([1, 2, 3])
|
|
211
|
+
assert.equal(S.test(s1), false, "definition can be dynamically changed 1/4")
|
|
212
|
+
assert.equal(S.test(s2), true, "definition can be dynamically changed 2/4")
|
|
213
|
+
s1.clear();
|
|
214
|
+
assert.throws(() => s1.add("hello"), /TypeError/, "definition can be dynamically changed 3/4")
|
|
215
|
+
s1.add(42);
|
|
216
|
+
assert.ok(s1.has(42), "definition can be dynamically changed 4/4")
|
|
217
|
+
|
|
218
|
+
let OM = ObjectModel({ n: Number });
|
|
219
|
+
S.definition = OM;
|
|
220
|
+
s1.clear();
|
|
221
|
+
s1.add({ n: 42 });
|
|
222
|
+
assert.ok([...s1][0] instanceof OM, "autocast still works after definition dynamically changed")
|
|
223
223
|
})
|
package/test/umd.html
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html>
|
|
3
|
-
|
|
4
|
-
<head>
|
|
5
|
-
<meta charset="utf-8">
|
|
6
|
-
<title>QUnit - ObjectModel unit tests</title>
|
|
7
|
-
<link rel="stylesheet" href="lib/qunit.css">
|
|
8
|
-
<script src="lib/qunit.js"></script>
|
|
9
|
-
<script src="https://umd.cdn.pika.dev/objectmodel/v4"></script>
|
|
10
|
-
<script>const { Model, BasicModel, ObjectModel, ArrayModel, FunctionModel, MapModel, SetModel, Any } = objectmodel</script>
|
|
11
|
-
<script src="model.spec.cjs"></script>
|
|
12
|
-
<script src="basic-model.spec.cjs"></script>
|
|
13
|
-
<script src="any-model.spec.cjs"></script>
|
|
14
|
-
<script src="object-model.spec.cjs"></script>
|
|
15
|
-
<script src="array-model.spec.cjs"></script>
|
|
16
|
-
<script src="function-model.spec.cjs"></script>
|
|
17
|
-
<script src="map-model.spec.cjs"></script>
|
|
18
|
-
<script src="set-model.spec.cjs"></script>
|
|
19
|
-
</head>
|
|
20
|
-
|
|
21
|
-
<body>
|
|
22
|
-
<div id="qunit"></div>
|
|
23
|
-
<div id="qunit-fixture"></div>
|
|
24
|
-
</body>
|
|
25
|
-
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
|
|
4
|
+
<head>
|
|
5
|
+
<meta charset="utf-8">
|
|
6
|
+
<title>QUnit - ObjectModel unit tests</title>
|
|
7
|
+
<link rel="stylesheet" href="lib/qunit.css">
|
|
8
|
+
<script src="lib/qunit.js"></script>
|
|
9
|
+
<script src="https://umd.cdn.pika.dev/objectmodel/v4"></script>
|
|
10
|
+
<script>const { Model, BasicModel, ObjectModel, ArrayModel, FunctionModel, MapModel, SetModel, Any } = objectmodel</script>
|
|
11
|
+
<script src="model.spec.cjs"></script>
|
|
12
|
+
<script src="basic-model.spec.cjs"></script>
|
|
13
|
+
<script src="any-model.spec.cjs"></script>
|
|
14
|
+
<script src="object-model.spec.cjs"></script>
|
|
15
|
+
<script src="array-model.spec.cjs"></script>
|
|
16
|
+
<script src="function-model.spec.cjs"></script>
|
|
17
|
+
<script src="map-model.spec.cjs"></script>
|
|
18
|
+
<script src="set-model.spec.cjs"></script>
|
|
19
|
+
</head>
|
|
20
|
+
|
|
21
|
+
<body>
|
|
22
|
+
<div id="qunit"></div>
|
|
23
|
+
<div id="qunit-fixture"></div>
|
|
24
|
+
</body>
|
|
25
|
+
|
|
26
26
|
</html>
|
package/types/index.d.ts
CHANGED
|
@@ -13,4 +13,6 @@ export const ObjectModel: ObjectModelConstructor;
|
|
|
13
13
|
export const ArrayModel: ArrayModelConstructor;
|
|
14
14
|
export const FunctionModel: FunctionModelConstructor;
|
|
15
15
|
export const MapModel: MapModelConstructor;
|
|
16
|
-
export const SetModel: SetModelConstructor;
|
|
16
|
+
export const SetModel: SetModelConstructor;
|
|
17
|
+
|
|
18
|
+
export { ModelError } from "../src/object-model";
|