lightview 1.6.3-b → 1.6.6-b
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/README.md +2 -2
- package/components/chart.html +6 -5
- package/components/gauge.html +1 -1
- package/examples/chart.html +11 -9
- package/examples/foreign.html +27 -13
- package/examples/forgeinform.html +29 -8
- package/examples/form.html +1 -1
- package/examples/medium/remote.html +59 -0
- package/examples/remote.json +1 -1
- package/examples/types.html +1 -0
- package/examples/xor.html +1 -1
- package/jest.config.json +1 -1
- package/lightview.js +287 -209
- package/package.json +1 -1
- package/test/basic.html +1 -1
- package/test/extended.html +32 -0
- package/test/extended.test.mjs +448 -0
- package/types.js +62 -53
package/types.js
CHANGED
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
const toJSON = (value) => {
|
|
2
|
+
if([-Infinity,Infinity].includes(value)) return `@${value}`;
|
|
3
|
+
if(typeof(value)==="number" && isNaN(value)) return "@NaN";
|
|
4
|
+
if(value && typeof(value)==="object") {
|
|
5
|
+
return Object.entries(value)
|
|
6
|
+
.reduce((json,[key,value]) => {
|
|
7
|
+
if(value && typeof(value)==="object" && value.toJSON) value = value.toJSON();
|
|
8
|
+
json[key] = toJSON(value);
|
|
9
|
+
return json;
|
|
10
|
+
},Array.isArray(value) ? [] : {})
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
};
|
|
14
|
+
function reviver(property,value) {
|
|
15
|
+
if(value==="@-Infinity") return -Infinity;
|
|
16
|
+
if(value==="@Infinity") return Infinity;
|
|
17
|
+
if(value==="@NaN") return NaN;
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
|
|
1
21
|
function ValidityState(options) {
|
|
2
22
|
if(!this || !(this instanceof ValidityState)) return new ValidityState(options);
|
|
3
23
|
Object.assign(this,{
|
|
@@ -15,9 +35,17 @@ function ValidityState(options) {
|
|
|
15
35
|
},options);
|
|
16
36
|
}
|
|
17
37
|
|
|
38
|
+
function DataType(options) {
|
|
39
|
+
if(!this || !(this instanceof DataType)) return new DataType(options);
|
|
40
|
+
Object.assign(this,options);
|
|
41
|
+
}
|
|
42
|
+
DataType.prototype.toJSON = function() {
|
|
43
|
+
return toJSON(this);
|
|
44
|
+
}
|
|
45
|
+
|
|
18
46
|
const tryParse = (value) => {
|
|
19
47
|
try {
|
|
20
|
-
return JSON.parse(value+"")
|
|
48
|
+
return JSON.parse(value+"",reviver)
|
|
21
49
|
} catch(e) {
|
|
22
50
|
|
|
23
51
|
}
|
|
@@ -25,7 +53,7 @@ const tryParse = (value) => {
|
|
|
25
53
|
|
|
26
54
|
const ifInvalid = (variable) => {
|
|
27
55
|
variable.validityState.type = typeof(variable.type)==="string" ? variable.type : variable.type.type;
|
|
28
|
-
throw new TypeError(JSON.stringify(variable));
|
|
56
|
+
throw new TypeError(JSON.stringify(DataType(variable)));
|
|
29
57
|
// or could return existing value variable.value
|
|
30
58
|
// or could return nothing
|
|
31
59
|
}
|
|
@@ -80,7 +108,7 @@ const validateArray = function(value,variable) {
|
|
|
80
108
|
}
|
|
81
109
|
return this.whenInvalid(variable,value);
|
|
82
110
|
}
|
|
83
|
-
const array = ({coerce=false, required = false,whenInvalid = ifInvalid,maxlength=Infinity,minlength=0,...rest}) => {
|
|
111
|
+
const array = ({coerce=false, required = false,whenInvalid = ifInvalid,maxlength=Infinity,minlength=0,...rest}={}) => {
|
|
84
112
|
if(typeof(coerce)!=="boolean") throw new TypeError(`coerce, ${JSON.stringify(coerce)}, must be a boolean`);
|
|
85
113
|
if(typeof(required)!=="boolean") throw new TypeError(`required, ${JSON.stringify(required)}, must be a boolean`);
|
|
86
114
|
if(typeof(whenInvalid)!=="function") throw new TypeError(`whenInvalid, ${whenInvalid}, must be a function`);
|
|
@@ -98,9 +126,7 @@ const array = ({coerce=false, required = false,whenInvalid = ifInvalid,maxlength
|
|
|
98
126
|
validate: validateArray
|
|
99
127
|
}
|
|
100
128
|
}
|
|
101
|
-
|
|
102
|
-
array.coerce = false;
|
|
103
|
-
array.required = false;
|
|
129
|
+
|
|
104
130
|
|
|
105
131
|
const validateBoolean = function(value,variable) {
|
|
106
132
|
if(value===undefined && variable.value===undefined) {
|
|
@@ -110,7 +136,7 @@ const validateBoolean = function(value,variable) {
|
|
|
110
136
|
if(this.required && value==null) {
|
|
111
137
|
variable.validityState = ValidityState({valueMissing: true});
|
|
112
138
|
} else {
|
|
113
|
-
const result =
|
|
139
|
+
const result = this.coerce ? tryParse(value) : value;
|
|
114
140
|
if(typeof(result)!=="boolean") {
|
|
115
141
|
variable.validityState = ValidityState({typeMismatch: true, value});
|
|
116
142
|
} else {
|
|
@@ -120,7 +146,7 @@ const validateBoolean = function(value,variable) {
|
|
|
120
146
|
}
|
|
121
147
|
return this.whenInvalid(variable,value);
|
|
122
148
|
}
|
|
123
|
-
const boolean = ({coerce=false,required=false, whenInvalid = ifInvalid,...rest}) =>{
|
|
149
|
+
const boolean = ({coerce=false,required=false, whenInvalid = ifInvalid,...rest}={}) =>{
|
|
124
150
|
if(typeof(coerce)!=="boolean") throw new TypeError(`coerce, ${JSON.stringify(coerce)}, must be a boolean`);
|
|
125
151
|
if(typeof(required)!=="boolean") throw new TypeError(`required, ${JSON.stringify(required)}, must be a boolean`);
|
|
126
152
|
if(typeof(whenInvalid)!=="function") throw new TypeError(`whenInvalid, ${whenInvalid}, must be a function`);
|
|
@@ -134,9 +160,7 @@ const boolean = ({coerce=false,required=false, whenInvalid = ifInvalid,...rest})
|
|
|
134
160
|
validate: validateBoolean
|
|
135
161
|
}
|
|
136
162
|
}
|
|
137
|
-
|
|
138
|
-
boolean.coerce = false;
|
|
139
|
-
boolean.required = false;
|
|
163
|
+
|
|
140
164
|
|
|
141
165
|
const validateNumber = function(value,variable) {
|
|
142
166
|
if(value===undefined && variable.value===undefined) {
|
|
@@ -148,13 +172,13 @@ const validateNumber = function(value,variable) {
|
|
|
148
172
|
const result = this.coerce ? tryParse(value) : value;
|
|
149
173
|
if(typeof(result)!=="number") {
|
|
150
174
|
variable.validityState = ValidityState({typeMismatch:true,value});
|
|
151
|
-
} else if(isNaN(result) && !allowNaN) {
|
|
175
|
+
} else if(isNaN(result) && !this.allowNaN) {
|
|
152
176
|
variable.validityState = ValidityState({badInput:true,value});
|
|
153
|
-
} else if(result<this.min) {
|
|
177
|
+
} else if(this.min!=null && result<this.min) {
|
|
154
178
|
variable.validityState = ValidityState({rangeUnderflow:true,value});
|
|
155
|
-
} else if(result>this.max) {
|
|
179
|
+
} else if(this.max!=null && result>this.max) {
|
|
156
180
|
variable.validityState = ValidityState({rangeOverflow:true,value});
|
|
157
|
-
} else if((result % this.step)!==0) {
|
|
181
|
+
} else if(this.step!==null && (result % this.step)!==0) {
|
|
158
182
|
variable.validityState = ValidityState({rangeUnderflow:true,value});
|
|
159
183
|
} else {
|
|
160
184
|
variable.validityState = ValidityState({valid:true});
|
|
@@ -163,13 +187,13 @@ const validateNumber = function(value,variable) {
|
|
|
163
187
|
}
|
|
164
188
|
return this.whenInvalid(variable,value);
|
|
165
189
|
}
|
|
166
|
-
const number = ({coerce=false,required = false,whenInvalid = ifInvalid,min=-Infinity,max=Infinity,step = 1,allowNaN = true,...rest}) => {
|
|
190
|
+
const number = ({coerce=false,required = false,whenInvalid = ifInvalid,min=-Infinity,max=Infinity,step = 1,allowNaN = true,...rest}={}) => {
|
|
167
191
|
if(typeof(coerce)!=="boolean") throw new TypeError(`coerce, ${JSON.stringify(coerce)}, must be a boolean`);
|
|
168
192
|
if(typeof(required)!=="boolean") throw new TypeError(`required, ${JSON.stringify(required)}, must be a boolean`);
|
|
169
193
|
if(typeof(whenInvalid)!=="function") throw new TypeError(`whenInvalid, ${whenInvalid}, must be a function`);
|
|
170
|
-
if(typeof(min)!=="number") throw new TypeError(`min, ${JSON.stringify(min)}, must be a number`);
|
|
171
|
-
if(typeof(max)!=="number") throw new TypeError(`max, ${JSON.stringify(max)}, must be a number`);
|
|
172
|
-
if(typeof(step)!=="number") throw new TypeError(`step, ${JSON.stringify(step)}, must be a number`);
|
|
194
|
+
if(min!=null && typeof(min)!=="number") throw new TypeError(`min, ${JSON.stringify(min)}, must be a number`);
|
|
195
|
+
if(max!=null && typeof(max)!=="number") throw new TypeError(`max, ${JSON.stringify(max)}, must be a number`);
|
|
196
|
+
if(step!=null && typeof(step)!=="number") throw new TypeError(`step, ${JSON.stringify(step)}, must be a number`);
|
|
173
197
|
if(typeof(allowNaN)!=="boolean") throw new TypeError(`step, ${JSON.stringify(allowNaN)}, must be a boolean`);
|
|
174
198
|
if(rest.default!==undefined && typeof(rest.default)!=="number") throw new TypeError(`default, ${rest.default}, must be a number`);
|
|
175
199
|
return {
|
|
@@ -185,13 +209,7 @@ const number = ({coerce=false,required = false,whenInvalid = ifInvalid,min=-Infi
|
|
|
185
209
|
validate: validateNumber
|
|
186
210
|
}
|
|
187
211
|
}
|
|
188
|
-
|
|
189
|
-
number.min = -Infinity;
|
|
190
|
-
number.max = Infinity;
|
|
191
|
-
number.coerce = false;
|
|
192
|
-
number.required = false;
|
|
193
|
-
number.allwNaN = true;
|
|
194
|
-
number.step = 1;
|
|
212
|
+
|
|
195
213
|
|
|
196
214
|
const validateObject = function(value,variable) {
|
|
197
215
|
if(value===undefined && variable.value===undefined) {
|
|
@@ -210,7 +228,7 @@ const validateObject = function(value,variable) {
|
|
|
210
228
|
}
|
|
211
229
|
return this.whenInvalid(variable,value);
|
|
212
230
|
}
|
|
213
|
-
const object = ({coerce=false, required = false,whenInvalid = ifInvalid,...rest}) => {
|
|
231
|
+
const object = ({coerce=false, required = false,whenInvalid = ifInvalid,...rest}={}) => {
|
|
214
232
|
if(typeof(coerce)!=="boolean") throw new TypeError(`coerce, ${JSON.stringify(coerce)}, must be a boolean`);
|
|
215
233
|
if(typeof(required)!=="boolean") throw new TypeError(`required, ${JSON.stringify(required)}, must be a boolean`);
|
|
216
234
|
if(typeof(whenInvalid)!=="function") throw new TypeError(`whenInvalid, ${whenInvalid}, must be a function`);
|
|
@@ -224,9 +242,7 @@ const object = ({coerce=false, required = false,whenInvalid = ifInvalid,...rest}
|
|
|
224
242
|
validate: validateObject
|
|
225
243
|
}
|
|
226
244
|
}
|
|
227
|
-
|
|
228
|
-
object.coerce = false;
|
|
229
|
-
object.required = false;
|
|
245
|
+
|
|
230
246
|
|
|
231
247
|
const validateString = function(value,variable) {
|
|
232
248
|
if(value===undefined && variable.value===undefined) {
|
|
@@ -249,7 +265,7 @@ const validateString = function(value,variable) {
|
|
|
249
265
|
}
|
|
250
266
|
return this.whenInvalid(variable,value);
|
|
251
267
|
}
|
|
252
|
-
const string = ({coerce=false, required = false,whenInvalid = ifInvalid, maxlength = Infinity, minlength = 0, pattern
|
|
268
|
+
const string = ({coerce=false, required = false,whenInvalid = ifInvalid, maxlength = Infinity, minlength = 0, pattern,...rest}={}) => {
|
|
253
269
|
if(typeof(coerce)!=="boolean") throw new TypeError(`coerce, ${JSON.stringify(coerce)}, must be a boolean`);
|
|
254
270
|
if(typeof(required)!=="boolean") throw new TypeError(`required, ${JSON.stringify(required)}, must be a boolean`);
|
|
255
271
|
if(typeof(whenInvalid)!=="function") throw new TypeError(`whenInvalid, ${whenInvalid}, must be a function`);
|
|
@@ -268,11 +284,7 @@ const string = ({coerce=false, required = false,whenInvalid = ifInvalid, maxleng
|
|
|
268
284
|
validate: validateString
|
|
269
285
|
}
|
|
270
286
|
}
|
|
271
|
-
|
|
272
|
-
string.coerce = false;
|
|
273
|
-
string.required = false;
|
|
274
|
-
string.maxlength = Infinity;
|
|
275
|
-
string.minlength = 0;
|
|
287
|
+
|
|
276
288
|
|
|
277
289
|
const validateSymbol = function(value,variable) {
|
|
278
290
|
if(value===undefined && variable.value===undefined) {
|
|
@@ -291,7 +303,7 @@ const validateSymbol = function(value,variable) {
|
|
|
291
303
|
}
|
|
292
304
|
return this.whenInvalid(variable,value);
|
|
293
305
|
}
|
|
294
|
-
const symbol = ({coerce=false,required=false, whenInvalid = ifInvalid,...rest}) =>{
|
|
306
|
+
const symbol = ({coerce=false,required=false, whenInvalid = ifInvalid,...rest}={}) =>{
|
|
295
307
|
if(typeof(coerce)!=="boolean") throw new TypeError(`coerce, ${JSON.stringify(coerce)}, must be a boolean`);
|
|
296
308
|
if(typeof(required)!=="boolean") throw new TypeError(`required, ${JSON.stringify(required)}, must be a boolean`);
|
|
297
309
|
if(typeof(whenInvalid)!=="function") throw new TypeError(`whenInvalid, ${whenInvalid}, must be a function`);
|
|
@@ -305,9 +317,6 @@ const symbol = ({coerce=false,required=false, whenInvalid = ifInvalid,...rest})
|
|
|
305
317
|
validate: validateSymbol
|
|
306
318
|
}
|
|
307
319
|
}
|
|
308
|
-
symbol.validate = validateSymbol;
|
|
309
|
-
symbol.coerce = false;
|
|
310
|
-
symbol.required = false;
|
|
311
320
|
|
|
312
321
|
const remoteProxy = ({json, variable,config, reactive, component}) => {
|
|
313
322
|
const type = typeof (config);
|
|
@@ -394,7 +403,7 @@ const put = (href,variable) => {
|
|
|
394
403
|
})
|
|
395
404
|
}
|
|
396
405
|
|
|
397
|
-
const handleRemote = async ({variable,
|
|
406
|
+
const handleRemote = async ({variable, functionalType, config=functionalType, component},doput) => {
|
|
398
407
|
const type = typeof (config);
|
|
399
408
|
let value;
|
|
400
409
|
if (type === "string") {
|
|
@@ -404,20 +413,20 @@ const handleRemote = async ({variable, config, reactive, component},doput) => {
|
|
|
404
413
|
: get(href,variable));
|
|
405
414
|
if(variable.value===undefined) variable.value = value; // do not await here
|
|
406
415
|
} else if (remote && type === "object") {
|
|
407
|
-
let href;
|
|
408
416
|
if(!config.path) config.path = `./${variable.name}`;
|
|
409
|
-
if(config.path)
|
|
417
|
+
if(config.path.endsWith("/")) config.path = `${config.path}${variable.name}`;
|
|
418
|
+
const href = new URL(config.path,window.location.href).href;
|
|
410
419
|
if(!config.get || !config.put) {
|
|
411
420
|
if(!href) throw new Error(`A remote path is required if no put function is provided for remote data`)
|
|
412
421
|
if(!config.get) config.get = get;
|
|
413
|
-
if(!config.put && reactive) config.put = put;
|
|
422
|
+
if(!config.put && variable.reactive) config.put = put;
|
|
414
423
|
}
|
|
415
424
|
value = (doput
|
|
416
425
|
? config.put(href,variable)
|
|
417
426
|
: config.get(href,variable));
|
|
418
427
|
if(config.ttl && !doput && !config.intervalId) {
|
|
419
428
|
config.intervalId = setInterval(async () => {
|
|
420
|
-
await handleRemote({variable, config,
|
|
429
|
+
await handleRemote({variable, config, component});
|
|
421
430
|
//schedule();
|
|
422
431
|
},config.ttl);
|
|
423
432
|
}
|
|
@@ -425,13 +434,11 @@ const handleRemote = async ({variable, config, reactive, component},doput) => {
|
|
|
425
434
|
}
|
|
426
435
|
if(value) {
|
|
427
436
|
const json = await value;
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
}
|
|
434
|
-
//})
|
|
437
|
+
if (json && typeof (json) === "object" && variable.reactive) {
|
|
438
|
+
variable.value = remoteProxy({json, variable,config, component});
|
|
439
|
+
} else {
|
|
440
|
+
component.setVariableValue(variable.name,json);
|
|
441
|
+
}
|
|
435
442
|
}
|
|
436
443
|
}
|
|
437
444
|
|
|
@@ -442,4 +449,6 @@ const remote = (config) => {
|
|
|
442
449
|
}
|
|
443
450
|
}
|
|
444
451
|
|
|
445
|
-
|
|
452
|
+
const remoteGenerator = handleRemote;
|
|
453
|
+
|
|
454
|
+
export {ValidityState,any,array,boolean,number,object,remote,remoteGenerator,string,symbol,reviver}
|