-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathOperand.js
More file actions
748 lines (657 loc) · 20.7 KB
/
Operand.js
File metadata and controls
748 lines (657 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
import { OperandType } from './Enums/index.js';
import { JsonMapper } from './LitJson/JsonMapper.js';
import { JsonData } from './LitJson/JsonData.js';
import { FunctionUtil } from './Internals/Functions/FunctionUtil.js';
import { MyDate } from './Internals/MyDate.js';
/**
* 操作数
*/
export class Operand {
/**
* 版本号
*/
static Version;
/**
* True
*/
static True;
/**
* False
*/
static False;
/**
* One
*/
static One;
/**
* Zero
*/
static Zero;
/**
* 是否为空值
*/
get IsNull() { return false; }
/**
* 是否数字
*/
get IsNumber() { return false; }
/**
* 是否字符串
*/
get IsText() { return false; }
/**
* 是否布尔值
*/
get IsBoolean() { return false; }
/**
* 是否数组
*/
get IsArray() { return false; }
/**
* 是否日期
*/
get IsDate() { return false; }
/**
* 是否Json对象
*/
get IsJson() { return false; }
/**
* 是否Json数组
*/
get IsArrayJson() { return false; }
/**
* 是否出错
*/
get IsError() { return false; }
/**
* 错误信息
*/
get ErrorMsg() { return null; }
/**
* 操作数类型
*/
get Type() { throw new Error('FIXME'); }
/**
* 数字值
*/
get NumberValue() { throw new Error('FIXME'); }
/**
* double值
*/
get DoubleValue() { throw new Error('FIXME'); }
/**
* int值
*/
get IntValue() { throw new Error('FIXME'); }
/**
* long值
*/
get LongValue() { throw new Error('FIXME'); }
/**
* 字符串值
*/
get TextValue() { throw new Error('FIXME'); }
/**
* 布尔值
*/
get BooleanValue() { throw new Error('FIXME'); }
/**
* 数组值
*/
get ArrayValue() { throw new Error('FIXME'); }
get JsonValue() { throw new Error('FIXME'); }
/**
* 时间值
*/
get DateValue() { throw new Error('FIXME'); }
/**
* 创建操作数
*/
static Create(obj) {
if (obj instanceof Operand) {
return obj;
} else if (typeof obj === 'boolean') {
return obj ? Operand.True : Operand.False;
} else if (typeof obj === 'number') {
return new OperandDouble(obj);
} else if (typeof obj === 'string') {
if (obj === null) {
return Operand.CreateNull();
}
return new OperandString(obj);
} else if (obj instanceof MyDate) {
return new OperandMyDate(obj);
} else if (obj instanceof JsonData) {
return new OperandJson(obj);
} else if (Array.isArray(obj)) {
let arr=[];
for (let index = 0; index < obj.length; index++) {
let element =Operand.Create(obj[index]);
arr.push(element);
}
return new OperandArray(arr);
}
return Operand.CreateNull();
}
/**
* 创建操作数
*/
static CreateJson(txt) {
if ((txt.startsWith('{') && txt.endsWith('}')) || (txt.startsWith('[') && txt.endsWith(']'))) {
try {
let json = JsonMapper.toObject(txt);
return Operand.Create(json);
} catch (e) { }
}
return Operand.Error("Convert to json error!");
}
/**
* 创建操作数
*/
static Error(msg, ...args) {
if (args.length > 0) {
msg = msg.replace(/\{\d+\}/g, (match, index) => {
let i = parseInt(match.substring(1, match.length - 1));
return args[i] !== undefined ? args[i] : match;
});
}
return new OperandError(msg);
}
/**
* 创建操作数
*/
static CreateNull() {
return new OperandNull();
}
/**
* 转数值类型
*/
ToNumber(errorMessage = null, ...args) {
if (args.length > 0) {
errorMessage = errorMessage.replace(/\{\d+\}/g, (match, index) => {
let i = parseInt(match.substring(1, match.length - 1));
return args[i] !== undefined ? args[i] : match;
});
}
return Operand.Error(errorMessage ?? "Convert to number error!");
}
/**
* 转bool类型
*/
ToBoolean(errorMessage = null, ...args) {
if (args.length > 0) {
errorMessage = errorMessage.replace(/\{\d+\}/g, (match, index) => {
let i = parseInt(match.substring(1, match.length - 1));
return args[i] !== undefined ? args[i] : match;
});
}
return Operand.Error(errorMessage ?? "Convert to bool error!");
}
/**
* 转string类型
*/
ToText(errorMessage = null, ...args) {
if (args.length > 0) {
errorMessage = errorMessage.replace(/\{\d+\}/g, (match, index) => {
let i = parseInt(match.substring(1, match.length - 1));
return args[i] !== undefined ? args[i] : match;
});
}
return Operand.Error(errorMessage ?? "Convert to string error!");
}
/**
* 转MyDate类型
*/
ToMyDate(errorMessage = null, ...args) {
if (args.length > 0) {
errorMessage = errorMessage.replace(/\{\d+\}/g, (match, index) => {
let i = parseInt(match.substring(1, match.length - 1));
return args[i] !== undefined ? args[i] : match;
});
}
return Operand.Error(errorMessage ?? "Convert to date error!");
}
/**
* 转Array类型
*/
ToArray(errorMessage = null, ...args) {
if (args.length > 0) {
errorMessage = errorMessage.replace(/\{\d+\}/g, (match, index) => {
let i = parseInt(match.substring(1, match.length - 1));
return args[i] !== undefined ? args[i] : match;
});
}
return Operand.Error(errorMessage ?? "Convert to array error!");
}
/**
* 转Json类型
*/
ToJson(errorMessage = null) {
return Operand.Error(errorMessage ?? "Convert to json error!");
}
}
class OperandDouble extends Operand {
constructor(obj) {
super();
this._value = obj;
}
get IsNumber() { return true; }
get Type() { return OperandType.NUMBER; }
get IntValue() { return Math.floor(this._value); }
get NumberValue() { return this._value; }
get LongValue() { return Math.floor(this._value); }
get DoubleValue() { return this._value; }
ToNumber(errorMessage) { return this; }
ToNumber(errorMessage, ...args) { return this; }
ToBoolean(errorMessage) {
if (this._value === 0) {
return Operand.False;
} else if (this._value === 1) {
return Operand.True;
}
return super.ToBoolean(errorMessage);
}
ToBoolean(errorMessage, ...args) {
if (this._value === 0) {
return Operand.False;
} else if (this._value === 1) {
return Operand.True;
}
return super.ToBoolean(errorMessage, ...args);
}
ToText(errorMessage) { return Operand.Create(this.DoubleValue.toString()); }
ToText(errorMessage, ...args) { return Operand.Create(this.DoubleValue.toString()); }
toString() { return this.DoubleValue.toString(); }
}
class OperandBoolean extends Operand {
constructor(obj) {
super();
this._value = obj;
}
get IsBoolean() { return true; }
get Type() { return OperandType.BOOLEAN; }
get BooleanValue() { return this._value; }
ToNumber(errorMessage) { return this._value ? Operand.One : Operand.Zero; }
ToNumber(errorMessage, ...args) {
return this._value ? Operand.One : Operand.Zero;
}
ToBoolean(errorMessage) { return this; }
ToBoolean(errorMessage, ...args) { return this; }
ToText(errorMessage) { return Operand.Create(this._value ? "TRUE" : "FALSE"); }
ToText(errorMessage, ...args) { return Operand.Create(this._value ? "TRUE" : "FALSE"); }
toString() { return this._value ? "true" : "false"; }
}
class OperandString extends Operand {
constructor(obj) {
super();
this._value = obj;
}
get IsText() { return true; }
get Type() { return OperandType.TEXT; }
get TextValue() { return this._value; }
ToNumber(errorMessage) {
if (this.TextValue.indexOf('.') === -1) {
let num = parseInt(this.TextValue);
if (!isNaN(num)) {
return Operand.Create(num);
}
}
let d = parseFloat(this.TextValue);
if (!isNaN(d)) {
return Operand.Create(d);
}
if (errorMessage == null) {
return Operand.Error("Convert to number error!");
}
return Operand.Error(errorMessage);
}
ToNumber(errorMessage, ...args) {
if (this.TextValue.indexOf('.') === -1) {
let num = parseInt(this.TextValue);
if (!isNaN(num)) {
return Operand.Create(num);
}
}
let d = parseFloat(this.TextValue);
if (!isNaN(d)) {
return Operand.Create(d);
}
if (errorMessage == null) {
return Operand.Error("Convert to number error!");
}
return Operand.Error(errorMessage);
}
ToText(errorMessage) { return this; }
ToText(errorMessage, ...args) { return this; }
ToBoolean(errorMessage) {
let b = FunctionUtil.TryParseBoolean(this.TextValue);
if (b !== null) {
return b ? Operand.True : Operand.False;
}
if (errorMessage == null) {
return Operand.Error("Convert to bool error!");
}
return Operand.Error(errorMessage);
}
ToBoolean(errorMessage, ...args) {
let b = FunctionUtil.TryParseBoolean(this.TextValue);
if (b !== null) {
return b ? Operand.True : Operand.False;
}
if (errorMessage == null) {
return Operand.Error("Convert to bool error!");
}
return Operand.Error(errorMessage);
}
ToMyDate(errorMessage) {
try {
let date = new Date(this.TextValue.replaceAll("/","-"));
if (!isNaN(date.getTime())) {
return Operand.Create(new MyDate(date));
}
} catch (e) { }
if (errorMessage == null) {
return Operand.Error("Convert to date error!");
}
return Operand.Error(errorMessage);
}
ToMyDate(errorMessage, ...args) {
try {
let date = new Date(this.TextValue.replaceAll("/","-"));
if (!isNaN(date.getTime())) {
return Operand.Create(new MyDate(date));
}
} catch (e) { }
if (errorMessage == null) {
return Operand.Error("Convert to date error!");
}
return Operand.Error(errorMessage);
}
ToArray(errorMessage) {
return Operand.Error(errorMessage ?? "Convert to array error!");
}
ToJson(errorMessage = null) {
let txt = this.TextValue.trim();
if ((txt.startsWith('{') && txt.endsWith('}')) || (txt.startsWith('[') && txt.endsWith(']'))) {
try {
let json = JsonMapper.toObject(txt);
return Operand.Create(json);
} catch (e) { }
}
return Operand.Error(errorMessage ?? "Convert to json error!");
}
toString() {
let result = '"';
for (let c of this._value) {
switch (c) {
case '"': result += '\\"'; break;
case '\n': result += '\\n'; break;
case '\r': result += '\\r'; break;
case '\t': result += '\\t'; break;
case '\0': result += '\\0'; break;
case '\v': result += '\\v'; break;
case '\a': result += '\\a'; break;
case '\b': result += '\\b'; break;
case '\f': result += '\\f'; break;
default: result += c; break;
}
}
result += '"';
return result;
}
}
class OperandMyDate extends Operand {
constructor(obj) {
super();
this._value = obj;
}
get IsDate() { return true; }
get Type() { return OperandType.DATE; }
get DateValue() { return this._value; }
ToNumber(errorMessage) { return Operand.Create(this._value.valueOf()); }
ToNumber(errorMessage, ...args) { return Operand.Create(this._value.valueOf()); }
ToBoolean(errorMessage) { return this._value.valueOf() !== 0 ? Operand.True : Operand.False; }
ToBoolean(errorMessage, ...args) { return this._value.valueOf() !== 0 ? Operand.True : Operand.False; }
ToText(errorMessage) { return Operand.Create(this._value.toString()); }
ToText(errorMessage, ...args) { return Operand.Create(this._value.toString()); }
ToMyDate(errorMessage) { return this; }
ToMyDate(errorMessage, ...args) { return this; }
toString() { return '"' + this._value.toString() + '"'; }
}
class OperandJson extends Operand {
constructor(obj) {
super();
this._value = obj;
}
get IsJson() { return true; }
get Type() { return OperandType.JSON; }
get JsonValue() { return this._value; }
get IsArrayJson() { return false; }
ToText(errorMessage = null) {
return Operand.Create(this._value.toString());
}
ToText(errorMessage, ...args) {
return Operand.Create(this._value.toString());
}
ToArray(errorMessage) {
if (this.JsonValue.IsArray) {
let list = [];
for (let v of this.JsonValue) {
if (v.IsString) {
list.push(Operand.Create(v.StringValue));
} else if (v.IsBoolean) {
list.push(Operand.Create(v.BooleanValue));
} else if (v.IsDouble) {
list.push(Operand.Create(v.NumberValue));
} else if (v.IsNull) {
list.push(Operand.CreateNull());
} else {
list.push(Operand.Create(v));
}
}
return Operand.Create(list);
}
return Operand.Error(errorMessage ?? "Convert to array error!");
}
ToArray(errorMessage, ...args) {
if (this.JsonValue.IsArray) {
let list = [];
for (let v of this.JsonValue) {
if (v.IsString) {
list.push(Operand.Create(v.StringValue));
} else if (v.IsBoolean) {
list.push(Operand.Create(v.BooleanValue));
} else if (v.IsDouble) {
list.push(Operand.Create(v.NumberValue));
} else if (v.IsNull) {
list.push(Operand.CreateNull());
} else {
list.push(Operand.Create(v));
}
}
return Operand.Create(list);
}
return Operand.Error(errorMessage);
}
ToJson(errorMessage = null) {
return this;
}
TryGetValue(key) {
if (this.JsonValue.IsObject) {
let value = this.JsonValue.inst_object[key];
if (value) {
if (value.IsString) {
return Operand.Create(value.StringValue);
} else if (value.IsBoolean) {
return Operand.Create(value.BooleanValue);
} else if (value.IsDouble) {
return Operand.Create(value.NumberValue);
} else if (value.IsNull) {
return Operand.CreateNull();
} else {
return Operand.Create(value);
}
}
}
return null;
}
toString() {
return this._value.toString();
}
}
class OperandArray extends Operand {
constructor(obj) {
super();
this._value = obj;
}
get IsArray() { return true; }
get Type() { return OperandType.ARRARY; }
get ArrayValue() { return this._value; }
ToText(errorMessage = null) {
return Operand.Create(this.toString());
}
ToText(errorMessage, ...args) {
return Operand.Create(this.toString());
}
ToArray(errorMessage) { return this; }
ToArray(errorMessage, ...args) { return this; }
ToJson(errorMessage = null) {
let txt = this.toString();
try {
let json = JsonMapper.toObject(txt);
return Operand.Create(json);
} catch (e) { }
return Operand.Error(errorMessage ?? "Convert to json error!");
}
toString() {
let elements = this.ArrayValue.map(item => item.toString());
return '[' + elements.join(',') + ']';
}
}
class OperandError extends Operand {
get Type() { return OperandType.ERROR; }
get IsError() { return true; }
constructor(msg) {
super();
this._errorMsg = msg;
}
get ErrorMsg() { return this._errorMsg; }
ToNumber(errorMessage) { return this; }
ToNumber(errorMessage, ...args) { return this; }
ToBoolean(errorMessage) { return this; }
ToBoolean(errorMessage, ...args) { return this; }
ToText(errorMessage) { return this; }
ToText(errorMessage, ...args) { return this; }
ToArray(errorMessage) { return this; }
ToArray(errorMessage, ...args) { return this; }
ToMyDate(errorMessage) { return this; }
ToMyDate(errorMessage, ...args) { return this; }
}
class OperandNull extends Operand {
get IsNull() { return true; }
get IsNotNull() { return false; }
get Type() { return OperandType.NULL; }
toString() { return "null"; }
}
class KeyValue {
constructor(key, value) {
this.Key = key;
this.Value = value;
}
}
class OperandKeyValueList extends Operand {
constructor() {
super();
this.TextList = [];
}
get IsArrayJson() { return true; }
get Type() { return OperandType.ARRARYJSON; }
get ArrayValue() { return this.TextList.map(q => q.Value); }
ToText(errorMessage = null) {
return Operand.Create(this.toString());
}
ToText(errorMessage, ...args) {
return Operand.Create(this.toString());
}
ToArray(errorMessage) {
return Operand.Create(this.ArrayValue);
}
ToArray(errorMessage, ...args) {
return Operand.Create(this.ArrayValue);
}
ToJson(errorMessage = null) {
let txt = this.toString();
try {
let json = JsonMapper.toObject(txt);
return Operand.Create(json);
} catch (e) { }
return Operand.Error(errorMessage ?? "Convert to json error!");
}
AddValue(keyValue) {
this.TextList.push(keyValue);
}
TryGetValue(key) {
for (let item of this.TextList) {
if (item.key === key.toString()) {
return item.value;
}
}
return null;
}
ContainsKey(value) {
for (let item of this.TextList) {
if (value.TextValue === item.value) {
return true;
}
}
return false;
}
ContainsValue(value) {
for (let item of this.TextList) {
let op = item.Value;
if (value.Type !== op.Type) { continue; }
if (value.IsText) {
if (value.TextValue === op.TextValue) {
return true;
}
}
}
return false;
}
toString() {
let elements = this.TextList.map(item => '"' + item.Key + '":' + item.Value.toString());
return '{' + elements.join(',') + '}';
}
}
class OperandKeyValue extends Operand {
constructor(obj) {
super();
this._value = obj;
}
get IsArrayJson() { return true; }
get Type() { return OperandType.ARRARYJSON; }
get Value() { return this._value; }
}
// 导出所有类
export { OperandDouble, OperandBoolean, OperandString, OperandMyDate, OperandJson, OperandArray, OperandError, OperandNull, KeyValue, OperandKeyValueList, OperandKeyValue };
// 初始化静态属性
Operand.Version = new OperandString("ToolGood.Algorithm 6.2");
Operand.True = new OperandBoolean(true);
Operand.False = new OperandBoolean(false);
Operand.One = Operand.Create(1);
Operand.Zero = Operand.Create(0);
// 浏览器支持
if (typeof window !== 'undefined') {
window.Operand = Operand;
window.OperandDouble = OperandDouble;
window.OperandBoolean = OperandBoolean;
window.OperandString = OperandString;
window.OperandMyDate = OperandMyDate;
window.OperandJson = OperandJson;
window.OperandArray = OperandArray;
window.OperandError = OperandError;
window.OperandNull = OperandNull;
window.KeyValue = KeyValue;
window.OperandKeyValueList = OperandKeyValueList;
window.OperandKeyValue = OperandKeyValue;
}