Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mololab
GitHub Repository: mololab/json-translator
Path: blob/master/src/core/ignorer.ts
235 views
1
export function map(
2
str: string
3
): {
4
word: string;
5
double_brackets_map: { [key: string]: string };
6
single_brackets_map: { [key: string]: string };
7
} {
8
// encode urls if exists in the str
9
str = urlEncoder(str);
10
11
let {
12
map: double_brackets_map,
13
word: initial_ignored_word,
14
} = mapByDoubleBracket(str);
15
let { map: single_brackets_map, word: ignored_word } = mapBySingleBracket(
16
initial_ignored_word
17
);
18
19
return {
20
word: ignored_word,
21
double_brackets_map,
22
single_brackets_map: single_brackets_map,
23
};
24
}
25
26
export function unMap(
27
str: string,
28
double_brackets_map: object,
29
single_brackets_map: object
30
): string {
31
let word = unmapBySingleBracket(str, single_brackets_map);
32
word = unmapByDoubleBracket(word, double_brackets_map);
33
34
// decode urls if exists in the str
35
word = urlDecoder(word);
36
37
return word;
38
}
39
40
function mapBySingleBracket(
41
str: string
42
): { word: string; map: { [key: string]: string } } {
43
return mapIgnoredValues(str, '{', '}', '{', '}');
44
}
45
46
function unmapBySingleBracket(str: string, map: object): string {
47
return unmapIgnoredValues(str, map, '{', '}', '{', '}');
48
}
49
50
function mapByDoubleBracket(
51
str: string
52
): { word: string; map: { [key: string]: string } } {
53
return mapIgnoredValues(str, '{{', '}}', '{', '}');
54
}
55
56
function unmapByDoubleBracket(str: string, map: object): string {
57
return unmapIgnoredValues(str, map, '{{', '}}', '{', '}');
58
}
59
60
function mapIgnoredValues(
61
str: string,
62
start: string,
63
end: string,
64
replaced_start: string,
65
replaced_end: string
66
): { word: string; map: { [key: string]: string } } {
67
let counter = 0;
68
let map: { [key: string]: string } = {};
69
70
let regex = new RegExp(`${start}(.*?)${end}`, 'g');
71
72
let new_str = str.replace(regex, function(word) {
73
word = word.substring(start.length, word.length - end.length);
74
75
// const key = "*".repeat(counter)
76
const key = counter;
77
78
map[`${key}`] = word;
79
80
let locked_ignored = replaced_start + key + replaced_end;
81
82
counter++;
83
return locked_ignored;
84
});
85
86
return { word: new_str, map: map };
87
}
88
89
function unmapIgnoredValues(
90
str: string,
91
map: object,
92
start: string,
93
end: string,
94
replaced_start: string,
95
replaced_end: string
96
): string {
97
for (const [key, value] of Object.entries(map)) {
98
let for_replace = replaced_start + key + replaced_end;
99
100
str = str.replace(for_replace, start + value + end);
101
}
102
103
return str;
104
}
105
106
// URL detector & encode AND decoder
107
function urlEncoder(text: string): string {
108
// url finder regex => url
109
const regex = /(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!;:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!;:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!;:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/gim;
110
111
let new_text = text.replace(regex, function(url) {
112
url = `{` + url + `}`;
113
return url;
114
});
115
116
return new_text;
117
}
118
119
function urlDecoder(text: string): string {
120
// url finder regex => {url}
121
const regex = /{(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!;:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!;:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!;:,.]*\)|[A-Z0-9+&@#\/%=~_|$])}/gim;
122
123
let new_text = text.replace(regex, function(url) {
124
url = url.substring(1, url.length - 1);
125
return url;
126
});
127
128
return new_text;
129
}
130
131