-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
1053 lines (844 loc) · 30.9 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
'use strict';
const BigNumber = require('bignumber.js');
BigNumber.config({ DECIMAL_PLACES: 8 });
const
HEX_CHARS = '0123456789ABCDEFabcdef',
BASE56_CHARS = '23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz',
BASE58_CHARS = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz',
BASE64_CHARS = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/=';
let isEnabled = true;
const activeExports = {};
const precon = module.exports = {
/**
* Value must be an array.
*
* @param val {*[]}
* @param [paramName] {string} The name of the parameter.
* @param [length] {number} The exact length the array must be.
* @returns {*[]} val
*/
array: array,
/**
* Value must be an array or otherwise not set.
*
* @param val {*[]|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @param [length] {number} The exact length the array must be.
* @returns {*[]|undefined|null} val
*/
opt_array: opt_precon(array),
/**
* Value must be an array whose elements are a specific type of value.
*
* @param val {*[]}
* @param type {string} The name of the type.
* @param [paramName] {string} The name of the parameter.
* @returns {*[]} val
*/
arrayOf: arrayOf,
/**
* Value must be an array whose elements are a specific type of value or otherwise not set.
*
* @param val {*[]|undefined|null}
* @param type {string} The name of the type.
* @param [paramName] {string} The name of the parameter.
* @returns {*[]|undefined|null} val
*/
opt_arrayOf: opt_precon(arrayOf),
/**
* Value must be an array whose elements are an instance of a specific type.
*
* @param val {*[]}
* @param instanceType {*} The instance type.
* @param [paramName] {string} The name of the parameter.
* @returns {*[]} val
*/
arrayOfInstance: arrayOfInstance,
/**
* Value must be an array whose elements are an instance of a specific type or otherwise not set.
*
* @param val {*[]|undefined|null}
* @param instanceType {*} The instance type.
* @param [paramName] {string} The name of the parameter.
* @returns {*[]|undefined|null} val
*/
opt_arrayOfInstance: opt_precon(arrayOfInstance),
/**
* Value must be a string of base56 characters.
*
* @param val {string}
* @param [paramName] {string} The name of the parameter.
* @returns {string} val
*/
base56: base56,
/**
* Value must be a string of base56 characters or otherwise not set.
*
* @param val {string|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {string|undefined|null} val
*/
opt_base56: opt_precon(base56),
/**
* Value must be a string of base58 characters.
*
* @param val {string}
* @param [paramName] {string} The name of the parameter.
* @returns {string} val
*/
base58: base58,
/**
* Value must be a string of base58 characters or otherwise not set.
*
* @param val {string|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {string|undefined|null} val
*/
opt_base58: opt_precon(base58),
/**
* Value must be a string of base64 characters.
*
* @param val {string}
* @param [paramName] {string} The name of the parameter.
* @returns {string} val
*/
base64: base64,
/**
* Value must be a string of base64 characters or otherwise not set.
*
* @param val {string|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {string|undefined|null} val
*/
opt_base64: opt_precon(base64),
/**
* Value must be a boolean type.
*
* @param val {boolean}
* @param [paramName] {string} The name of the parameter.
* @returns {boolean} val
*/
boolean: boolean,
/**
* Value must be a boolean type or otherwise not set.
*
* @param val {boolean|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {boolean|undefined|null} val
*/
opt_boolean: opt_precon(boolean),
/**
* Value must a BigInt type.
*
* @param val {BigInt}
* @param [paramName] {string} The name of the parameter.
* @returns {BigInt} val
*/
bigint: bigint,
/**
* Value must a BigInt type or otherwise not set.
*
* @param val {BigInt|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {BigInt|undefined|null} val
*/
opt_bigint: opt_precon(bigint),
/**
* Value must be a bignumber.js type.
*
* @param val {BigNumber}
* @param [paramName] {string} The name of the parameter.
* @returns {BigNumber} val
*/
bigNumberJS: bigNumberJS,
/**
* Value must be a bignumber.js type or otherwise not set.
*
* @param val {BigNumber|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {BigNumber|undefined|null} val
*/
opt_bigNumberJS: opt_precon(bigNumberJS),
/**
* Value must be a Buffer type.
*
* @param val {Buffer}
* @param [paramName] {string} The name of the parameter.
* @param [length] {number} The exact length the Buffer must be.
* @returns {Buffer}
*/
buffer: buffer,
/**
* Value must be a Buffer type or otherwise not set.
*
* @param val {Buffer|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @param [length] {number} The exact length the Buffer must be.
* @returns {Buffer|undefined|null}
*/
opt_buffer: opt_precon(buffer),
/**
* Value must be any type other than "undefined".
*
* @param val {*}
* @param [paramName] {string} The name of the parameter.
* @returns {*} val
*/
defined: defined,
/**
* Value must be a function.
*
* @param val {function}
* @param [paramName] {string} The name of the parameter.
* @returns {function} val
*/
funct: funct,
/**
* Value must be a function or otherwise not set.
*
* @param val {function|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {function|undefined|null} val
*/
opt_funct: opt_precon(funct),
/**
* Value must be a string of hexadecimal characters.
*
* @param val {string}
* @param [paramName] {string} The name of the parameter.
* @returns {string} val
*/
hex: hex,
/**
* Value must be a string of hexadecimal characters or otherwise not set.
*
* @param val {string|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {string|undefined|null} val
*/
opt_hex: opt_precon(hex),
/**
* Value must be an instance of a specific type.
*
* @param val {*}
* @param instanceType {*} The type of instance.
* @param [paramName] {string} The name of the parameter.
* @returns {*} val
*/
instanceOf: instanceOf,
/**
* Value must be an instance of a specific type or otherwise not set.
*
* @param val {*|undefined|null}
* @param instanceType {*} The type of instance.
* @param [paramName] {string} The name of the parameter.
* @returns {*|undefined|null} val
*/
opt_instanceOf: opt_precon(instanceOf),
/**
* Value must be a number type with no decimal places.
*
* @param val {number}
* @param [paramName] {string} The name of the parameter.
* @returns {number} val
*/
integer: integer,
/**
* Value must be a number type with no decimal places or otherwise not set.
*
* @param val {number|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {number|undefined|null} val
*/
opt_integer: opt_precon(integer),
/**
* Value must be a Map.
*
* @param val {Map}
* @param [paramName] {string} The name of the parameter.
* @returns {Map} val
*/
map: map,
/**
* Value must be a Map or otherwise not set.
*
* @param val {Map|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {Map|undefined|null} val
*/
opt_map: opt_precon(map),
/**
* Value must be a bignumber.js type and the value must be between specified min and max values.
*
* @param val {BigNumber}
* @param min {number} The minimum value (inclusive).
* @param max {number} The maximum value (inclusive).
* @param [paramName] {string} The name of the parameter.
* @returns {BigNumber} val
*/
minMaxBigNumberJS: minMaxBigNumberJS,
/**
* Value must be a bignumber.js type and the value must be between specified min and max values or otherwise not set.
*
* @param val {BigNumber|undefined|null}
* @param min {number} The minimum value (inclusive).
* @param max {number} The maximum value (inclusive).
* @param [paramName] {string} The name of the parameter.
* @returns {BigNumber|undefined|null} val
*/
opt_minMaxBigNumberJS: opt_precon(minMaxBigNumberJS),
/**
* Value must be a number type with no decimal places and must be between specified min and max values.
*
* @param val {number}
* @param min {number} The minimum value (inclusive).
* @param max {number} The maximum value (inclusive).
* @param [paramName] {string} The name of the parameter.
* @returns {number} val
*/
minMaxInteger: minMaxInteger,
/**
* Value must be a number type with no decimal places and must be between specified min and max values or
* otherwise not set.
*
* @param val {number|undefined|null}
* @param min {number} The minimum value (inclusive).
* @param max {number} The maximum value (inclusive).
* @param [paramName] {string} The name of the parameter.
* @returns {number|undefined|null} val
*/
opt_minMaxInteger: opt_precon(minMaxInteger),
/**
* Value must be a number type with a value between the specified min and max values.
*
* @param val {number}
* @param min {number} The minimum value (inclusive).
* @param max {number} The maximum value (inclusive).
* @param [paramName] {string} The name of the parameter.
* @returns {number} val
*/
minMaxNumber: minMaxNumber,
/**
* Value must be a number type with a value between the specified min and max values or otherwise not set.
*
* @param val {number|undefined|null}
* @param min {number} The minimum value (inclusive).
* @param max {number} The maximum value (inclusive).
* @param [paramName] {string} The name of the parameter.
* @returns {number|undefined|null} val
*/
opt_minMaxNumber: opt_precon(minMaxNumber),
/**
* Value must be bignumber.js type with a value less than or equal to 0.
*
* @param val {BigNumber}
* @param [paramName] {string} The name of the parameter.
* @returns {BigNumber} val
*/
negativeBigNumberJS: negativeBigNumberJS,
/**
* Value must be bignumber.js type with a value less than or equal to 0 or otherwise not set.
*
* @param val {BigNumber|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {BigNumber|undefined|null} val
*/
opt_negativeBigNumberJS: opt_precon(negativeBigNumberJS),
/**
* Value must be a number type with no decimal places and a value less than or equal to 0.
*
* @param val {number}
* @param [paramName] {string} The name of the parameter.
* @returns {number} val
*/
negativeInteger: negativeInteger,
/**
* Value must be a number type with no decimal places and a value less than or equal to 0 or otherwise not set.
*
* @param val {number|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {number|undefined|null} val
*/
opt_negativeInteger: opt_precon(negativeInteger),
/**
* Value must be a number type with a value less than or equal to 0.
*
* @param val {number}
* @param [paramName] {string} The name of the parameter.
* @returns {number} val
*/
negativeNumber: negativeNumber,
/**
* Value must be a number type with a value less than or equal to 0 or otherwise not set.
*
* @param val {number|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {number|undefined|null} val
*/
opt_negativeNumber: opt_precon(negativeNumber),
/**
* Value cannot be null or undefined.
*
* @param val {*}
* @param [paramName] {string} The name of the parameter.
* @returns {*} val
*/
notNull: notNull,
/**
* Value must be a number type.
*
* @param val {number}
* @param [paramName] {string} The name of the parameter.
* @returns {number}
*/
number: number,
/**
* Value must be a number type or otherwise not set.
*
* @param val {number|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {number|undefined|null}
*/
opt_number: opt_precon(number),
/**
* Value must be an object type.
*
* @param val {object}
* @param [paramName] {string} The name of the parameter.
* @returns {object}
*/
obj: obj,
/**
* Value must be an object type or otherwise not set.
*
* @param val {object|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {object|undefined|null}
*/
opt_obj: opt_precon(obj),
/**
* Value must be one of the specified values.
*
* @param val {*}
* @param optionsArr {*[]}
* @param [paramName] {string} The name of the parameter.
* @returns {*}
*/
oneOf: oneOf,
/**
* Value must be one of the specified values or otherwise not set.
*
* @param val {*|undefined|null}
* @param optionsArr {*[]} Array of valid values.
* @param [paramName] {string} The name of the parameter.
* @returns {*|undefined|null}
*/
opt_oneOf: opt_precon(oneOf),
/**
* Value must be an instance of one of the specified instance types.
*
* @param val {*}
* @param instanceTypeArr {*[]} Array of valid instance types.
* @param [paramName] {string} The name of the parameter.
* @returns {*}
*/
oneOfInstance: oneOfInstance,
/**
* Value must be an instance of one of the specified instance types or otherwise not set.
*
* @param val {*|undefined|null}
* @param instanceTypeArr {*[]} Array of valid instance types.
* @param [paramName] {string} The name of the parameter.
* @returns {*|undefined|null}
*/
opt_oneOfInstance: opt_precon(oneOfInstance),
/**
* Value must be one of the specified value types.
*
* @param val {*}
* @param typeArr {string[]} Array of valid value types.
* @param [paramName] {string} The name of the parameter.
* @returns {*}
*/
oneOfType: oneOfType,
/**
* Value must be one of the specified value types or otherwise not set.
*
* @param val {*|undefined|null}
* @param typeArr {string[]} Array of valid value types.
* @param [paramName] {string} The name of the parameter.
* @returns {*|undefined|null}
*/
opt_oneOfType: opt_precon(oneOfType),
/**
* Value must be a bignumber.js type with a value greater than or equal to 0.
*
* @param val {BigNumber}
* @param [paramName] {string} The name of the parameter.
* @returns {BigNumber}
*/
positiveBigNumberJS: positiveBigNumberJS,
/**
* Value must be a bignumber.js type with a value greater than or equal to 0 or otherwise not set.
*
* @param val {BigNumber|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {BigNumber|undefined|null}
*/
opt_positiveBigNumberJS: opt_precon(positiveBigNumberJS),
/**
* Value must be a number type with no decimal places and a value greater than or equal to 0.
*
* @param val {number}
* @param [paramName] {string} The name of the parameter.
* @returns {number}
*/
positiveInteger: positiveInteger,
/**
* Value must be a number type with no decimal places and a value greater than or equal to 0 or otherwise not set.
*
* @param val {number|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {number|undefined|null}
*/
opt_positiveInteger: opt_precon(positiveInteger),
/**
* Value must be a number type with a value greater than or equal to 0.
*
* @param val {number}
* @param [paramName] {string} The name of the parameter.
* @returns {number}
*/
positiveNumber: positiveNumber,
/**
* Value must be a number type with a value greater than or equal to 0 or otherwise not set.
*
* @param val {number|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {number|undefined|null}
*/
opt_positiveNumber: opt_precon(positiveNumber),
/**
* Value must be a Set.
*
* @param val {Set}
* @param [paramName] {string} The name of the parameter.
* @returns {Set}
*/
set: set,
/**
* Value must be a Set or otherwise not set.
*
* @param val {Set|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {Set|undefined|null}
*/
opt_set: opt_precon(set),
/**
* Value must be a string.
*
* @param val {string}
* @param [paramName] {string} The name of the parameter.
* @returns {string}
*/
string: string,
/**
* Value must be a string or otherwise not set.
*
* @param val {string|undefined|null}
* @param [paramName] {string} The name of the parameter.
* @returns {string|undefined|null}
*/
opt_string: opt_precon(string),
/**
* Value must not be defined at all.
*
* @param val {undefined}
* @param [paramName] {string} The name of the parameter.
*/
undef: undef
};
class PreconError extends Error {
constructor(message) {
super(message);
}
}
Object.defineProperties(precon, {
enabled: {
get: () => { return isEnabled; },
set: val => {
isEnabled = !!val;
_updateEnabled();
}
},
PreconError: {
value: PreconError
}
});
// Archive functions so that they can be swapped in and out of the exported object as precon is enabled and disabled.
Object.keys(precon).forEach(funcName => {
activeExports[funcName] = precon[funcName];
});
_updateEnabled();
function _updateEnabled() {
Object.keys(activeExports).forEach(funcName => {
precon[funcName] = isEnabled ? activeExports[funcName] : inactivePrecon;
});
}
function inactivePrecon(obj) {
return obj;
}
function opt_precon(preconFunct) {
return function(...args) {
if (args[0] === null || typeof args[0] === 'undefined')
return args[0];
return preconFunct(...args);
}
}
function array(val, paramName, length) {
if (typeof paramName === 'number') {
length = paramName;
paramName = null;
}
if (!Array.isArray(val))
throw new PreconError(`${paramName || 'Argument'} must be an array. Type is "${typeof(val)}" and value is: ${val}`);
if (typeof length === 'number' && val.length !== length)
throw new PreconError(`${paramName || 'Argument'} array must contain ${length} elements. Has ${val.length} elements.`);
return val;
}
function arrayOf(val, type, paramName) {
if (typeof type !== 'string')
throw new Error('type is expected to be a string');
array(val, paramName);
val.forEach(item => {
if (typeof item !== type)
throw new PreconError(`${paramName || 'Argument'} must be an array of "${type}". Contains "${typeof(val)}"`);
});
return val;
}
function arrayOfInstance(val, instanceType, paramName) {
array(val, paramName);
val.forEach(item => {
if (!(item instanceof instanceType))
throw new PreconError(`${paramName || 'Argument'} must be an array of "${instanceType.name}". Contains "${val.constructor.name}"`);
});
return val;
}
function base56(val, paramName) {
if (typeof val !== 'string')
throw new PreconError(`${paramName || 'Argument'} must be a base56 string. Type is "${typeof(val)}" and value is: ${val}`);
for (let i = 0; i < val.length; i++) {
if (BASE56_CHARS.indexOf(val.charAt(i)) === -1)
throw new PreconError(`${paramName || 'Argument'} must be a base56 string. Value is: ${val}`);
}
return val;
}
function base58(val, paramName) {
if (typeof val !== 'string')
throw new PreconError(`${paramName || 'Argument'} must be a base58 string. Type is "${typeof(val)}" and value is: ${val}`);
for (let i = 0; i < val.length; i++) {
if (BASE58_CHARS.indexOf(val.charAt(i)) === -1)
throw new PreconError(`${paramName || 'Argument'} must be a base58 string. Value is: ${val}`);
}
return val;
}
function base64(val, paramName) {
if (typeof val !== 'string')
throw new PreconError(`${paramName || 'Argument'} must be a base64 string. Type is "${typeof(val)}" and value is: ${val}`);
if (val.length % 2 !== 0)
throw new PreconError(`${paramName || 'Argument'} must be a hex string. Odd number of characters in value. Value is: ${val}`);
for (let i = 0; i < val.length; i++) {
if (BASE64_CHARS.indexOf(val.charAt(i)) === -1)
throw new PreconError(`${paramName || 'Argument'} must be a base64 string. Value is: ${val}`);
}
return val;
}
function boolean(val, paramName) {
if (typeof val !== 'boolean')
throw new PreconError(`${paramName || 'Argument'} must be boolean type. Type is "${typeof(val)}" and value is: ${val}`);
return val
}
function bigint(val, paramName) {
if (typeof val !== 'bigint')
throw new PreconError(`${paramName || 'Argument'} must be BigInt type. Type is "${typeof(val)}" and value is: ${val}`);
return val;
}
function bigNumberJS(val, paramName) {
if (!BigNumber.isBigNumber(val) || val.isNaN())
throw new PreconError(`${paramName || 'Argument'} must be BigNumber type. Type is "${typeof(val)}" and value is: ${val}`);
return val;
}
function buffer(val, paramName, length) {
if (typeof paramName === 'number') {
length = paramName;
paramName = null;
}
if (!Buffer.isBuffer(val))
throw new PreconError(`${paramName || 'Argument'} must be a Buffer. Type is "${typeof(val)}" and value is: ${val}`);
if (typeof length === 'number' && val.length !== length)
throw new PreconError(`${paramName || 'Argument'} Buffer must be ${length} bytes. Is ${val.length} bytes.`);
return val;
}
function defined(val, paramName) {
if (typeof val === 'undefined')
throw new PreconError(`${paramName || 'Argument'} must be defined.'`);
return val;
}
function funct(val, paramName) {
if (typeof val !== 'function')
throw new PreconError(`${paramName || 'Argument'} must be a function. Type is "${typeof(val)}" and value is: ${val}`);
return val;
}
function hex(val, paramName) {
if (typeof val !== 'string')
throw new PreconError(`${paramName || 'Argument'} must be a hex string. Type is "${typeof(val)}" and value is: ${val}`);
if (val.length % 2 !== 0)
throw new PreconError(`${paramName || 'Argument'} must be a hex string. Odd number of characters in value. Value is: ${val}`);
const start = val.startsWith('0x') ? 2 : 0;
for (let i = start; i < val.length; i++) {
if (HEX_CHARS.indexOf(val.charAt(i)) === -1)
throw new PreconError(`${paramName || 'Argument'} must be a hex string. Value is: ${val}`);
}
return val;
}
function instanceOf(val, instanceType, paramName) {
if (!(val instanceof instanceType))
throw new PreconError(`${paramName || 'Argument'} must be an instance of ${instanceType.constructor.name}. Is ${val && val.constructor ? val.constructor.name : val}`);
return val;
}
function integer(val, paramName) {
if (typeof val !== 'number' || isNaN(val))
throw new PreconError(`${paramName || 'Argument'} must be a number. Type is "${typeof(val)}" and value is: ${val}`);
if (!Number.isInteger(val))
throw new PreconError(`${paramName || 'Argument'} must be an integer. Value is ${val}`);
return val;
}
function map(val, paramName) {
if (!(val instanceof Map))
throw new PreconError(`${paramName || 'Argument'} must be a Map. Type is "${typeof(val)}" and value is: ${val}`);
return val;
}
function minMaxBigNumberJS(val, min, max, paramName) {
if (!BigNumber.isBigNumber(val) || val.isNaN())
throw new PreconError(`${paramName || 'Argument'} must be a BigNumber (bignumber.js) type. Type is "${typeof(val)}" and value is: ${val}`);
if (val.isLessThan(min) || val.isGreaterThan(max))
throw new PreconError(`${paramName || 'Argument'} must be greater than or equal to ${min} and less than or equal to ${max}. Value is ${val}`);
return val;
}
function minMaxInteger(val, min, max, paramName) {
if (typeof val !== 'number' || isNaN(val))
throw new PreconError(`${paramName || 'Argument'} must be a number. Type is "${typeof(val)}" and value is: ${val}`);
if (!Number.isInteger(val))
throw new PreconError(`${paramName || 'Argument'} must be an integer. Value is ${val}`);
if (val < min || val > max)
throw new PreconError(`${paramName || 'Argument'} must be greater than or equal to ${min} and less than or equal to ${max}. Value is ${val}`);
return val;
}
function minMaxNumber(val, min, max, paramName) {
if (typeof val !== 'number' || isNaN(val))
throw new PreconError(`${paramName || 'Argument'} must be a number. Type is "${typeof(val)}" and value is: ${val}`);
if (val < min || val > max)
throw new PreconError(`${paramName || 'Argument'} must be greater than or equal to ${min} and less than or equal to ${max}. Value is ${val}`);
return val;
}
function negativeBigNumberJS(val, paramName) {
if (!BigNumber.isBigNumber(val) || val.isNaN())
throw new PreconError(`${paramName || 'Argument'} must be BigNumber (bignumber.js) type. Type is "${typeof(val)}" and value is: ${val}`);
if (val.isGreaterThan(0))
throw new PreconError(`${paramName || 'Argument'} must be less than or equal to 0. Value is ${val}`);
return val;
}
function negativeInteger(val, paramName) {
if (typeof val !== 'number' || isNaN(val))
throw new PreconError(`${paramName || 'Argument'} must be a number. Type is "${typeof(val)}" and value is: ${val}`);
if (!Number.isInteger(val))
throw new PreconError(`${paramName || 'Argument'} must be an integer. Value is ${val}`);
if (val > 0)
throw new PreconError(`${paramName || 'Argument'} must be less than or equal to 0. Value is ${val}`);
return val;
}
function negativeNumber(val, paramName) {
if (typeof val !== 'number' || isNaN(val))
throw new PreconError(`${paramName || 'Argument'} must be a number. Type is "${typeof(val)}" and value is: ${val}`);
if (val > 0)
throw new PreconError(`${paramName || 'Argument'} must be less than or equal to 0. Value is ${val}`);
return val;
}
function notNull(val, paramName) {
defined(val, paramName);
if (val == null)
throw new PreconError(`${paramName || 'Argument'} must not be null.`);
return val;
}
function number(val, paramName) {
if (typeof val !== 'number' || isNaN(val))
throw new PreconError(`${paramName || 'Argument'} must be a number. Type is "${typeof(val)}" and value is: ${val}`);
return val;
}
function obj(val, paramName) {
if (typeof val !== 'object' || val === null)
throw new PreconError(`${paramName || 'Argument'} must be an object. Type is "${typeof(val)}" and value is: ${val}`);
return val;
}
function oneOf(val, optionsArr, paramName) {
for (let i = 0; i < optionsArr.length; i++) {
if (val === optionsArr[i])
return val;
}
throw new PreconError(`${paramName || 'Argument'} must be a specific value: ${optionsArr.join(', ')}, is: ${val}`);
}
function oneOfInstance(val, instanceTypeArr, paramName) {
let i;
for (i = 0; i < instanceTypeArr.length; i++) {
if (val instanceof instanceTypeArr[i])
return val;
}
throw new PreconError(`${paramName || 'Argument'} must be one of a specific instance type.`);
}
function oneOfType(val, typeArr, paramName) {
let i;
for (i = 0; i < typeArr.length; i++) {
if (typeof typeArr[i] !== 'string')
throw new PreconError('Expected string for type');
if (typeof val === typeArr[i])
return val;
}
throw new PreconError(`${paramName || 'Argument'} must be one of a specific type: ${typeArr.join(', ')}, is: ${val}`);
}
function positiveBigNumberJS(val, paramName) {
if (!BigNumber.isBigNumber(val) || val.isNaN())
throw new PreconError(`${paramName || 'Argument'} must be BigNumber (bignumber.js) type. Type is "${typeof(val)}" and value is: ${val}`);