-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_highlighter.js
52 lines (40 loc) · 1.09 KB
/
string_highlighter.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
function spanInserter(str, markers) {
var prev = 0;
var newstr = markers.reduce(function (newstr, m) {
newstr += str.substring(prev, m[0]) + "<span>"+str.substring(m[0], m[1])+"</span>";
prev = m[1];
return newstr;
}, "")
newstr += str.substring(prev);
return newstr;
}
describe('string insertion', function() {
it('should insert spans into the string', function() {
var input = 'hello world, this is first span';
// set markers
var markers = [
[6, 11],
[13, 17]
];
var expected = 'hello <span>world</span>, <span>this</span> is first span';
var result = spanInserter(input, markers);
expect(result).toBe(expected);
});
});
/* function spanInserter(str, markers) {
var out_str = [],
marker;
str = str.split("");
marker = markers.shift();
for (var i = 0; i < str.length; i++) {
if (marker) {
if ( i == marker[0]) out_str.push("<span>");
else if (i == marker[1]) {
out_str.push("</span>");
marker = markers.shift();
}
}
out_str.push(str[i]);
}
return out_str.join("");
} */