sp-number-kit
v1.0.1
Published
A small collection of handy number utility functions - even/odd, prime, factorial, gcd, lcm, clamp, average, and more.
Maintainers
Readme
sp-number-kit
A small collection of handy number utility functions — even/odd checks, primes, factorial, gcd, lcm, clamping, averages, and rounding.
Installation
npm install sp-number-kitUsage
const {
isEven,
isOdd,
isPrime,
isPositive,
isNegative,
factorial,
gcd,
lcm,
randomInRange,
clamp,
average,
roundTo,
} = require('sp-number-kit');
isEven(4); // true
isOdd(3); // true
isPrime(7); // true
isPositive(5); // true
isNegative(-5); // true
factorial(5); // 120
gcd(12, 18); // 6
lcm(4, 6); // 12
randomInRange(1, 10); // random integer between 1 and 10
clamp(15, 0, 10); // 10
average([1, 2, 3, 4]); // 2.5
roundTo(3.14159, 2); // 3.14API
| Function | Description |
|---|---|
| isEven(num) | Returns true if num is even |
| isOdd(num) | Returns true if num is odd |
| isPrime(num) | Returns true if num is a prime number |
| isPositive(num) | Returns true if num is greater than 0 |
| isNegative(num) | Returns true if num is less than 0 |
| factorial(num) | Returns the factorial of a non-negative integer |
| gcd(a, b) | Returns the greatest common divisor of a and b |
| lcm(a, b) | Returns the least common multiple of a and b |
| randomInRange(min, max) | Returns a random integer between min and max (inclusive) |
| clamp(num, min, max) | Restricts num to lie within min and max |
| average(arr) | Returns the average of an array of numbers |
| roundTo(num, decimals) | Rounds num to the given number of decimal places |
License
MIT
