var ranges = [{
"id": 1,
"from": 0,
"to": 100
}, {
"id": 2,
"from": 30,
"to": 30
}, {
"id": 3,
"from": 50,
"to": 200
}, {
"id": 4,
"from": 0,
"to": 100
}, {
"id": 5,
"from": 70,
"to": 200
}, {
"id": 6,
"from": 90,
"to": 300
}]
var nonOverlapping = [{
"id": 1,
"from": 0,
"to": 29
}, {
"id": 4,
"from": 0,
"to": 29
}, {
"id": 1,
"from": 30,
"to": 30
}, {
"id": 2,
"from": 30,
"to": 30
}, {
"id": 4,
"from": 30,
"to": 30
}, {
"id": 1,
"from": 31,
"to": 49
}, {
"id": 4,
"from": 31,
"to": 49
}, {
"id": 1,
"from": 50,
"to": 69
}, {
"id": 3,
"from": 50,
"to": 69
}, {
"id": 4,
"from": 50,
"to": 69
}, {
"id": 3,
"from": 70,
"to": 89
}, {
"id": 4,
"from": 70,
"to": 89
}, {
"id": 5,
"from": 70,
"to": 89
}, {
"id": 1,
"from": 70,
"to": 89
}, {
"id": 3,
"from": 90,
"to": 100
}, {
"id": 4,
"from": 90,
"to": 100
}, {
"id": 5,
"from": 90,
"to": 100
}, {
"id": 6,
"from": 90,
"to": 100
}, {
"id": 1,
"from": 90,
"to": 100
}, {
"id": 3,
"from": 101,
"to": 200
}, {
"id": 5,
"from": 101,
"to": 200
}, {
"id": 6,
"from": 101,
"to": 200
}, {
"id": 6,
"from": 201,
"to": 300
}]
var result = [];
var firstItem = null;
for (var c = 0; c < items.length; c++) {
if (!firstItem) {
firstItem = items[c];
} else if (firstItem.value === items[c].value) {
if (firstItem.type !== items[c].type) {
result.push({
from: firstItem.value,
to: items[c].value
});
firstItem = items[c];
} else {
}
} else if (firstItem.type === 'start' && items[c].type === 'start') {
result.push({
from: firstItem.value,
to: items[c].value - 1
});
firstItem = items[c];
} else if (firstItem.type === 'start' && items[c].type === 'end') {
result.push({
from: firstItem.value,
to: items[c].value
});
firstItem = items[c];
} else if (firstItem.type === 'end' && items[c].type === 'end') {
result.push({
from: firstItem.value + 1,
to: items[c].value
});
firstItem = items[c];
} else if (firstItem.type === 'end' && items[c].type === 'start') {
result.push({
from: firstItem.value + 1,
to: items[c].value - 1
});
firstItem = items[c];
}
}