JSFiddle - React, Tailwind, and code Playground
by Artem
JavaScript
'use strict';
const annotationGroup0 = { label: 'Type A', offsets: [0, 1] };
const annotationGroup1 = { label: 'Type B', offsets: [3, 4] };
const annotationGroup2 = { label: 'Type C', offsets: [6, 7, 8, 9, 10, 11] };
const groups = [annotationGroup2, annotationGroup1];
const arr = [{
text: 'Hi',
annotationGroup: null,
index: 0,
},
{
text: 'My name is',
annotationGroup: null,
index: 1,
},
{
text: 'Illuxa',
annotationGroup: null,
index: 2,
},
{
text: 'Hello',
annotationGroup: annotationGroup1,
index: 3,
},
{
text: 'world',
annotationGroup: annotationGroup1,
index: 4,
},
{
text: 'Bye',
annotationGroup: null,
index: 5,
},
{
text: 'Python',
annotationGroup: annotationGroup2,
index: 6,
},
{
text: 'is',
annotationGroup: annotationGroup2,
index: 7,
},
{
text: 'the',
annotationGroup: annotationGroup2,
index: 8,
},
{
text: 'best',
annotationGroup: annotationGroup2,
index: 9,
},
{
text: 'programming',
annotationGroup: annotationGroup2,
index: 10,
},
{
text: 'language',
annotationGroup: annotationGroup2,
index: 11,
},
{
text: 'Fin',
annotationGroup: null,
index: 12,
},
{
text: '!',
annotationGroup: null,
index: 13,
},
];
/* const iterator = function*() {
let res = [];
for (let item of arr) {
if (item.annotationGroup) {
res.push(item);
} else {
if (res.length > 0) {
yield res;
res = [];
res.push(item);
yield res;
res = [];
} else {
res.push(item);
yield res;
res = [];
}
}
}
}; */
const iterator = function*() {
let res = [...arr];
for (const group of groups) {
res.splice(group.offsets[0], 0, { ...group, tokens: group.offsets.map(offset => arr[offset]) });
res = res.filter(item => !group.offsets.includes(item.index));
}
for (const...