JSFiddle - React, Tailwind, and code Playground
by kentaromiura
JavaScript
var test = [{
start: 0,
end: 100
}, {
start: 0,
end: 50
}, {
start: 0,
end: 50
}, {
start: 50,
end: 100
}]
_test = [{
start: 30,
end: 150
}, {
start: 540,
end: 600
}, {
start: 560,
end: 620
}, {
start: 610,
end: 670
}]
_test = [{
id: 1,
start: 0,
end: 45
}, {
id: 2,
start: 90,
end: 180
}, {
id: 3,
start: 15,
end: 180
}, {
id: 4,
start: 400,
end: 700
}, {
id: 5,
start: 600,
end: 720
}]
_test = [{
id: 1,
start: 0,
end: 60
}, {
id: 5,
start: 15,
end: 100
}, {
id: 4,
start: 30,
end: 170
}, {
id: 2,
start: 70,
end: 130
}, {
id: 6,
start: 110,
end: 160
}, {
id: 3,
start: 140,
end: 200
}, {
id: 7,
start: 600,
end: 700
}]
_test = [{
start: 0,
end: 70
}, {
start: 30,
end: 70
}, {
start: 30,
end: 70
}, {
start: 50,
end: 100
}, {
start: 70,
end: 120
}, {
start: 70,
end: 120
}, {
start: 70,
end: 120
}, {
start: 70,
end: 120
}]
function Blah(x, index) {
this.start = x.start
this.end = x.end
this.index = index
}
Blah.prototype.collides = function (min, max) {
var start = this.start,
end = this.end
//reverse logic is simpler and better
if (max <= start) return false
if (min >= end) return false
return true
}
Blah.prototype.collidesWithBlah = function (blah) {
return this.collides(blah.start, blah.end)
}
var x = test.sort(function (a, b) {
if (a.start < b.start) return false
if (a.start == b.start && a.end > b.end) return false
return true
}).map(function (x, index) {
return new Blah(x, index)
});
(function (global) {
function groupify(ungrouped) {
var output = [
[]
],
index = 0,
currentGroup = output[index]
currentGroup.push(ungrouped.shift())
...