-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
1903 lines (1709 loc) · 48.7 KB
/
index.js
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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Based on ndef.parser, by Raphael Graf([email protected])
http://www.undefined.ch/mparser/index.html
Ported to JavaScript and modified by Matthew Crumley ([email protected], http://silentmatt.com/)
You are free to use and modify this code in any way you find useful. Please leave this comment in the code
to acknowledge its original source. If you feel like it, I enjoy hearing about projects that use my code,
but don't feel like you have to let me know or ask permission.
*/
/*
* INTER-Mediator
* Copyright (c) INTER-Mediator Directive Committee (http://inter-mediator.org)
* This project started at the end of 2009 by Masayuki Nii [email protected].
*
* INTER-Mediator is supplied under MIT License.
* Please see the full license for details:
* https://github.com/INTER-Mediator/INTER-Mediator/blob/master/dist-docs/License.txt
*/
/**
*
* Usually you don't have to instantiate this class with a new operator.
* @constructor
*/
let Parser = (function (scope) {
let TNUMBER = 0
let TOP1 = 1
let TOP2 = 2
let TOP3 = 5
let SEP = 65
let TVAR = 3
let TFUNCALL = 4
Parser.regFirstVarChar = new RegExp('[\u00A0-\u1FFF\u2C00-\uDFFFa-zA-Z@_]')
Parser.regRestVarChar = new RegExp('[\u00A0-\u1FFF\u2C00-\uDFFFa-zA-Z@_:0-9]')
function Token(type_, index_, prio_, number_) {
this.type_ = type_
this.index_ = index_ || 0
this.prio_ = prio_ || 0
this.number_ = (number_ !== undefined && number_ !== null) ? number_ : 0
this.toString = function () {
switch (this.type_) {
case TNUMBER:
return this.number_
case TOP1:
case TOP2:
case TOP3:
case TVAR:
return this.index_
case TFUNCALL:
return 'CALL'
case SEP:
return 'SEPARATOR'
default:
return 'Invalid Token'
}
}
}
function Expression(tokens, ops1, ops2, functions, ops3, ops3Trail) {
this.tokens = tokens
}
// Based on http://www.json.org/json2.js
// let cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g
let escapable = /[\\\'\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g
let meta = { // table of character substitutions
'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '\'': '\\\'', '\\': '\\\\'
}
function escapeValue(v) {
if (typeof v === 'string') {
escapable.lastIndex = 0
return escapable.test(v) ? '\'' + v.replace(escapable, function (a) {
let c = meta[a]
return typeof c === 'string' ? c : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4)
}) + '\'' : '\'' + v + '\''
}
return v
}
Expression.prototype = {
simplify: function (values) {
values = values || {}
let nstack = []
let newexpression = []
let n1
let n2
let n3
let f
let L = this.tokens.length
let item
let i
for (i = 0; i < L; i++) {
item = this.tokens[i]
let type_ = item.type_
if (type_ === TNUMBER) {
nstack.push(item)
} else if (type_ === TVAR && (item.index_ in values)) {
item = new Token(TNUMBER, 0, 0, values[item.index_])
nstack.push(item)
} else if (type_ === TOP3 && nstack.length > 2) {
n3 = nstack.pop()
n2 = nstack.pop()
n1 = nstack.pop()
f = Parser.ops3[item.index_]
item = new Token(TNUMBER, 0, 0, f(n1.number_, n2.number_, n3.number_))
nstack.push(item)
} else if (type_ === TOP2 && nstack.length > 1) {
n2 = nstack.pop()
n1 = nstack.pop()
f = Parser.ops2[item.index_]
item = new Token(TNUMBER, 0, 0, f(n1.number_, n2.number_))
nstack.push(item)
} else if (type_ === TOP1 && nstack.length > 0) {
n1 = nstack.pop()
f = Parser.ops1[item.index_]
item = new Token(TNUMBER, 0, 0, f(n1.number_))
nstack.push(item)
} else {
while (nstack.length > 0) {
newexpression.push(nstack.shift())
}
newexpression.push(item)
}
}
while (nstack.length > 0) {
newexpression.push(nstack.shift())
}
return new Expression(newexpression)
},
substitute: function (variable, expr) {
if (!(expr instanceof Expression)) {
expr = new Parser().parse(String(expr))
}
let newexpression = []
let L = this.tokens.length
let item
let i
for (i = 0; i < L; i++) {
item = this.tokens[i]
let type_ = item.type_
if (type_ === TVAR && item.index_ === variable) {
for (let j = 0; j < expr.tokens.length; j++) {
let expritem = expr.tokens[j]
let replitem = new Token(expritem.type_, expritem.index_, expritem.prio_, expritem.number_)
newexpression.push(replitem)
}
} else {
newexpression.push(item)
}
}
return new Expression(newexpression)
},
evaluate: function (values) {
values = values || {}
let nstack = []
let L = this.tokens.length
let item
for (let i = 0; i < L; i++) {
item = this.tokens[i]
let type_ = item.type_
if (type_ === TNUMBER) {
nstack.push(item.number_)
} else if (type_ === TOP3) {
const n3 = nstack.pop()
const n2 = nstack.pop()
const n1 = nstack.pop()
const f = Parser.ops3Trail[item.index_]
nstack.push(f(n1, n2, n3))
} else if (type_ === TOP2) {
const n2 = nstack.pop()
const n1 = nstack.pop()
const f = Parser.ops2[item.index_]
nstack.push(f(n1, n2))
} else if (type_ === TVAR) {
if (item.index_ in values) {
nstack.push(values[item.index_])
} else if (item.index_ in Parser.functions) {
nstack.push(Parser.functions[item.index_])
} else {
throw new Error('undefined variable: ' + item.index_)
}
} else if (type_ === TOP1) {
const n1 = nstack.pop()
const f = Parser.ops1[item.index_]
nstack.push(f(n1))
} else if (type_ === SEP) {
const n2 = nstack.pop()
const n1 = nstack.pop()
nstack.push([n1, n2])
} else if (type_ === TFUNCALL) {
const n1 = nstack.pop()
const f = nstack.pop()
if (f.apply && f.call) {
if (Object.prototype.toString.call(n1) === '[object Array]') {
nstack.push(f.apply(undefined, n1))
} else {
nstack.push(f.call(undefined, n1))
}
} else {
throw new Error(f + ' is not a function')
}
} else {
throw new Error('invalid Expression')
}
}
if (nstack.length > 1) {
throw new Error('invalid Expression (parity)')
}
return nstack[0]
},
variables: function () {
let L = this.tokens.length
let vars = []
for (let i = 0; i < L; i++) {
let item = this.tokens[i]
if (item.type_ === TVAR && (vars.indexOf(item.index_) === -1) && !(item.index_ in Parser.functions)) {
vars.push(item.index_)
}
}
return vars
}
}
function iff(a, b, c) {
let vala, valb, valc
vala = (a instanceof Array) ? arguments[0][0] : arguments[0]
valb = (b instanceof Array) ? arguments[1][0] : arguments[1]
valc = (c instanceof Array) ? arguments[2][0] : arguments[2]
return vala ? valb : valc
}
function greaterthan(a, b) {
let numa, numb
numa = toNumber(a)
numb = toNumber(b)
if (!isNaN(numa) && !isNaN(numa) && numa === a && numb === b) {
return Number(numa) > Number(numb)
}
return a > b
}
function lessthan(a, b) {
let numa, numb
numa = toNumber(a)
numb = toNumber(b)
if (!isNaN(numa) && !isNaN(numa) && numa === a && numb === b) {
return Number(numa) < Number(numb)
}
return a < b
}
function greaterequal(a, b) {
let numa, numb
numa = toNumber(a)
numb = toNumber(b)
if (!isNaN(numa) && !isNaN(numa) && numa === a && numb === b) {
return Number(numa) >= Number(numb)
}
return a >= b
}
function lessequal(a, b) {
let numa, numb
numa = toNumber(a)
numb = toNumber(b)
if (!isNaN(numa) && !isNaN(numa) && numa === a && numb === b) {
return Number(numa) <= Number(numb)
}
return a <= b
}
function equal(a, b) {
// let numa, numb
// numa = toNumber(a)
// numb = toNumber(b)
// if (!isNaN(numa) && !isNaN(numa) && numa === a && numb === b) {
// return Number(numa) === Number(numb)
// }
// return a === b
return a == b // Don't set the operator to ===
}
function notequal(a, b) {
// let numa, numb
// numa = toNumber(a)
// numb = toNumber(b)
// if (!isNaN(numa) && !isNaN(numa) && numa === a && numb === b) {
// return Number(numa) !== Number(numb)
// }
// return a !== b
return a != b // Don't set the operator tor !==
}
// http://qiita.com/south37/items/e400a3a698957ab4aa7a
// NaN === NaN returns false.
function isReallyNaN(x) {
return x !== x // if x is NaN returns true, otherwise false.
}
function add(a, b) {
let numa = arrayToValue(a)
let numb = arrayToValue(b)
if (((typeof numa) === 'string' && !isNumberable(numa)) || ((typeof numb) === 'string' && !isNumberable(numb))) {
return numa + numb
}
numa = toNumber(numa)
numb = toNumber(numb)
if (isReallyNaN(numa) || isReallyNaN(numb) || typeof (numa) === 'undefined' || typeof (numb) === 'undefined') {
return NaN
}
if (!isNaN(numa) && !isNaN(numb)) {
return Number(numa) + Number(numb)
}
return a + b
}
function addstring(a, b) {
return String(a) + String(b)
}
/* ===== private ===== */
function isNumber(a) {
return ((typeof a === 'number') && (isFinite(a)));
}
function isNumberable(a) {
const v = unformat(a)
if (v === '') {
return false
}
const val = parseFloat(v)
if (isNaN(val)) {
return false
}
return String(val).length > 0
}
/* ===== private ===== */
function arrayToValue(a) {
let values = []
let isAllNum = true
if (Array.isArray(a)) {
for (const value of a) {
values.push(arrayToValue(value))
isAllNum &&= isNumber(value)
}
return isAllNum ? sumfunc(values) : values.join()
}
if (typeof (a) === 'object') {
for (const key in a) {
values.push(arrayToValue(a[key]))
isAllNum &&= isNumber(value)
}
return isAllNum ? sumfunc(values) : values.join()
}
// if (a === null || isNaN(a) || typeof (a) === 'undefined') {
// return a
// }
return a
}
/* ===== private ===== */
function toNumber(str) {
let value
if (str === undefined) {
return NaN
}
if (str === true) {
return true
}
if (str === false) {
return false
}
if (str === '') {
return 0
}
value = str
if (Array.isArray(str)) {
if (str.length < 1) {
return 0
} else {
value = str[0]
}
}
value = unformat(value)
return value
}
function sub(a, b) {
let numa = arrayToValue(a)
let numb = arrayToValue(b)
if (isReallyNaN(numa) || isReallyNaN(numb) || typeof (numa) == 'undefined' || typeof (numb) == 'undefined') {
return NaN
}
numa = (a === null || a === '') ? 0 : toNumber(a)
numb = (b === null || b === '') ? 0 : toNumber(b)
if (!isNaN(numa) && !isNaN(numb)) {
return numa - numb // Numeric substruct
}
let str = String(a), pos
do { // String substruct
pos = str.indexOf(b)
if (pos > -1) {
str = str.substring(0, pos) + str.substring(pos + b.length)
}
} while (pos > -1)
return str
}
function mul(a, b) {
let numa = arrayToValue(a)
let numb = arrayToValue(b)
if (isReallyNaN(numa) || isReallyNaN(numb) || typeof (numa) === 'undefined' || typeof (numb) === 'undefined') {
return NaN
}
numa = (numa === null || numa === '') ? 0 : toNumber(numa)
numb = (numb === null || numb === '') ? 0 : toNumber(numb)
return numa * numb
}
function div(a, b) {
let numa = arrayToValue(a)
let numb = arrayToValue(b)
if (isReallyNaN(numa) || isReallyNaN(numb) || typeof (numa) === 'undefined' || typeof (numb) === 'undefined') {
return NaN
}
numa = (numa === null || numa === '') ? 0 : toNumber(numa)
numb = (numb === null || numb === '') ? 0 : toNumber(numb)
return numa / numb
}
function mod(a, b) {
a = toNumber(a)
b = toNumber(b)
if (isReallyNaN(a) || isReallyNaN(b)) {
return NaN
}
return a % b
}
function neg(a) {
if (isReallyNaN(a)) {
return NaN
}
a = toNumber(a)
return -a
}
function random(a) {
a = toNumber(a)
return Math.random() * (a || 1)
}
function fac(a) { //a!
if (isReallyNaN(a)) {
return NaN
}
a = toNumber(a)
a = Math.floor(a)
let b = a
while (a > 1) {
b = b * (--a)
}
return b
}
function implies(a, b) {
return !(toValue(a) === true && toValue(b) === false)
}
function logicalnot(a) {
return !toValue(a)
}
function logicaland(a, b) {
return toValue(a) && toValue(b)
}
function logicalor(a, b) {
return toValue(a) || toValue(b)
}
function sumfunc() {
let result = 0, i
for (i = 0; i < arguments.length; i++) {
result += toNumber(arguments[i])
}
return result
}
function averagefunc() {
let result = 0, i, count = 0
for (i = 0; i < arguments.length; i++) {
result += toNumber(arguments[i])
count++
}
return result / count
}
function countElements() {
let i, count = 0
for (i = 0; i < arguments.length; i++) {
count += Array.isArray(arguments[i]) ? arguments[i].length : 1
}
return count
}
function listfunc() {
let result = '', i
for (i = 0; i < arguments.length; i++) {
result += String(arguments[i])
result += '\n'
}
return result
}
function roundfunc(a, b) {
if (b === undefined) {
return Math.round(a)
} else {
a = (a instanceof Array) ? a.join() : a
b = (b instanceof Array) ? b.join() : b
return round(a, b)
}
}
function isnull(a) {
return a === null;
}
/**
* This method returns the rounded value of the 1st parameter to the 2nd parameter from decimal point.
* @param {number} value The source value.
* @param {number} digit Positive number means after the decimal point, and negative menas before it.
* @returns {number}
*/
function round(value, digit) {
'use strict'
let powers = Math.pow(10, digit)
return Math.round(value * powers) / powers
}
function length(a) {
if (a === undefined || a === null) {
return 0
}
return paramStr(a).length
}
/* ===== private ===== */
function toValue(str) {
if (Array.isArray(str)) {
if (str.length > 0) {
return str[0]
}
}
return str
}
/* ===== private ===== */
// TODO: use hypot that doesn't overflow
function pyt(a, b) {
return Math.sqrt(a * a + b * b)
}
function append(a, b) {
if (Object.prototype.toString.call(a) !== '[object Array]') {
return [a, b]
}
a = a.slice()
a.push(b)
return a
}
function paramStrList(value) {
let val = value
val = (Array.isArray(val)) ? val.join('\n') : val
val = (val === false || val === null) ? '' : val
return String(val)
}
function charsetand(a, b) {
let result = ''
if (a === null || a === undefined || b === null || b === undefined) {
return null
}
const stra = paramStrList(a)
const strb = paramStrList(b)
for (let i = 0; i < stra.length; i++) {
if (strb.indexOf(stra.substring(i, i + 1)) > -1) {
result += stra.substring(i, i + 1)
}
}
return result
}
function charsetor(a, b) {
let result = ''
if (a === null || a === undefined || b === null || b === undefined) {
return null
}
const stra = paramStrList(a)
const strb = paramStrList(b)
for (let i = 0; i < strb.length; i++) {
if (stra.indexOf(strb.substring(i, i + 1)) < 0) {
result += strb.substring(i, i + 1)
}
}
return stra + result
}
function charsetnoother(a, b) {
let result = ''
if (a === null || a === undefined || b === null || b === undefined) {
return null
}
const stra = paramStrList(a)
const strb = paramStrList(b)
for (let i = 0; i < stra.length; i++) {
if (strb.indexOf(stra.substring(i, i + 1)) < 0) {
result += stra.substring(i, i + 1)
}
}
return result
}
/* ===== private ===== */
function parametersOfMultiline(a, b) {
let nl = '\n'
if (a === null || a === undefined || b === null || b === undefined) {
return null
}
const stra = paramStrList(a)
const strb = paramStrList(b)
const nls = [stra.indexOf('\r\n'), stra.indexOf('\r'), stra.indexOf('\n')]
for (let i = 0; i < nls.length; i++) {
nls[i] = (nls[i] < 0) ? stra.length : nls[i]
}
if (nls[0] < stra.length && nls[0] <= nls[1] && nls[0] < nls[2]) {
nl = '\r\n'
} else if (nls[1] < stra.length && nls[1] < nls[0] && nls[1] < nls[2]) {
nl = '\r'
}
const arraya = stra.replace('\r\n', '\n').replace('\r', '\n').split('\n')
const arrayb = strb.replace('\r\n', '\n').replace('\r', '\n').split('\n')
return [arraya, arrayb, nl]
}
/* ===== private ===== */
function itemsetand(a, b) {
let result = ''
const params = parametersOfMultiline(a, b)
if (params === null) {
return null
}
const arraya = params[0]
const arrayb = params[1]
const nl = params[2]
for (let i = 0; i < arraya.length; i++) {
if (arrayb.indexOf(arraya[i]) > -1 && arraya[i].length > 0) {
result += arraya[i] + nl
}
}
return result
}
function itemsetor(a, b) {
let result = ''
const params = parametersOfMultiline(a, b)
if (params === null) {
return null
}
const arraya = params[0]
const arrayb = params[1]
const nl = params[2]
for (i = 0; i < arraya.length; i++) {
if (arraya[i].length > 0) {
result += arraya[i] + nl
}
}
for (let i = 0; i < arrayb.length; i++) {
if (arraya.indexOf(arrayb[i]) < 0 && arrayb[i].length > 0) {
result += arrayb[i] + nl
}
}
return result
}
function itemsetnoother(a, b) {
let result = ''
const params = parametersOfMultiline(a, b)
if (params === null) {
return null
}
const arraya = params[0]
const arrayb = params[1]
const nl = params[2]
for (let i = 0; i < arraya.length; i++) {
if (arrayb.indexOf(arraya[i]) < 0 && arraya[i].length > 0) {
result += arraya[i] + nl
}
}
return result
}
function itematindex(a, start, end) {
let result = ''
const params = parametersOfMultiline(a, '')
if (params === null) {
return null
}
const arraya = params[0]
const nl = params[2]
end = (end === undefined) ? arraya.length : end
for (let i = start; (i < start + end) && (i < arraya.length); i++) {
result += arraya[i] + nl
}
return result
}
function itemIndexOfFunc(list, str) {
if (!list) {
return -1
}
let a = list.replace('\r\n', '\n').replace('\r', '\n')
let ix = 0
let item, pos
while (a.length > 0) {
pos = a.indexOf('\n')
if (pos > -1) {
item = a.substring(0, pos)
a = a.substring(pos + 1)
} else {
item = a
a = ''
}
if (item === str) {
return ix
}
ix++
}
return -1
}
function numberformat(val, digit) {
return IMLibFormat.numberFormat(paramStr(val), digit ? paramNum(digit) : 0, {useSeparator: true})
}
function currencyformat(val, digit) {
return IMLibFormat.currencyFormat(paramStr(val), paramNum(digit), {useSeparator: true})
}
function substr(str, pos, len = 1) {
if (str == null) {
return null
}
if (pos < 0) {
const l = length(str)
pos = l + pos
}
if (len < 0) {
const origLen = len
pos += len
len *= -1
if (pos < 0) {
len = pos - origLen
pos = 0
}
}
return paramStr(str).substring(paramNum(pos), paramNum(pos) + paramNum(len))
}
function substring(str, start, end) {
if (str == null) {
return null
}
const len = length(str)
if (start < 0) {
start = len + start
}
if (end < 0) {
end = len + end
}
if (typeof end === 'undefined') {
end = start + 1
}
return paramStr(str).substring(paramNum(start), paramNum(end))
}
function leftstring(str, start) {
if (str == null) {
return null
}
return paramStr(str).substring(0, paramNum(start))
}
function midstring(str, start, end) {
if (str == null) {
return null
}
return paramStr(str).substring(paramNum(start), paramNum(start) + paramNum(end))
}
function rightstring(str, start) {
if (str == null) {
return null
}
return paramStr(str).substring(paramStr(str).length - paramNum(start))
}
function indexof(str, search, from) {
if (str == null) {
return null
}
if (typeof from === 'undefined') {
return paramStr(str).indexOf(paramStr(search))
}
return paramStr(str).indexOf(paramStr(search), paramStr(from))
}
function lastindexof(str, search, from) {
if (str == null) {
return null
}
if (typeof from === 'undefined') {
return paramStr(str).lastIndexOf(paramStr(search))
}
return paramStr(str).lastIndexOf(paramStr(search), paramStr(from))
}
function replace(str, start, end, rep) {
if (str == null) {
return null
}
return paramStr(str).substring(0, paramNum(start)) + paramStr(rep) + paramStr(str).substring(paramNum(end))
}
function substitute(str, search, rep) {
if (str == null) {
return null
}
const reg = new RegExp(paramStr(search), 'g')
return paramStr(str).replace(reg, paramStr(rep))
}
function match(str, pattern) {
return paramStr(str).match(new RegExp(paramStr(pattern)))
}
function test(str, pattern) {
if (str == null) {
return null
}
return (new RegExp(paramStr(pattern))).test(paramStr(str))
}
function basename(path) {
if (path == null) {
return null
}
let str = paramStr(path)
if (str.substring(str.length - 1) === '/') {
str = str.substring(0, str.length - 1)
}
return str.substring(str.lastIndexOf('/') + 1)
}
function extname(path) {
if (path == null) {
return null
}
const str = paramStr(path)
const dotPos = str.lastIndexOf('.')
return (dotPos < 0) ? '' : str.substring(str.lastIndexOf('.') + 1)
}
function dirname(path) {
if (path == null) {
return null
}
const str = paramStr(path)
return str.substring(0, str.lastIndexOf('/'))
}
function startsWith(str, pin) {
if (str === null || pin === null) {
return null
}
return paramStr(str).indexOf(paramStr(pin)) === 0
}
function endsWith(str, pin) {
if (str === null || pin === null) {
return null
}
const stra = paramStr(str)
const pina = paramStr(pin)
return (stra.indexOf(pina) + pina.length) === stra.length
}
function tolowerFunc(str) {
if (str === null) {
return null
}
return paramStr(str).toLowerCase()
}
function toupperFunc(str) {
if (str === null) {
return null
}
return paramStr(str).toUpperCase()
}
function decodeURIFunc(str) {
if (str === null) {
return null
}
return decodeURI(paramStr(str))
}
function encodeURIFunc(str) {
if (str === null) {
return null
}
return encodeURI(paramStr(str))
}
function decodeURIComponentFunc(str) {
if (str === null) {
return null
}
return decodeURIComponent(paramStr(str))
}
function encodeURIComponentFunc(str) {
if (str === null) {
return null
}
return encodeURIComponent(paramStr(str))
}
function jsonparse(jstr, path) {
if (jstr === null || path === null) {
return null
}
const parsed = JSON.parse(jstr)
const nodes = path.split(".")
let result = parsed
for (const node of nodes) {
result = result[node]
}
return result
}
function paramStr(value) {
let val = value
val = (Array.isArray(val)) ? val.join() : val
val = (val === false || val === null) ? '' : val
return String(val)
}
function paramNum(value) {
let val = value
val = (Array.isArray(val)) ? val.join() : val
val = (val === false || val === null) ? 0 : val
return parseInt(val)
}
Parser.timeOffset = (new Date()).getTimezoneOffset()
function DateInt(str) {
let theDate
if (str === undefined) {
theDate = Date.now()
} else {
theDate = Date.parse(str.replace(/-/g, '/'))
theDate -= Parser.timeOffset * 60000
}
return parseInt(theDate / 86400000)
}
function SecondInt(str) {
let theDate
if (str === undefined) {
theDate = Date.now()
} else {