-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-substrings-by-mask.js
230 lines (186 loc) · 5.55 KB
/
get-substrings-by-mask.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
const INCORRECT_MASK = 0;
const LOW_LEVEL = 1;
const HIGH_LEVEL = 2;
function getSubstringsByMask(mask, string, suppressErrors=true) {
try {
const disassembledMask = _disassembleMask(mask);
_checkMask(disassembledMask);
const collapsedMask = _collapseMask(disassembledMask);
const positions = _calculatePositions(collapsedMask, string);
return _extractSubstrings(collapsedMask, positions, string);
}
catch (error) {
if (!suppressErrors) {
throw error;
}
return null;
}
}
function _extractSubstrings(mask, positions, string) {
const substrings = {};
for (let idx = 0; idx < mask.length; idx++) {
let maskSegment = mask[idx];
if (typeof maskSegment === 'string') {
continue;
}
let currentPosition = positions[idx];
let stringSegment = string.substring(
currentPosition.start,
currentPosition.end + 1
);
let substringStartPosition = 0;
for (let vidx = 0; vidx < maskSegment.length; vidx++) {
let maskSubSegment = maskSegment[vidx];
let substringEndPosition = (maskSubSegment.len > -1)
? substringStartPosition + maskSubSegment.len
: stringSegment.length;
let substring = stringSegment.substring(
substringStartPosition,
substringEndPosition
);
substrings[maskSubSegment.key] = substring;
substringStartPosition = substringEndPosition;
}
}
return substrings;
}
function _calculatePositions(mask, string) {
const positions = _calculateConstantsPositions(mask, string);
_calculateVariablesPositions(positions, string.length);
return positions;
}
function _calculateConstantsPositions(mask, string) {
const positions = [];
let startPosition = 0;
for (let idx = 0; idx < mask.length; idx++) {
let currentElement = mask[idx];
if (typeof currentElement !== 'string') {
positions.push(null);
continue;
}
let foundIndex = string.indexOf(currentElement, startPosition);
if (foundIndex === -1) {
throw new Error(`The string doesn't match to the mask`);
}
positions.push({
start: foundIndex,
end: foundIndex + (currentElement.length - 1)
});
startPosition = foundIndex + currentElement.length;
}
return positions;
}
function _calculateVariablesPositions(positions, strLength) {
for (let idx = 0; idx < positions.length; idx++) {
let currentPosition = positions[idx];
if (currentPosition !== null) {
continue;
}
let isFirstElement = (idx === 0);
let isLastElement = (idx === positions.length - 1);
let previousElement = (!isFirstElement) ? positions[idx - 1] : {end: -1};
let nextElement = (!isLastElement) ? positions[idx + 1] : {start: strLength};
positions[idx] = {
start: previousElement.end + 1,
end: nextElement.start - 1
};
}
}
function _collapseMask(mask) {
const collapsedMask = [];
for (let idx = 0; idx < mask.length; idx++) {
let currentElement = mask[idx];
if (typeof currentElement === 'string') {
collapsedMask.push(currentElement);
continue;
}
let lastCollapsedElement = (collapsedMask.length > 0)
? collapsedMask[collapsedMask.length - 1] : '';
let keyObject = {
key: currentElement[0],
len: (currentElement.length > 1) ? Number(currentElement[1]) : -1
};
if (Array.isArray(lastCollapsedElement)) {
lastCollapsedElement.push(keyObject);
}
else {
collapsedMask.push([keyObject]);
}
}
return collapsedMask;
}
function _checkMask(mask) {
const keys = {};
for (let idx = 0; idx < mask.length; idx++) {
let currentElement = mask[idx];
if (typeof currentElement === 'string') {
continue;
}
let key = currentElement[0];
let valueLength = (currentElement.length > 1) ? currentElement[1] : 0;
if (key.length === 0) {
throw new Error(`A key can't be an empty string`);
}
if (key in keys) {
throw new Error(`Found a duplicated key: ${key}`);
}
if (Number.isNaN(Number(valueLength))) {
throw new Error(`Incorrect value length: ${valueLength}`);
}
keys[key] = key;
}
}
function _disassembleMask(mask) {
const metaSymbols = '{|}';
const parts = [];
let currentLevel = 2;
let part = '';
for (let idx = 0; idx < mask.length; idx++) {
let currentSymbol = mask.charAt(idx);
if (metaSymbols.indexOf(currentSymbol) === -1) {
part += currentSymbol;
continue;
}
let newLevel = _checkLevel(currentLevel, currentSymbol);
if (newLevel === INCORRECT_MASK) {
throw new Error(`Mask has an error at index: ${idx}`);
}
if (currentLevel === HIGH_LEVEL && newLevel === LOW_LEVEL) {
if (part.length > 0) {
parts.push(part);
}
parts.push([]);
}
else if (currentLevel === LOW_LEVEL) {
let lastPart = parts[parts.length - 1];
lastPart.push(part);
}
part = '';
currentLevel = newLevel;
}
if (part.length > 0) {
parts.push(part);
part = '';
}
return parts;
}
function _checkLevel(currentLevel, currentMetasymbol) {
let result = currentLevel;
if (currentMetasymbol === '{' && currentLevel === 2) {
result = LOW_LEVEL;
}
else if (currentMetasymbol === '{' && currentLevel === 1) {
result = INCORRECT_MASK;
}
else if (currentMetasymbol === '}' && currentLevel === 2) {
result = INCORRECT_MASK;
}
else if (currentMetasymbol === '}' && currentLevel === 1) {
result = HIGH_LEVEL;
}
else if (currentMetasymbol === '|' && currentLevel === 2) {
result = INCORRECT_MASK;
}
return result;
}
module.exports = getSubstringsByMask;