objectmodel 4.3.1 → 4.4.0
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/.travis.yml +2 -2
- 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/dist/object-model.cjs +462 -469
- package/dist/object-model.js +462 -469
- package/dist/object-model.js.map +1 -1
- package/dist/object-model.min.js +2 -2
- package/dist/object-model.min.js.map +1 -1
- package/index.html +4 -4
- package/package.json +9 -3
- package/rollup.config.js +13 -13
- package/src/array-model.d.ts +16 -0
- package/src/array-model.js +68 -68
- package/src/devtool-formatter.js +198 -198
- package/src/function-model.d.ts +24 -0
- package/src/function-model.js +58 -65
- package/src/helpers.js +43 -43
- package/src/index.js +4 -4
- package/src/list-model.js +43 -43
- package/src/map-model.d.ts +18 -0
- package/src/map-model.js +48 -48
- package/src/object-model.d.ts +74 -0
- package/src/set-model.d.ts +16 -0
- package/src/set-model.js +41 -41
- package/test/array-model.spec.cjs +291 -291
- package/test/array-model.test-d.ts +24 -0
- package/test/basic-model.spec.cjs +263 -263
- package/test/basic-model.test-d.ts +30 -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/function-model.spec.cjs +161 -162
- package/test/function-model.test-d.ts +18 -0
- package/test/index.cjs +13 -13
- package/test/map-model.spec.cjs +224 -224
- package/test/map-model.test-d.ts +21 -0
- package/test/model.spec.cjs +30 -30
- package/test/object-model.spec.cjs +1345 -1346
- package/test/object-model.test-d.ts +53 -0
- package/test/set-model.spec.cjs +213 -213
- package/test/set-model.test-d.ts +17 -0
- package/test/umd.html +25 -25
- package/types/definitions.d.ts +43 -0
- package/types/helpers.d.ts +4 -0
- package/types/index.d.ts +6 -128
|
@@ -1,163 +1,162 @@
|
|
|
1
|
-
/* global QUnit, ArrayModel, ObjectModel, FunctionModel */
|
|
2
|
-
|
|
3
|
-
QUnit.module("Function Models");
|
|
4
|
-
|
|
5
|
-
QUnit.test("constructor && proto", function (assert) {
|
|
6
|
-
|
|
7
|
-
assert.equal(typeof FunctionModel, "function", "FunctionModel is defined");
|
|
8
|
-
|
|
9
|
-
const Operation = FunctionModel(Number, Number).return(Number);
|
|
10
|
-
|
|
11
|
-
assert.ok(Operation instanceof FunctionModel, "model instance of FunctionModel");
|
|
12
|
-
|
|
13
|
-
assert.ok(typeof Operation.
|
|
14
|
-
assert.ok(typeof Operation.
|
|
15
|
-
assert.ok(typeof Operation.
|
|
16
|
-
assert.
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
assert.ok(Operation.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
assert.
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
assert.equal(joe.
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
case "
|
|
86
|
-
case "
|
|
87
|
-
case "
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
assert.equal(calc(
|
|
94
|
-
assert.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
assert.equal(api({ list: [1, 2, 3, 4], op: "
|
|
118
|
-
assert.
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
assert.
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
const
|
|
155
|
-
const
|
|
156
|
-
const
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
assert.
|
|
160
|
-
assert.equal(returnValue.
|
|
161
|
-
|
|
162
|
-
|
|
1
|
+
/* global QUnit, ArrayModel, ObjectModel, FunctionModel */
|
|
2
|
+
|
|
3
|
+
QUnit.module("Function Models");
|
|
4
|
+
|
|
5
|
+
QUnit.test("constructor && proto", function (assert) {
|
|
6
|
+
|
|
7
|
+
assert.equal(typeof FunctionModel, "function", "FunctionModel is defined");
|
|
8
|
+
|
|
9
|
+
const Operation = FunctionModel(Number, Number).return(Number);
|
|
10
|
+
|
|
11
|
+
assert.ok(Operation instanceof FunctionModel, "model instance of FunctionModel");
|
|
12
|
+
|
|
13
|
+
assert.ok(typeof Operation.assert === "function", "test Function model method assert");
|
|
14
|
+
assert.ok(typeof Operation.test === "function", "test Function model method test");
|
|
15
|
+
assert.ok(typeof Operation.return === "function", "test Function model method return");
|
|
16
|
+
assert.equal(Operation.definition.arguments.map(a => a.name).join(','),
|
|
17
|
+
'Number,Number', "test Function model prop definition");
|
|
18
|
+
assert.ok(Operation.definition.return === Number, "test Function model prop return");
|
|
19
|
+
assert.ok(typeof Operation.assertions === "object", "test Function model prop assertions");
|
|
20
|
+
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
QUnit.test("instanciation and controls", function (assert) {
|
|
24
|
+
|
|
25
|
+
const op = FunctionModel(Number, Number).return(Number);
|
|
26
|
+
|
|
27
|
+
const add = op(function (a, b) { return a + b; });
|
|
28
|
+
const noop = op(function () { return undefined; });
|
|
29
|
+
const addStr = op(function (a, b) { return String(a) + String(b); });
|
|
30
|
+
|
|
31
|
+
assert.ok(add instanceof Function && add instanceof op, "fn instanceof functionModel and Function");
|
|
32
|
+
|
|
33
|
+
assert.equal(add(15, 25), 40, "valid function model call");
|
|
34
|
+
assert.throws(function () {
|
|
35
|
+
add(15)
|
|
36
|
+
}, /TypeError/, "too few arguments");
|
|
37
|
+
assert.throws(function () {
|
|
38
|
+
noop(15, 25)
|
|
39
|
+
}, /TypeError/, "no return");
|
|
40
|
+
assert.throws(function () {
|
|
41
|
+
addStr(15, 25)
|
|
42
|
+
}, /TypeError/, "incorrect return type");
|
|
43
|
+
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
QUnit.test("object models methods", function (assert) {
|
|
47
|
+
|
|
48
|
+
const Person = ObjectModel({
|
|
49
|
+
name: String,
|
|
50
|
+
age: Number,
|
|
51
|
+
// function without arguments returning a String
|
|
52
|
+
sayMyName: FunctionModel().return(String)
|
|
53
|
+
}).defaultTo({
|
|
54
|
+
sayMyName: function () {
|
|
55
|
+
return "my name is " + this.name;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const greetFnModel = FunctionModel(Person).return(String);
|
|
60
|
+
|
|
61
|
+
Person.prototype.greet = greetFnModel(function (otherguy) {
|
|
62
|
+
return "Hello " + otherguy.name + ", " + this.sayMyName();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
const joe = new Person({ name: "Joe", age: 28 });
|
|
66
|
+
const ann = new Person({ name: "Ann", age: 23 });
|
|
67
|
+
|
|
68
|
+
assert.equal(joe.sayMyName(), "my name is Joe", "valid function model method call 1/2");
|
|
69
|
+
assert.equal(joe.greet(ann), "Hello Ann, my name is Joe", "valid function model method call 2/2");
|
|
70
|
+
|
|
71
|
+
assert.throws(function () {
|
|
72
|
+
joe.greet("dog");
|
|
73
|
+
}, /TypeError/, "invalid argument type");
|
|
74
|
+
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
QUnit.test("default arguments & arguments control", function (assert) {
|
|
78
|
+
|
|
79
|
+
const Calculator = FunctionModel(Number, ["+", "-", "*", "/", undefined], [Number])
|
|
80
|
+
.return(Number);
|
|
81
|
+
|
|
82
|
+
const calc = new Calculator(function (a = 0, operator = '+', b = 1) {
|
|
83
|
+
switch (operator) {
|
|
84
|
+
case "+": return a + b
|
|
85
|
+
case "-": return a - b
|
|
86
|
+
case "*": return a * b
|
|
87
|
+
case "/": return a / b
|
|
88
|
+
}
|
|
89
|
+
return null
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
assert.equal(calc(3, "+"), 4, "default argument value");
|
|
93
|
+
assert.equal(calc(41), 42, "default arguments values");
|
|
94
|
+
assert.throws(function () {
|
|
95
|
+
calc(6, "*", false);
|
|
96
|
+
}, /TypeError/, "invalid argument type");
|
|
97
|
+
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
QUnit.test("other models & objects as arguments", function (assert) {
|
|
101
|
+
|
|
102
|
+
const api = FunctionModel({
|
|
103
|
+
list: ArrayModel(Number),
|
|
104
|
+
op: ["sum", "product"]
|
|
105
|
+
})(function (options) {
|
|
106
|
+
return options.list.reduce(function (a, b) {
|
|
107
|
+
switch (options.op) {
|
|
108
|
+
case "sum":
|
|
109
|
+
return a + b;
|
|
110
|
+
case "product":
|
|
111
|
+
return a * b;
|
|
112
|
+
}
|
|
113
|
+
}, options.op === "product" ? 1 : 0);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
assert.equal(api({ list: [1, 2, 3, 4], op: "sum" }), 10, "FunctionModel object argument 1/5");
|
|
117
|
+
assert.equal(api({ list: [1, 2, 3, 4], op: "product" }), 24, "FunctionModel object argument 2/5");
|
|
118
|
+
assert.throws(function () {
|
|
119
|
+
api({ list: [1, 2, "3", 4], op: "product" });
|
|
120
|
+
}, /TypeError/, "FunctionModel object argument 3/5");
|
|
121
|
+
assert.throws(function () {
|
|
122
|
+
api({ list: [1, 2, 3, 4], op: "divide" });
|
|
123
|
+
}, /TypeError/, "FunctionModel object argument 4/5");
|
|
124
|
+
assert.throws(function () {
|
|
125
|
+
api({ list: [1, 2, 3, 4] });
|
|
126
|
+
}, /TypeError/, "FunctionModel object argument 5/5");
|
|
127
|
+
|
|
128
|
+
assert.ok(FunctionModel() instanceof FunctionModel, "FunctionModel does not throw when receiving no arguments");
|
|
129
|
+
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
QUnit.test("default value", function (assert) {
|
|
133
|
+
|
|
134
|
+
const yell = FunctionModel(String).return(String).defaultTo(s => s.toUpperCase());
|
|
135
|
+
|
|
136
|
+
assert.strictEqual(yell()("yo!"), "YO!", "Function model default value");
|
|
137
|
+
assert.throws(function () {
|
|
138
|
+
yell()(42)
|
|
139
|
+
}, /TypeError.*got Number 42/, "invalid arguments still throws TypeError for defaulted function models");
|
|
140
|
+
|
|
141
|
+
yell.default = function (s) {
|
|
142
|
+
return s.length
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
assert.throws(function () {
|
|
146
|
+
yell()("yo!")
|
|
147
|
+
}, /TypeError.*got Number 3/, "invalid default property still throws TypeError for function models");
|
|
148
|
+
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
QUnit.test("Automatic model casting", function (assert) {
|
|
152
|
+
|
|
153
|
+
const N = ObjectModel({ x: Number, y: [Number] }).defaultTo({ x: 5, y: 7 });
|
|
154
|
+
const F = FunctionModel(N, N).return(N);
|
|
155
|
+
const f = F(function (a, b) { return { x: a.x + b.x, y: a.y + b.y } });
|
|
156
|
+
const returnValue = f({ x: 1 }, { x: 2 });
|
|
157
|
+
|
|
158
|
+
assert.ok(returnValue instanceof N, "test automatic model casting with return value");
|
|
159
|
+
assert.equal(returnValue.x, 3, "test automatic casting with function args 1/2");
|
|
160
|
+
assert.equal(returnValue.y, 14, "test automatic casting with function args 2/2");
|
|
161
|
+
|
|
163
162
|
})
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {expectError, expectType} from 'tsd';
|
|
2
|
+
import { FunctionModel, ObjectModel } from '../types';
|
|
3
|
+
|
|
4
|
+
const F1 = FunctionModel(Number, String).return(Boolean);
|
|
5
|
+
const f1 = F1((n,s) => +s === n);
|
|
6
|
+
|
|
7
|
+
expectType<(a: number, b: string) => boolean>(f1)
|
|
8
|
+
expectType<boolean>(f1(1, "test"))
|
|
9
|
+
|
|
10
|
+
expectError(f1("test", 2))
|
|
11
|
+
|
|
12
|
+
expectError(f1(0))
|
|
13
|
+
|
|
14
|
+
const OM = ObjectModel({ returnValue: Number })
|
|
15
|
+
const F2 = FunctionModel().return(OM)
|
|
16
|
+
const f2 = () => ({ returnValue: 1 })
|
|
17
|
+
expectType<() => { returnValue: number}>(f2)
|
|
18
|
+
expectType<number>(f2().returnValue)
|
package/test/index.cjs
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
QUnit.test("loading module", async t => {
|
|
2
|
-
const globals = await import("../dist/object-model.min.js")
|
|
3
|
-
Object.assign(globalThis, globals)
|
|
4
|
-
t.ok("Model" in globals, "libarary correcty loaded as module");
|
|
5
|
-
});
|
|
6
|
-
|
|
7
|
-
require("./model.spec.cjs")
|
|
8
|
-
require("./basic-model.spec.cjs")
|
|
9
|
-
require("./any-model.spec.cjs")
|
|
10
|
-
require("./object-model.spec.cjs")
|
|
11
|
-
require("./array-model.spec.cjs")
|
|
12
|
-
require("./function-model.spec.cjs")
|
|
13
|
-
require("./map-model.spec.cjs")
|
|
1
|
+
QUnit.test("loading module", async t => {
|
|
2
|
+
const globals = await import("../dist/object-model.min.js")
|
|
3
|
+
Object.assign(globalThis, globals)
|
|
4
|
+
t.ok("Model" in globals, "libarary correcty loaded as module");
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
require("./model.spec.cjs")
|
|
8
|
+
require("./basic-model.spec.cjs")
|
|
9
|
+
require("./any-model.spec.cjs")
|
|
10
|
+
require("./object-model.spec.cjs")
|
|
11
|
+
require("./array-model.spec.cjs")
|
|
12
|
+
require("./function-model.spec.cjs")
|
|
13
|
+
require("./map-model.spec.cjs")
|
|
14
14
|
require("./set-model.spec.cjs")
|