@rootintf/json-msg 1.0.4 → 1.1.1

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.
@@ -142,77 +142,71 @@ const l_= {
142
142
 
143
143
  // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
144
144
  var JSON_Msg = (function() {
145
- function JSON_Msg() {}
146
- JSON_Msg.prototype.merge = function (json) {
147
- const dt_Msg = new Date();
148
- let json_msg = {
149
- id: v4(),
150
- referer: null,
151
- dt: dt_Msg.toISOString(),
152
- };
153
- const instance = Object.create(JSON_Msg.prototype);
154
- Object.assign(instance, json_msg, json);
155
- return instance;
156
- };
157
- JSON_Msg.prototype.validate = function (json) {
158
- const b_valid = schema_Msg(json);
159
- if (!b_valid)
160
- LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors);
161
- return b_valid;
162
- };
163
- // TODO: requires a separate ajv module require, and JTD specific schema
164
- // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
165
- // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
166
- // static parse(json) {
167
- // return this.#ajv_parse_(json);
168
- // }
169
- return new JSON_Msg();
145
+ return {
146
+ merge: function(json) {
147
+ return Object.assign({
148
+ id: v4(),
149
+ referer: null,
150
+ dt: new Date().toISOString()
151
+ }, json || {});
152
+ },
153
+ validate : function (json) {
154
+ const b_valid = schema_Msg(json);
155
+ if (!b_valid)
156
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors);
157
+ return b_valid;
158
+ },
159
+ // TODO: requires a separate ajv module require, and JTD specific schema
160
+ // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
161
+ // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
162
+ // static parse(json) {
163
+ // return this.#ajv_parse_(json);
164
+ // }
165
+ }
170
166
  })();
171
167
 
172
168
  var JSON_Msg_msg = (function() {
173
- function JSON_Msg_msg() {}
174
- JSON_Msg_msg.prototype.create_with_msg_merge = function(_msg, json) {
175
- let json_msg= JSON_Msg.merge(json);
176
- json_msg._msg= _msg;
177
- return json_msg;
178
- };
179
- JSON_Msg_msg.prototype.validate = function(json) {
180
- if (! JSON_Msg.validate(json))
181
- return false;
182
- const b_valid= schema_Msg_msg(json);
183
- if (!b_valid)
184
- LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors);
185
- return b_valid;
186
- };
187
- return new JSON_Msg_msg()
169
+ return {
170
+ create_with_msg_merge: function(_msg, json) {
171
+ return Object.assign(JSON_Msg.merge(json), { _msg });
172
+ },
173
+ validate : function(json) {
174
+ if (! JSON_Msg.validate(json))
175
+ return false;
176
+ const b_valid= schema_Msg_msg(json);
177
+ if (!b_valid)
178
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors);
179
+ return b_valid;
180
+ }
181
+ }
188
182
  })();
189
183
 
190
184
  var JSON_MsgAck_msg = (function() {
191
- function JSON_MsgAck_msg() {}
192
- JSON_MsgAck_msg.prototype.create_with_msgOk = function(_msg, b_ok= true) {
193
- return JSON_Msg_msg.create_with_msg_merge(_msg, {
194
- ack : {
195
- "ok" : b_ok,
196
- },
197
- });
198
- },
199
- JSON_MsgAck_msg.prototype.create_with_msgExpired = function(_msg) {
200
- return JSON_Msg_msg.create_with_msg_merge(_msg, {
201
- ack : {
202
- "ok" : false,
203
- "expired" : true
204
- },
205
- });
206
- },
207
- JSON_MsgAck_msg.prototype.validate = function(json) {
208
- if (! JSON_Msg_msg.validate(json))
209
- return false;
210
- const b_valid= schema_MsgAck_msg(json);
211
- if (!b_valid)
212
- LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors);
213
- return b_valid;
214
- };
215
- return new JSON_MsgAck_msg();
185
+ return {
186
+ create_with_msgOk : function(_msg, b_ok= true) {
187
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
188
+ ack : {
189
+ "ok" : b_ok,
190
+ },
191
+ });
192
+ },
193
+ create_with_msgExpired : function(_msg) {
194
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
195
+ ack : {
196
+ "ok" : false,
197
+ "expired" : true
198
+ },
199
+ });
200
+ },
201
+ validate : function(json) {
202
+ if (! JSON_Msg_msg.validate(json))
203
+ return false;
204
+ const b_valid= schema_MsgAck_msg(json);
205
+ if (!b_valid)
206
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors);
207
+ return b_valid;
208
+ }
209
+ }
216
210
  })();
217
211
 
218
212
  exports.JSON_Msg = JSON_Msg;
