shoveit

shuffle an array of elements

Usage no npm install needed!

<script type="module">
  import shoveit from 'https://cdn.skypack.dev/shoveit';
</script>

README

shoveit

Take an array of elements that have either points or ranges defined, and shove them into an array of arrays where the elements ranges and points do not overlap. Here an visualization of the idea:

-aaa----------
-------bbbbb--
--cccc--------
----d---dd----
----e-------e-

=>

-aaae--bbbbbe-
--cccc--------
----d---dd----


var array = [{
    name: 'a',
    range: [2, 4]
}, {
    name: 'b',
    range: [8, 12]
}, {
    name: 'c',
    range: [3, 6]
}, {
    name: 'd',
    points: [5, 9,10]
}];

console.log(shove(array));

[ [ { name: 'a', range: [Object] },
    { name: 'b', range: [Object] } ],
  [ { name: 'c', range: [Object] },
    { name: 'd', points: [Object] } ] ]

The algorithm is not that smart so it wont always get the optimal shuffle, but thats okay.