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