-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.brackets2.js
More file actions
104 lines (89 loc) · 2.7 KB
/
string.brackets2.js
File metadata and controls
104 lines (89 loc) · 2.7 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
/*
* @title: String parse brackets
* @description: simple function to parse brackets out of input
* @author: Thorsten Kober
* @email: [email protected]
*/
/* Given a string, parse it and return a string array.
For exmple, string="abc(edf)hij{klmn}opq[rst]uvw"
1.) The delimitors are (), {}, []. They are in pair.
output: ["abc", "edf", "hij", "klmn", "opq", "rst", "uvw"]
2.) If any two consecutive "(" means escaping, that is "((" is actually
output char "(". It's not part of the delimitor.
Similar to ")", "{", "}", "[", "]".
abc(e))df) = ["abc", "e)df"], since the "))" outputs ")".
3.) if "{" is inside a delimitor pair (), then "{" isn't part of the
delimitor. Output it as is.
abc(e{df}}g) = ["abc", "e{df}}g"]
*/
function parseString(str) {
const options = '(){}[]';
let position;
let current = [];
const stack = [];
const result = [];
for (let i = 0; i < str.length; i++) {
const char = str[i];
position = options.indexOf(char);
if (position === -1) {
// no option found;
current.push(char);
continue; // eslint-disable-line
}
if (position % 2 === 0) {
// found opening bracket
if (stack.length === 0) {
stack.push(position + 1);
result.push(current.join(''));
current = [];
} else {
current.push(char);
}
} else {
// found closing bracket
// closing bracket in the beginning or
// without a previous opening bracket
// push it in and continue
if (stack.length === 0) {
current.push(char);
continue // eslint-disable-line
}
const next = str[i + 1];
const prev = str[i - 1];
// const close = options[stack[0]];
const open = options[stack[0] - 1];
if (next === char) {
current.push(char);
continue; // eslint-disable-line
} else if (prev === char) {
if (open === '(' && char !== ')') {
current.push(char);
}
continue; // eslint-disable-line
} else {
result.push(current.join(''));
current = [];
stack.pop();
}
}
}
if (current.length > 0) {
result.push(current.join(''));
}
return result;
}
// npx jest algorithms/string/string.brackets2.js
describe('parsing out brackets from string', () => {
it('should remove brackets', () => {
expect(parseString('abc(edf)hij{klmn}opq[rst]uvw'))
.toEqual(['abc', 'edf', 'hij', 'klmn', 'opq', 'rst', 'uvw']);
});
it('should remove brackets', () => {
expect(parseString('abc(e))df)'))
.toEqual(['abc', 'e)df']);
});
it('should remove brackets', () => {
expect(parseString('abc(e{df}}g)'))
.toEqual(['abc', 'e{df}}g']);
});
});