-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_util.bal
37 lines (33 loc) · 1.29 KB
/
common_util.bal
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
import ballerina/regex;
import ballerina/random;
import ballerina/lang.'string as strings;
isolated function generateTopicName(string topic) returns string {
return nomalizeString(topic);
}
isolated function generateGroupName(string topic, string callbackUrl) returns string {
string idValue = topic + ":::" + callbackUrl;
return nomalizeString(idValue);
}
isolated function nomalizeString(string baseString) returns string {
return regex:replaceAll(baseString, "[^a-zA-Z0-9]", "_");
}
isolated function generateRandomString() returns string {
int[] codePoints = [];
int leftLimit = 48; // numeral '0'
int rightLimit = 122; // letter 'z'
int iterator = 0;
while iterator < 10 {
int|error randomInt = random:createIntInRange(leftLimit, rightLimit);
if randomInt is error {
break;
} else {
// character literals from 48 - 57 are numbers | 65 - 90 are capital letters | 97 - 122 are simple letters
if (randomInt <= 57 || randomInt >= 65) && (randomInt <= 90 || randomInt >= 97) {
codePoints.push(randomInt);
iterator += 1;
}
}
}
string|error generatedValue = strings:fromCodePointInts(codePoints);
return generatedValue is string ? generatedValue : "";
}