@@ -140,77 +140,71 @@ const l_= {
140
140
 
141
141
  // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
142
142
  var JSON_Msg = (function() {
143
- function JSON_Msg() {}
144
- JSON_Msg.prototype.merge = function (json) {
145
- const dt_Msg = new Date();
146
- let json_msg = {
147
- id: v4(),
148
- referer: null,
149
- dt: dt_Msg.toISOString(),
150
- };
151
- const instance = Object.create(JSON_Msg.prototype);
152
- Object.assign(instance, json_msg, json);
153
- return instance;
154
- };
155
- JSON_Msg.prototype.validate = function (json) {
156
- const b_valid = schema_Msg(json);
157
- if (!b_valid)
158
- LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors);
159
- return b_valid;
160
- };
161
- // TODO: requires a separate ajv module require, and JTD specific schema
162
- // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
163
- // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
164
- // static parse(json) {
165
- // return this.#ajv_parse_(json);
166
- // }
167
- return new JSON_Msg();
143
+ return {
144
+ merge: function(json) {
145
+ return Object.assign({
146
+ id: v4(),
147
+ referer: null,
148
+ dt: new Date().toISOString()
149
+ }, json || {});
150
+ },
151
+ validate : function (json) {
152
+ const b_valid = schema_Msg(json);
153
+ if (!b_valid)
154
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors);
155
+ return b_valid;
156
+ },
157
+ // TODO: requires a separate ajv module require, and JTD specific schema
158
+ // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
159
+ // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
160
+ // static parse(json) {
161
+ // return this.#ajv_parse_(json);
162
+ // }
163
+ }
168
164
  })();
169
165
 
170
166
  var JSON_Msg_msg = (function() {
171
- function JSON_Msg_msg() {}
172
- JSON_Msg_msg.prototype.create_with_msg_merge = function(_msg, json) {
173
- let json_msg= JSON_Msg.merge(json);
174
- json_msg._msg= _msg;
175
- return json_msg;
176
- };
177
- JSON_Msg_msg.prototype.validate = function(json) {
178
- if (! JSON_Msg.validate(json))
179
- return false;
180
- const b_valid= schema_Msg_msg(json);
181
- if (!b_valid)
182
- LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors);
183
- return b_valid;
184
- };
185
- return new JSON_Msg_msg()
167
+ return {
168
+ create_with_msg_merge: function(_msg, json) {
169
+ return Object.assign(JSON_Msg.merge(json), { _msg });
170
+ },
171
+ validate : function(json) {
172
+ if (! JSON_Msg.validate(json))
173
+ return false;
174
+ const b_valid= schema_Msg_msg(json);
175
+ if (!b_valid)
176
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors);
177
+ return b_valid;
178
+ }
179
+ }
186
180
  })();
187
181
 
188
182
  var JSON_MsgAck_msg = (function() {
189
- function JSON_MsgAck_msg() {}
190
- JSON_MsgAck_msg.prototype.create_with_msgOk = function(_msg, b_ok= true) {
191
- return JSON_Msg_msg.create_with_msg_merge(_msg, {
192
- ack : {
193
- "ok" : b_ok,
194
- },
195
- });
196
- },
197
- JSON_MsgAck_msg.prototype.create_with_msgExpired = function(_msg) {
198
- return JSON_Msg_msg.create_with_msg_merge(_msg, {
199
- ack : {
200
- "ok" : false,
201
- "expired" : true
202
- },
203
- });
204
- },
205
- JSON_MsgAck_msg.prototype.validate = function(json) {
206
- if (! JSON_Msg_msg.validate(json))
207
- return false;
208
- const b_valid= schema_MsgAck_msg(json);
209
- if (!b_valid)
210
- LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors);
211
- return b_valid;
212
- };
213
- return new JSON_MsgAck_msg();
183
+ return {
184
+ create_with_msgOk : function(_msg, b_ok= true) {
185
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
186
+ ack : {
187
+ "ok" : b_ok,
188
+ },
189
+ });
190
+ },
191
+ create_with_msgExpired : function(_msg) {
192
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
193
+ ack : {
194
+ "ok" : false,
195
+ "expired" : true
196
+ },
197
+ });
198
+ },
199
+ validate : function(json) {
200
+ if (! JSON_Msg_msg.validate(json))
201
+ return false;
202
+ const b_valid= schema_MsgAck_msg(json);
203
+ if (!b_valid)
204
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors);
205
+ return b_valid;
206
+ }
207
+ }
214
208
  })();
215
209
 
216
210
  export { JSON_Msg, JSON_MsgAck_msg, JSON_Msg_msg };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rootintf/json-msg",
3
- "version": "1.0.4",
3
+ "version": "1.1.1",
4
4
  "description": "",
5
5
  "main": "dist/json-msg.cjs.js",
6
6
  "browser": "dist/json-msg.es.mjs",
