@utilify/math
v1.0.0
Published
The Utilify math functions are designed to simplify common and complex mathematical operations, with full type safety and optimized performance.
Downloads
77
Maintainers
Readme
Math Utilities
The math utility functions provide methods to perform common and complex mathematical operations. These functions can be extremely helpful for number manipulation, such as calculating averages, checking prime numbers, and other mathematical tasks.
Installation
To install the math utilities package, use one of the following commands, depending on your package manager:
npm install @utilify/math
yarn add @utilify/math
pnpm add @utilify/math
Once installed, you can import the functions into your project, using ESM or CJS.
import { average, clamp, divisors } from '@utilify/math';
const { average, clamp, divisors } = require('@utilify/math');
Overview
Here is an overview of the available functions in the math utilities package:
average
function average(values: number[]): number;
Returns the arithmetic average of the provided values.
clamp
function clamp(value: number, min: number, max: number): number;
Restricts the provided value to a range between a minimum and maximum value.
divisors
function divisors(num: number): number[];
Returns an array with all divisors of a number.
factorial
function factorial(value: number): number;
Calculates the factorial of a number.
fibonacci
function fibonacci(num: number): number;
Returns the Fibonacci number at the specified position.
frequency
function frequency<T>(arr: T[]): Record<string, number>;
Returns an object with the frequency of each value in an array.
isBetween
function isBetween(value: number, min: number, max: number): boolean;
Checks if a value is within a specific range.
isEven
function isEven(value: number): boolean;
Checks if the provided number is even.
isInteger
function isInteger(value: number): boolean;
Checks if the provided value is an integer.
isOdd
function isOdd(value: number): boolean;
Checks if the provided number is odd.
isPrime
function isPrime(num: number): boolean;
Checks if the provided number is prime.
random
function random(min: number = 0, max: number = 10): number;
Returns a random number between the provided minimum and maximum values.
round
function round(value: number, precision: number): number;
Rounds a number to the specified number of decimal places.
sum
function sum(values: number[]): number;
Returns the sum of all values in an array.
sumOfDigits
function sumOfDigits(num: number): number;
Returns the sum of the digits of a number.