1const text = 'This is a test to see whether nested style spans work properly.'
2const styling = [
3 {start: 22, end: 54, type: "strong"},
4 {start: 30, end: 36, type: "a"},
5 {start: 37, end: 48, type: "em"},
6 {start: 43, end: 48, type: "a"}
7];
8
9const result = [...text].reduce((a, v, i) => {
10 styling.filter(s => s.start === i).forEach(s => a += `<${s.type}>`);
11 styling.filter(s => s.end === i).forEach(s => a += `</${s.type}>`);
12 return a + v;
13}, '');
14
15document.body.innerHTML = result;
16
17// EXMAPLE FROM STACKOVERFLOW.COM //
18// OUTPUT:
19// This is a test to see <strong>whether <a>nested</a> <em>style <a>spans</em></a> work </strong>properly.