Package Exports
- @hridel/modulo
 - @hridel/modulo/dist/index.js
 
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@hridel/modulo) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
modulo
A robust modular arithmetic utility for JavaScript, handling both positive and negative integers with ease. Ensure consistent, non-negative results for your modulo operations. Perfect for mathematical computations, circular buffer implementations, and more.
Installation
npm install @hridel/modulomod function example
import { mod } from '@hridel/modulo';
const exampleResult: number = mod(-1, 5);
console.log(`The result of -1 mod 5 is: ${exampleResult}`);  // The result of -1 mod 5 is: 4rem function example
import { rem } from '@hridel/modulo';
// Example usage
const exampleResult: number = rem(-1, 5);
console.log(`The result of -1 rem 5 is: ${exampleResult}`);  // The result of -1 rem 5 is: -1div function example
import { div } from '@hridel/modulo';
// Example usage
const exampleResult: number = div(-1, 5);
console.log(`The result of -1 div 5 is: ${exampleResult}`);  // The result of -1 div 5 is: -1Why to use this library?
Because (-1 % 5) in JavaScript returns -1, which is not a valid result for a modulo operation.
This library ensures that the result of the mod function is always a non-negative integer.
The rem function returns the remainder of the division operation, which can be negative.
And the div function returns the quotient rounded towards negative infinity, similar to Haskell's div.