Check your cart for removals

Super puper DIY cart checker for decrease

by the_ghost

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.6/rx.all.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js"></script>
<div class="" />

CSS

div {
  width: 100px;
  height: 100px;
}

.warning {
  background-color: red;
}

JavaScript

var removalsFromCart = arrayValueEveryTwoSeconds([
    [1],
    [2, 3],
    [4],
    [5, 6],
    [],
    [7]
  ])
  .doAction(cart => appendText(cart.join(', ')))
  .bufferWithCount(2, 1)
  .where(history => history.length > 1 && history[1].length < history[0].length)
  .subscribe(setClassTmp('warning'))

function setClassTmp(className) {
  return () => {
    document.querySelector('div').className = className
    setTimeout(() => document.querySelector('div').className = '', 500)
  }
}

function arrayValueEveryTwoSeconds(arr) {
  return Rx.Observable
    .interval(2000)
    .take(arr.length)
    .map(i => arr[i])
}

function appendText(text) {
  document.body.appendChild(document.createTextNode(' [' + text + '] '))
}