forked from mrmalhotra/meaningful-string
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
170 lines (129 loc) · 4.37 KB
/
index.js
File metadata and controls
170 lines (129 loc) · 4.37 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
const animals = require('./assets/animals');
const adjectives = require('./assets/adjectives');
const emojis = require('./assets/emojis');
const number = '1234567890';
const smallCaseAlphabats = 'abcdefghijklmnopqrstuvwxyz';
const upperCaseAlphabats = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const smallAlphanumeric = 'abcdefghijklmnopqrstuvwxyz0123456789';
const upperAlphanumeric = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ123409876';
const mixedChars = 'NOPQRmDTBCMljLSIJkno3p051F787643G8EArKqH';
function getRandom(object) {
let string = '';
let referralString = mixedChars;
let lengthShouldBeAtlast = mixedChars.length;
let lengthShouldBeAtleast = 4;
let lengthShouldBe = lengthShouldBeAtlast;
if (object && object.custom)
referralString = object.custom;
if (object && object.allCaps)
referralString = upperCaseAlphabats;
if (object && object.allSmall)
referralString = smallCaseAlphabats;
if (object && object.capsWithNumbers)
referralString = upperAlphanumeric;
if (object && object.smallWithNumbers)
referralString = smallAlphanumeric;
if (object && object.onlyNumbers)
referralString = number;
if (object && object.max)
lengthShouldBeAtlast = parseInt(object.max);
if (object && object.min)
lengthShouldBeAtleast = parseInt(object.min);
if (lengthShouldBeAtleast > lengthShouldBeAtlast) {
var temp = lengthShouldBeAtleast;
lengthShouldBeAtleast = lengthShouldBeAtlast;
lengthShouldBeAtlast = temp;
console.log('checking swaping value ', lengthShouldBeAtleast, ' last ', lengthShouldBeAtlast)
}
if (object && object.max && object.min)
lengthShouldBe = Math.floor(Math.random() * (object.max - object.min + 1)) + object.min;
if (object && object.charLength && !object.fromShortId)
lengthShouldBe = parseInt(object.charLength);
if (object && object.fromShortId)
if (object.charLength >= 3 && object.charLength <= 8)
lengthShouldBe = parseInt(object.charLength);
for (let i = 0; i < lengthShouldBe; i++) {
let val = Math.floor(Math.random() * referralString.length - 1) + 1;
string += referralString[val];
}
if (object && object.startWith)
string = object.startWith + string;
if (object && object.endWith)
string = string + object.endWith;
return string;
}
// returns random string according to the params
random = (object) => {
return getRandom(object);
};
// returns a meaningful random string generated with the combination of animal-adjective-emoji-numbers
meaningful = (object) => {
let order = [];
let upto = 1000;
let joinString = '-';
let maxLength = 20;
let chooseAnimal = animals.animals[~~(Math.random() * animals.animals.length)];
let chooseAdjective = adjectives.adjectives[~~(Math.random() * adjectives.adjectives.length)];
let chooseEmoji = emojis.emojis[~~(Math.random() * emojis.emojis.length)]
if (object && object.numberUpto > 0)
upto = parseInt(object.numberUpto);
if (object && object.joinBy)
joinString = object.joinBy;
if (object && object.maxLength > 0)
maxLength = parseInt(object.maxLength);
let chooseNumber = Math.floor(Math.random() * upto);
order.push(chooseAdjective);
order.push(chooseAnimal);
order.push(chooseEmoji);
order.push(chooseNumber);
let res = order.join(joinString);
if (res.length > maxLength) {
return meaningful(object);
} else {
return res;
}
};
shortId = (object) => {
var empty = {};
if (object)
object.fromShortId = true;
if (object && !object.charLength && !object.min && object.max) {
object.max = 4;
object.min = 4;
}
if (object && object.charLength === 0) {
object.min = 3;
object.max = 3;
}
if ((object && object.charLength) && (parseInt(object.charLength) > 8)) {
object.max = 8;
object.min = 8
}
if (object && object.charLength && parseInt(object.charLength) < 3) {
object.max = 3;
object.min = 3;
}
if ((object && object.charLength) && (parseInt(object.charLength) >= 3 && parseInt(object.charLength) <= 8)) {
var value = parseInt(object.charLength);
object.max = value;
object.min = value;
}
if (object && object.startWith) {
object.startWith = null;
}
if (object && object.endWith) {
object.endWith = null;
}
if (!object) {
empty.max = 4;
empty.min = 4;
empty.fromShortId = true;
return getRandom(empty);
}
return getRandom(object);
}
module.exports = {
random,
meaningful,
shortId
}