-
Notifications
You must be signed in to change notification settings - Fork 0
/
modnarjs.ts
845 lines (818 loc) · 21.2 KB
/
modnarjs.ts
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
import { m, f, l } from "./src/names/name";
import { words } from "./src/words/words";
import { domain_arr } from "./src/net/domain";
import { country_arr } from "./src/country/country";
import { animals } from "./src/animals/animal";
import {
I_Name_Lname,
I_Name_Fname,
I_Card_Visa,
strUnd,
arrNum,
arrStr,
I_Color_RGB,
I_Card_Visa_Valid,
I_Net_Email,
I_Number_Float,
I_Number_Int,
I_NAME_CFG,
I_NAME_CFG_U,
I_Number_Array,
I_Lorem_Config,
} from "./types/type";
const typeError = (name: string, data: any, type: string) => {
console.error(`(${name}) Type Error, ${data} is not a ${type}.`);
};
class Randomly {
protected readonly MALE: string = "MALE";
protected readonly FEMALE: string = "FEMALE";
protected readonly PHONE_DEFAULT: string = "+1-###-555-####";
protected readonly DATE: Date = new Date();
protected readonly DAYS: string[] = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday",
];
protected readonly MONTHS: string[] = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
protected readonly WORDS_LENGTH: number = words.length;
protected NAME_CFG: I_NAME_CFG = {
F_NAME: f,
M_NAME: m,
LAST_NAME: l,
};
protected LOREM_CFG = {
WordsPerSentence: {
min: 4,
max: 16,
},
sentencesPerParagraph: {
min: 4,
max: 8,
},
};
// DEFAULT FUNCS
// * rnd random array index
protected rnd(arr: arrStr): number {
return Math.floor(Math.random() * (arr.length - 1));
}
// rnd number
protected numRnd(min: number, max: number): number {
return Math.floor(Math.random() * (max - min) + min);
}
// name
protected createName(gender: string, start: strUnd, end: strUnd): string {
const listOfNames: arrStr =
gender === this.MALE ? this.NAME_CFG.M_NAME : this.NAME_CFG.F_NAME;
if (start !== undefined || end !== undefined) {
const reg = new RegExp(`^${start ?? ""}.*${end ?? ""}$`, "ig");
const arr: arrNum = [];
listOfNames.forEach((value, index) => {
if (reg.test(value)) {
arr.push(index);
}
});
const index = Math.floor(Math.random() * (arr.length - 1));
arr.length > 0 ? "" : console.log(`ERROR: NO MATCH FOUND`);
return arr.length > 0 ? listOfNames[arr[index]] : "";
}
return listOfNames[this.rnd(listOfNames)];
}
protected createLastname(start: strUnd, end: strUnd): string {
if (start !== undefined || end !== undefined) {
const reg = new RegExp(`^${start ?? ""}.*${end ?? ""}$`, "ig");
const arr: arrNum = [];
this.NAME_CFG.LAST_NAME.forEach((value, index) => {
if (reg.test(value)) {
arr.push(index);
}
});
const index = Math.floor(Math.random() * (arr.length - 1));
arr.length > 0 ? "" : console.log(`ERROR: NO MATCH FOUND`);
return arr.length > 0 ? this.NAME_CFG.LAST_NAME[arr[index]] : "";
}
return this.NAME_CFG.LAST_NAME[this.rnd(this.NAME_CFG.LAST_NAME)];
}
protected createGender(): string {
const rnd = Math.round(Math.random());
return rnd == 0 ? this.MALE : this.FEMALE;
}
// animal
protected createAnimalName(): string {
const index = Math.floor(Math.random() * (animals.length - 1));
return animals[index];
}
// color
private rgb(): I_Color_RGB {
const rgb = {
r: Math.floor(Math.random() * 255),
g: Math.floor(Math.random() * 255),
b: Math.floor(Math.random() * 255),
};
return rgb;
}
private hex(): string {
return Math.floor(Math.random() * 16777215).toString(16);
}
protected createColor(format: string): number[] | undefined | string {
switch (format.toUpperCase()) {
case "RGB":
const rgb = this.rgb();
return [rgb.r, rgb.g, rgb.b];
case "RGBA":
const rgba = this.rgb();
const a = Math.random().toFixed(2) as string;
return [rgba.r, rgba.g, rgba.b, parseFloat(a)];
case "HEX":
return this.hex();
default:
break;
}
}
// phone
protected createNumberWithFormat(format: string): string {
let result: string = "";
for (let i = 0; i < format.length; i++) {
if (format[i] === "#") {
result += this.numRnd(0, 9);
continue;
}
result += format[i];
}
return result;
}
}
class Name extends Randomly {
constructor() {
super();
}
/**
* @param data - contains F_NAME, M_NAME, L_NAME
* @F_Name array of female names
* @M_Name array of male names
* @L_Name array of last names
*/
public config(data: I_NAME_CFG_U): void {
this.NAME_CFG.F_NAME = data.F_NAME ?? f;
this.NAME_CFG.M_NAME = data.M_NAME ?? m;
this.NAME_CFG.LAST_NAME = data.LAST_NAME ?? l;
}
/**
*
* @param data
* @type {name?: string | undefined, gender?: string | undefined,
* start?: string | undefined, end?: string | undefined
* }
* @returns string
*/
public fName(data?: I_Name_Fname): string {
/* Error handeling */
if (data?.start && typeof data?.start !== "string") {
typeError("start", data?.start, "string");
return "";
}
if (data?.end && typeof data?.end !== "string") {
typeError("end", data?.end, "string");
return "";
}
if (data?.name && typeof data?.name !== "string") {
typeError("name", data?.name, "string");
return "";
}
if (data?.gender && typeof data?.gender !== "string") {
typeError("gender", data?.gender, "string");
return "";
}
//
const _start: strUnd = data?.start ?? undefined;
const _end: strUnd = data?.end ?? undefined;
const _gender: strUnd =
data?.gender !== undefined
? data?.gender.toUpperCase()
: this.createGender();
const _name: strUnd =
data?.name !== undefined
? data.name
: this.createName(_gender, _start, _end);
return _name.toLowerCase();
}
/**
*
* @param data - is a object contains
* @type {name?: string | undefined, start?: string | undefined, end?: string | undefined
* }
* @returns string
*/
public lName(data?: I_Name_Lname): string {
/* Error handeling */
if (data?.start && typeof data?.start !== "string") {
typeError("start", data?.start, "string");
return "";
}
if (data?.end && typeof data?.end !== "string") {
typeError("end", data?.end, "string");
return "";
}
if (data?.name && typeof data?.name !== "string") {
typeError("name", data?.name, "string");
return "";
}
//
const _start: strUnd = data?.start ?? undefined;
const _end: strUnd = data?.end ?? undefined;
const _name: strUnd =
data?.name !== undefined ? data.name : this.createLastname(_start, _end);
return _name.toLowerCase();
}
/**
*
* @param gender - "male" or "female"
* @type ?string
* @returns string
*/
public prefix(gender?: string): string {
if (!gender) {
return this.createGender() === this.MALE ? "Mr." : "Ms.";
}
if (gender && typeof gender !== "string") {
typeError("gender", gender, "string");
return "";
}
if (
gender?.toUpperCase() === this.MALE ||
gender?.toUpperCase() === this.FEMALE
) {
return gender.toUpperCase() === this.MALE ? "Mr." : "Ms.";
} else {
console.error(
"(gender) Should be MALE or FEMALE, " + gender + " is not."
);
return "";
}
}
}
class Animal extends Randomly {
constructor() {
super();
}
/**
*
* @returns string
*/
public kind(): string {
return this.createAnimalName();
}
}
class Color extends Randomly {
constructor() {
super();
}
/**
*
* @param format
* @type ?string
* @returns number[] | undefined | string
*/
public color(format?: string) {
if (!format) {
return this.createColor("RGB");
}
if (format && typeof format !== "string") {
typeError("format", format, "string");
return "";
}
if (
format.toUpperCase() === "RGB" ||
format.toUpperCase() === "RGBA" ||
format.toUpperCase() === "HEX"
) {
return this.createColor(format);
} else {
console.error(
"(format) Should be RGB, RGBA, HEX , " + format + " is not."
);
return "";
}
}
}
class Phone extends Randomly {
constructor() {
super();
}
/**
*
* @param format
* @type ?string
* @returns string
*/
public phone(format?: string): string {
if (!format) {
return this.createNumberWithFormat(this.PHONE_DEFAULT);
}
if (format && typeof format !== "string") {
typeError("format", format, "string");
return "";
}
return this.createNumberWithFormat(format);
}
}
class Lorem extends Randomly {
constructor() {
super();
}
/**
*
* @param data
* @type {WPS?: {min?: number, max?: number}, SPP?: {min?: number, max?: number}}
*/
public config(data: I_Lorem_Config): void {
this.LOREM_CFG.WordsPerSentence.min =
data.WPS?.min ?? this.LOREM_CFG.WordsPerSentence.min;
this.LOREM_CFG.WordsPerSentence.max =
data.WPS?.max ?? this.LOREM_CFG.WordsPerSentence.max;
this.LOREM_CFG.sentencesPerParagraph.min =
data.SPP?.min ?? this.LOREM_CFG.sentencesPerParagraph.min;
this.LOREM_CFG.sentencesPerParagraph.max =
data.SPP?.max ?? this.LOREM_CFG.sentencesPerParagraph.max;
}
private method1(length: number): string {
let _result = "";
let _start: number = this.numRnd(0, words.length);
const _end = _start + length;
while (_start < _end) {
_result += `${words[_start]} `;
_start++;
}
return _result.slice(0, -1) + ".";
}
private method2(length: number): string {
let _result: string = "";
let i: number = 0;
while (i < length) {
_result += `${words[this.rnd(words)]} `;
i++;
}
return _result.slice(0, -1) + ".";
}
private lorem(words: number): string {
return words > this.WORDS_LENGTH
? this.method2(words)
: this.method1(words);
}
/**
*
* @param sentences
* @type !number
* @returns string
*/
public sentences(sentences: number): string {
if (typeof sentences !== "number") {
typeError("sentences", sentences, "number");
return "";
}
let _result: string = "";
let _i: number = 0;
let _end: number = this.numRnd(
this.LOREM_CFG.WordsPerSentence.min,
this.LOREM_CFG.WordsPerSentence.max
);
while (_i < sentences) {
_result += this.lorem(_end) + " ";
_i++;
}
return _result;
}
/**
*
* @param paragraphs
* @type !number
* @returns string
*/
public paragraphs(paragraphs: number): string {
if (typeof paragraphs !== "number") {
typeError("paragraphs", paragraphs, "number");
return "";
}
let _result: string = "";
let _i: number = 0;
let _end: number = this.numRnd(
this.LOREM_CFG.sentencesPerParagraph.min,
this.LOREM_CFG.sentencesPerParagraph.max
);
while (_i < paragraphs) {
_result += this.sentences(_end);
_i++;
}
return _result;
}
}
class CreditCard extends Name {
private DEFAULT = {
cvvLength: 3,
};
private readonly VISA_START: string = "4";
private readonly AMEX_START: arrStr = ["34", "37"];
private readonly MASTER_START: arrStr = ["51", "52", "53", "54", "55"];
constructor() {
super();
}
private creditNumber(type: string): number {
let result: string = type;
while (result.length < 15) {
const rnd = this.numRnd(0, 9);
result += rnd.toString();
}
const checksum = this.isValid(result);
result += checksum.isValid ? "0" : checksum.checksum;
return parseInt(result);
}
private holder(): string {
return `${this.fName({})} ${this.lName({})}`;
}
private expire(): string {
const d = Math.floor(Math.random() * 30) + 1;
const y: string = (
this.DATE.getFullYear() + Math.floor(Math.random() * 5)
).toString();
return `${d}/${y[2]}${y[3]}`;
}
private cvv(length: number = this.DEFAULT.cvvLength): number {
const min = 10 ** (length - 1),
max = 10 ** length - 1;
return this.numRnd(min, max);
}
/**
*
* @returns object
*/
public visaCard(): I_Card_Visa {
return {
name: "Visa",
creditNumber: this.creditNumber(this.VISA_START),
cvv: this.cvv(),
holder: this.holder(),
expire: this.expire(),
};
}
/**
*
* @returns object
*/
public masterCard(): I_Card_Visa {
return {
name: "Master",
creditNumber: this.creditNumber(
this.MASTER_START[this.numRnd(0, this.MASTER_START.length)]
),
cvv: this.cvv(),
holder: this.holder(),
expire: this.expire(),
};
}
/**
*
* @returns object
*/
public amexCard(): I_Card_Visa {
return {
name: "Amex",
creditNumber: this.creditNumber(
this.AMEX_START[this.numRnd(0, this.AMEX_START.length)]
),
cvv: this.cvv(),
holder: this.holder(),
expire: this.expire(),
};
}
// luhn algorithm
/**
* @param creaditCardNumber
* @type !string
* @returns object
*/
public isValid(creaditCardNumber: string): I_Card_Visa_Valid {
if (typeof creaditCardNumber !== "string") {
typeError("creaditCardNumber", creaditCardNumber, "string");
return { checksum: undefined, isValid: false };
}
const result: arrStr = [];
const length = creaditCardNumber.length;
let final: number = 0;
let i = 0;
// step 1
while (i < length) {
if ((i + 1) % 2 !== 0) {
result[i] = (parseInt(creaditCardNumber[i]) * 2).toString();
i++;
continue;
}
result[i] = creaditCardNumber[i];
i++;
}
// step 2
i = 0;
while (i < length) {
if (result[i].length === 2) {
result[i] = (
parseInt(result[i][0]) + parseInt(result[i][1])
).toString();
}
final += parseInt(result[i]);
i++;
}
return final % 10 === 0
? { isValid: true, checksum: undefined }
: { isValid: false, checksum: (10 - (final % 10)).toString() };
}
}
class Net extends Name {
private readonly provider: arrStr = [
"gmail.com",
"yahoo.com",
"hotmail.com",
"msn.com",
"outlook.com",
"live.com",
"me.com",
"aol.com",
"mac.com",
];
constructor() {
super();
}
// https://stackoverflow.com/questions/41914224/email-generator-javascript
private emailMethods(
num: number,
fn: string,
ln: string,
company: string
): string {
switch (num) {
case 1:
return fn + "@" + company;
case 2:
return fn + "." + ln + "@" + company;
case 3:
return fn + ln + "@" + company;
case 4:
return fn.charAt(0) + ln + "@" + company;
case 5:
return fn.charAt(0) + "." + ln + "@" + company;
case 6:
return fn + ln.charAt(0) + "@" + company;
case 7:
return fn + "." + ln.charAt(0) + "@" + company;
case 8:
return fn.charAt(0) + ln.charAt(0) + "@" + company;
case 9:
return fn + "_" + ln + "@" + company;
case 10:
return fn.charAt(0) + "_" + ln + "@" + company;
case 11:
return ln + fn + "@" + company;
case 12:
return ln + "." + fn + "@" + company;
case 13:
return ln + fn.charAt(0) + "@" + company;
case 14:
return ln + "." + fn.charAt(0) + "@" + company;
default:
return "error";
}
}
private classTypeNum(classType: string): string {
switch (classType.toUpperCase()) {
case "A":
return `${this.numRnd(1, 127)}`;
case "B":
return `${this.numRnd(128, 191)}`;
case "C":
return `${this.numRnd(192, 223)}`;
case "D":
return `${this.numRnd(224, 239)}`;
case "E":
return `${this.numRnd(240, 255)}`;
default:
return `ERROR: WRONG CLASS`;
}
}
/**
*
* @param data
* @type {firstName?: string; lastName?: string; company?: string;}
* @returns string
*/
public email(data?: I_Net_Email): string {
/* Error handeling */
if (data?.firstName && typeof data?.firstName !== "string") {
typeError("firstName", data?.firstName, "string");
return "";
}
if (data?.lastName && typeof data?.lastName !== "string") {
typeError("lastName", data?.lastName, "string");
return "";
}
if (data?.company && typeof data?.company !== "string") {
typeError("company", data?.company, "string");
return "";
}
const firstName: strUnd = data?.firstName ?? this.fName();
const lastName: strUnd = data?.lastName ?? this.lName();
const company: strUnd =
data?.company ?? this.provider[this.rnd(this.provider)];
return this.emailMethods(this.numRnd(1, 14), firstName, lastName, company);
}
/**
*
* @param classType - 'A', 'B', 'C', 'D', 'E'
* @type ?string
* @returns string
*/
public ipv4(classType?: string): string {
if (classType && typeof classType !== "string") {
typeError("classType", classType, "string");
return "";
}
return classType !== undefined
? `${this.classTypeNum(classType)}.${this.numRnd(0, 255)}.${this.numRnd(
0,
255
)}.${this.numRnd(1, 255)}`
: `${this.numRnd(0, 255)}.${this.numRnd(0, 255)}.${this.numRnd(
0,
255
)}.${this.numRnd(0, 255)}`;
}
/**
* 1487 top-level domains
* @returns string
*/
public domain(): string {
return domain_arr[this.rnd(domain_arr)].toLowerCase();
}
}
class Country extends Randomly {
constructor() {
super();
}
public country(): string {
return country_arr[this.rnd(country_arr)];
}
}
class Number extends Randomly {
private readonly DEFAULTS = {
min: 1,
max: 1000,
int: "INT",
float: "FLOAT",
};
constructor() {
super();
}
/**
*
* @param data
* @type {min?: number = 1, max?: number = 1000, decimal?: number = 2 }
* @returns number
*/
public float(data?: I_Number_Float): number {
if (data?.max && typeof data?.max !== "number") {
typeError("max", data?.max, "number");
return 0;
}
if (data?.min && typeof data?.min !== "number") {
typeError("min", data?.min, "number");
return 0;
}
if (data?.decimal && typeof data?.decimal !== "number") {
typeError("decimal", data?.decimal, "number");
return 0;
}
const _min: number = data?.min ?? this.DEFAULTS.min;
const _max: number = data?.max ?? this.DEFAULTS.max;
const _decimal: number = data?.decimal ?? 2;
return parseFloat((Math.random() * (_max - _min) + _min).toFixed(_decimal));
}
/**
*
* @param data
* @type {min?: number = 1, max?: number = 1000}
* @returns number
*/
public int(data?: I_Number_Int): number {
if (data?.max && typeof data?.max !== "number") {
typeError("max", data?.max, "number");
return 0;
}
if (data?.min && typeof data?.min !== "number") {
typeError("min", data?.min, "number");
return 0;
}
const _min: number = data?.min ?? this.DEFAULTS.min;
const _max: number = data?.max ?? this.DEFAULTS.max;
return this.numRnd(_min, _max);
}
/**
*
* @param data
* @type {min?: number = 1, max?: number = 1000,decimal?: number; type: string; length: number;}
* @returns number
*/
public array(data: I_Number_Array): number[] {
if (!data.length) {
console.error("(length) can not be undefined");
return [];
}
if (!data.type) {
console.error("(type) can not be undefined");
return [];
}
if (data.max && typeof data.max !== "number") {
typeError("max", data.max, "number");
return [];
}
if (data.min && typeof data.min !== "number") {
typeError("min", data.min, "number");
return [];
}
if (data.decimal && typeof data.decimal !== "number") {
typeError("decimal", data.decimal, "number");
return [];
}
if (data.length && typeof data.length !== "number") {
typeError("length", data.length, "number");
return [];
}
if (data.type && typeof data.type !== "string") {
typeError("type", data.type, "string");
return [];
}
const arr: number[] = [];
const type = data?.type.toUpperCase();
switch (type) {
case this.DEFAULTS.int:
for (let i = 0; i < data.length; i++) {
arr[i] = this.int({ min: data.min, max: data.max });
}
break;
case this.DEFAULTS.float:
for (let i = 0; i < data.length; i++) {
arr[i] = this.float({
min: data.min,
max: data.max,
decimal: data.decimal,
});
}
break;
}
return arr;
}
}
class Rdate extends Randomly {
constructor() {
super();
}
/**
*
* @returns string
*/
public day(): string {
return this.DAYS[this.rnd(this.DAYS)];
}
/**
*
* @returns string
*/
public month(): string {
return this.MONTHS[this.rnd(this.MONTHS)];
}
}
const modnarjs = {
name: new Name(),
animal: new Animal(),
color: new Color(),
phone: new Phone(),
card: new CreditCard(),
net: new Net(),
country: new Country(),
number: new Number(),
lorem: new Lorem(),
date: new Rdate(),
};
export = modnarjs;