➗ Math Utilities
Math utilities provide essential tools for common mathematical operations. These helpers simplify calculations, clamping, rounding, and statistical analysis.
📚 Quick Reference
| Method | Description |
|---|---|
abs | Get the absolute value of a number. |
add | Add two numbers with precision handling. |
subtract | Subtract one number from another. |
multiply | Multiply a number by a scalar. |
divide | Divide a number by a divisor. |
allocate | Distribute amount proportionally by ratios. |
distribute | Distribute amount evenly among N parties. |
sum | Sum all values in an array. |
average | Calculate the average of an array of numbers. |
median | Find the median value in an array of numbers. |
min | Find the minimum value in an array. |
max | Find the maximum value in an array. |
clamp | Clamp a number between a minimum and maximum value. |
round | Round a number to a specific decimal precision. |
range | Generate an array of numbers in a given range. |
rate | Calculate a percentage or rate. |
boil | Reduce an array to a single value using a custom comparator. |
💡 Practical Examples
Statistical Helpers
ts
import { sum, average, median, min, max } from '@vielzeug/toolkit';
const data = [10, 2, 38, 23, 38, 8, 15];
sum(data); // 134
average(data); // 19.14...
median(data); // 15
min(data); // 2
max(data); // 38Formatting & Constraints
ts
import { clamp, round, range } from '@vielzeug/toolkit';
// Clamp values (useful for UI sliders or bounds)
clamp(105, 0, 100); // 100
clamp(-5, 0, 100); // 0
// Round to precision
round(Math.PI, 4); // 3.1416
// Generate ranges
range(1, 5); // [1, 2, 3, 4, 5]
range(0, 10, 2); // [0, 2, 4, 6, 8, 10]