@@ -14,77 +14,71 @@ const l_= {
14
14
 
15
15
  // "module" pattern to avoid "static" and "#" private : https://stackoverflow.com/a/1479341/298545
16
16
  var JSON_Msg = (function() {
17
- function JSON_Msg() {}
18
- JSON_Msg.prototype.merge = function (json) {
19
- const dt_Msg = new Date();
20
- let json_msg = {
21
- id: uuidv4(),
22
- referer: null,
23
- dt: dt_Msg.toISOString(),
24
- }
25
- const instance = Object.create(JSON_Msg.prototype);
26
- Object.assign(instance, json_msg, json);
27
- return instance;
28
- };
29
- JSON_Msg.prototype.validate = function (json) {
30
- const b_valid = schema_Msg(json);
31
- if (!b_valid)
32
- LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors)
33
- return b_valid;
34
- };
35
- // TODO: requires a separate ajv module require, and JTD specific schema
36
- // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
37
- // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
38
- // static parse(json) {
39
- // return this.#ajv_parse_(json);
40
- // }
41
- return new JSON_Msg();
17
+ return {
18
+ merge: function(json) {
19
+ return Object.assign({
20
+ id: uuidv4(),
21
+ referer: null,
22
+ dt: new Date().toISOString()
23
+ }, json || {});
24
+ },
25
+ validate : function (json) {
26
+ const b_valid = schema_Msg(json);
27
+ if (!b_valid)
28
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg: validate: ', schema_Msg.errors)
29
+ return b_valid;
30
+ },
31
+ // TODO: requires a separate ajv module require, and JTD specific schema
32
+ // https://ajv.js.org/guide/getting-started.html#parsing-and-serializing-json
33
+ // static #ajv_parse_= ajv.compileParser(this.#schema_json_Msg);
34
+ // static parse(json) {
35
+ // return this.#ajv_parse_(json);
36
+ // }
37
+ }
42
38
  })();
43
39
 
44
40
  var JSON_Msg_msg = (function() {
45
- function JSON_Msg_msg() {}
46
- JSON_Msg_msg.prototype.create_with_msg_merge = function(_msg, json) {
47
- let json_msg= JSON_Msg.merge(json);
48
- json_msg._msg= _msg;
49
- return json_msg;
50
- };
51
- JSON_Msg_msg.prototype.validate = function(json) {
52
- if (! JSON_Msg.validate(json))
53
- return false;
54
- const b_valid= schema_Msg_msg(json);
55
- if (!b_valid)
56
- LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors)
57
- return b_valid;
41
+ return {
42
+ create_with_msg_merge: function(_msg, json) {
43
+ return Object.assign(JSON_Msg.merge(json), { _msg });
44
+ },
45
+ validate : function(json) {
46
+ if (! JSON_Msg.validate(json))
47
+ return false;
48
+ const b_valid= schema_Msg_msg(json);
49
+ if (!b_valid)
50
+ LOGR_.log(l_.VALIDATION, 'JSON_Msg_msg: validate: ', schema_Msg_msg.errors)
51
+ return b_valid;
52
+ }
58
53
  }
59
- return new JSON_Msg_msg()
60
54
  })();
61
55
 
62
56
  var JSON_MsgAck_msg = (function() {
63
- function JSON_MsgAck_msg() {}
64
- JSON_MsgAck_msg.prototype.create_with_msgOk = function(_msg, b_ok= true) {
65
- return JSON_Msg_msg.create_with_msg_merge(_msg, {
66
- ack : {
67
- "ok" : b_ok,
68
- },
69
- });
70
- },
71
- JSON_MsgAck_msg.prototype.create_with_msgExpired = function(_msg) {
72
- return JSON_Msg_msg.create_with_msg_merge(_msg, {
73
- ack : {
74
- "ok" : false,
75
- "expired" : true
76
- },
77
- });
78
- },
79
- JSON_MsgAck_msg.prototype.validate = function(json) {
80
- if (! JSON_Msg_msg.validate(json))
81
- return false;
82
- const b_valid= schema_MsgAck_msg(json);
83
- if (!b_valid)
84
- LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors)
85
- return b_valid;
57
+ return {
58
+ create_with_msgOk : function(_msg, b_ok= true) {
59
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
60
+ ack : {
61
+ "ok" : b_ok,
62
+ },
63
+ });
64
+ },
65
+ create_with_msgExpired : function(_msg) {
66
+ return JSON_Msg_msg.create_with_msg_merge(_msg, {
67
+ ack : {
68
+ "ok" : false,
69
+ "expired" : true
70
+ },
71
+ });
72
+ },
73
+ validate : function(json) {
74
+ if (! JSON_Msg_msg.validate(json))
75
+ return false;
76
+ const b_valid= schema_MsgAck_msg(json);
77
+ if (!b_valid)
78
+ LOGR_.log(l_.VALIDATION, 'JSON_MsgAck: validate: ', schema_MsgAck_msg.errors)
79
+ return b_valid;
80
+ }
86
81
  }
87
- return new JSON_MsgAck_msg();
88
82
  })();
89
83
 
90
84
  //-------------------------------------------------------------------------------------------------
@@ -42,13 +42,10 @@ describe("JSON_Msg validation", function () {
42
42
  it("JSON_Msg.merge({ foo: \"bar\" })", function () {
43
43
  const merged_msg = JSON_Msg.merge({ foo: "bar" });
44
44
 
45
- expect( merged_msg.constructor.name ).toBe('JSON_Msg');
46
45
  expect( merged_msg.foo ).toBe('bar');
47
46
  expect( JSON_Msg.validate(merged_msg) ).toBe(true);
48
47
  });
49
48
 
50
- // console.log(); // true
51
-
52
49
  it("JSON_Msg.validate(json_msgAckWarp)", function () {
53
50
  expect( JSON_Msg.validate(json_msgAckWarp) ).toBe(true);
54
51
  });