partition.js 449 B

12345678910111213141516171819202122
  1. "use strict";
  2. module.exports = (arr_, pred) => {
  3. const arr = arr_ || [],
  4. spans = []
  5. let span = { label: undefined,
  6. items: [arr.first] }
  7. arr.forEach (x => {
  8. const label = pred (x)
  9. if ((span.label !== label) && span.items.length) {
  10. spans.push (span = { label: label, items: [x] }) }
  11. else {
  12. span.items.push (x) } })
  13. return spans
  14. }