mod-loop
JavaScript's modulo operator (%
) doesn't handle negative numbers nicely -
here's something general to work around the issue.
Benchmarks on jsperf can be found here. Thanks to shama and mikolalysenko for finding a faster method.
Installation
npm install mod-loop
Usage
require('mod-loop')(x, y)
Returns x % y
, for both positive and negative and negative numbers.
var mod =// 0// 0// 5// 8