math-utils-simple 1.2.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "math-utils-simple",
3
- "version": "1.2.0",
3
+ "version": "2.0.0",
4
4
  "description": "A simple math utility library for npm",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  // ARITHMETIC
2
-
3
2
  export { default as add } from "./arithmetic/add.js";
4
3
  export { default as subtract } from "./arithmetic/subtract.js";
5
4
  export { default as multiply } from "./arithmetic/multiply.js";
@@ -18,7 +17,6 @@ export { default as cube } from "./arithmetic/cube.js";
18
17
  export { default as reciprocal } from "./arithmetic/reciprocal.js";
19
18
 
20
19
  // NUMBER
21
-
22
20
  export { default as digitalRoot } from "./number/digitalRoot.js";
23
21
  export { default as digitCount } from "./number/digitCount.js";
24
22
  export { default as divisors } from "./number/divisors.js";
@@ -41,4 +39,19 @@ export { default as nthFibonacci } from "./number/nthFibonacci.js";
41
39
  export { default as previousPrime } from "./number/previousPrime.js";
42
40
  export { default as primeFactors } from "./number/primeFactors.js";
43
41
  export { default as reverseNumber } from "./number/reverseNumber.js";
44
- export { default as sumOfDigits } from "./number/sumOfDigits.js";
42
+ export { default as sumOfDigits } from "./number/sumOfDigits.js";
43
+
44
+ // ROUNDING
45
+ export { default as round } from "./rounding/round.js";
46
+ export { default as floor } from "./rounding/floor.js";
47
+ export { default as ceil } from "./rounding/ceil.js";
48
+ export { default as truncate } from "./rounding/truncate.js";
49
+ export { default as roundTo } from "./rounding/roundTo.js";
50
+ export { default as clamp } from "./rounding/clamp.js";
51
+ export { default as roundUpToNearest } from "./rounding/roundUpToNearest.js";
52
+ export { default as roundDownToNearest } from "./rounding/roundDownToNearest.js";
53
+ export { default as roundToNearest } from "./rounding/roundToNearest.js";
54
+ export { default as snap } from "./rounding/snap.js";
55
+ export { default as isBetween } from "./rounding/isBetween.js";
56
+ export { default as wrap } from "./rounding/wrap.js";
57
+ export { default as normalize } from "./rounding/normalize.js";
@@ -0,0 +1,3 @@
1
+ export default function ceil(n) {
2
+ return Math.ceil(n);
3
+ }
@@ -0,0 +1,7 @@
1
+ export default function clamp(value, min, max) {
2
+ if (min > max) {
3
+ throw new Error("min cannot be greater than max.");
4
+ }
5
+
6
+ return Math.min(Math.max(value, min), max);
7
+ }
@@ -0,0 +1,3 @@
1
+ export default function floor(n) {
2
+ return Math.floor(n);
3
+ }
@@ -0,0 +1,11 @@
1
+ export default function isBetween(value, min, max, inclusive = true) {
2
+ if (min > max) {
3
+ throw new Error("min cannot be greater than max.");
4
+ }
5
+
6
+ if (inclusive) {
7
+ return value >= min && value <= max;
8
+ }
9
+
10
+ return value > min && value < max;
11
+ }
@@ -0,0 +1,7 @@
1
+ export default function normalize(value, min, max) {
2
+ if (min === max) {
3
+ throw new Error("min and max cannot be equal.");
4
+ }
5
+
6
+ return (value - min) / (max - min);
7
+ }
@@ -0,0 +1,3 @@
1
+ export default function round(n) {
2
+ return Math.round(n);
3
+ }
@@ -0,0 +1,7 @@
1
+ export default function roundDownToNearest(value, multiple) {
2
+ if (multiple === 0) {
3
+ throw new Error("multiple cannot be zero.");
4
+ }
5
+
6
+ return Math.floor(value / multiple) * multiple;
7
+ }
@@ -0,0 +1,4 @@
1
+ export default function roundTo(n, decimals = 0) {
2
+ const factor = 10 ** decimals;
3
+ return Math.round(n * factor) / factor;
4
+ }
@@ -0,0 +1,7 @@
1
+ export default function roundToNearest(value, multiple) {
2
+ if (multiple === 0) {
3
+ throw new Error("multiple cannot be zero.");
4
+ }
5
+
6
+ return Math.round(value / multiple) * multiple;
7
+ }
@@ -0,0 +1,7 @@
1
+ export default function roundUpToNearest(value, multiple) {
2
+ if (multiple === 0) {
3
+ throw new Error("multiple cannot be zero.");
4
+ }
5
+
6
+ return Math.ceil(value / multiple) * multiple;
7
+ }
@@ -0,0 +1,7 @@
1
+ export default function snap(value, step) {
2
+ if (step === 0) {
3
+ throw new Error("step cannot be zero.");
4
+ }
5
+
6
+ return Math.round(value / step) * step;
7
+ }
@@ -0,0 +1,3 @@
1
+ export default function truncate(n) {
2
+ return Math.trunc(n);
3
+ }
@@ -0,0 +1,9 @@
1
+ export default function wrap(value, min, max) {
2
+ if (min >= max) {
3
+ throw new Error("min must be less than max.");
4
+ }
5
+
6
+ const range = max - min;
7
+
8
+ return ((((value - min) % range) + range) % range) + min;
9
+ }