mod-loop

0.0.2 • Public • Published

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 = require('mod-loop')
 
mod(+100, 10) // 0
mod(-100, 10) // 0
mod(-105, 10) // 5
mod(-102, 10) // 8

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.0.2
    40
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 0.0.2
    40
  • 0.0.1
    1
  • 0.0.0
    13

Package Sidebar

Install

npm i mod-loop

Weekly Downloads

54

Version

0.0.2

License

MIT

Last publish

Collaborators

  • hughsk