math-utils-simple 1.2.0 → 2.1.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # math-utils-simple
2
2
 
3
- A simple math utility library for JavaScript.
3
+ A lightweight JavaScript math utility library built to scale for advanced use cases in the future.
4
4
 
5
5
  ## Install
6
6
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "math-utils-simple",
3
- "version": "1.2.0",
3
+ "version": "2.1.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,34 @@ 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";
58
+
59
+ // PERCENTAGES
60
+ export { default as percentage } from "./percentages/percentage.js";
61
+ export { default as increaseByPercentage } from "./percentages/increaseByPercentage.js";
62
+ export { default as decreaseByPercentage } from "./percentages/decreaseByPercentage.js";
63
+ export { default as percentageDifference } from "./percentages/percentageDifference.js";
64
+ export { default as percentageChange } from "./percentages/percentageChange.js";
65
+ export { default as findOriginalValue } from "./percentages/findOriginalValue.js";
66
+ export { default as findFinalValue } from "./percentages/findFinalValue.js";
67
+ export { default as discount } from "./percentages/discount.js";
68
+ export { default as markup } from "./percentages/markup.js";
69
+ export { default as profitPercentage } from "./percentages/profitPercentage.js";
70
+ export { default as lossPercentage } from "./percentages/lossPercentage.js";
71
+ export { default as margin } from "./percentages/margin.js";
72
+ export { default as relativeChange } from "./percentages/relativeChange.js";
@@ -0,0 +1,3 @@
1
+ export default function decreaseByPercentage(value, percent) {
2
+ return value * (1 - percent / 100);
3
+ }
@@ -0,0 +1,3 @@
1
+ export default function discount(price, percent) {
2
+ return price * (1 - percent / 100);
3
+ }
@@ -0,0 +1,3 @@
1
+ export default function findFinalValue(original, percent) {
2
+ return original * (1 + percent / 100);
3
+ }
@@ -0,0 +1,7 @@
1
+ export default function findOriginalValue(finalValue, percentIncrease) {
2
+ if (percentIncrease === -100) {
3
+ throw new Error("Percent increase cannot be -100.");
4
+ }
5
+
6
+ return finalValue / (1 + percentIncrease / 100);
7
+ }
@@ -0,0 +1,3 @@
1
+ export default function increaseByPercentage(value, percent) {
2
+ return value * (1 + percent / 100);
3
+ }
@@ -0,0 +1,11 @@
1
+ export default function lossPercentage(cost, sellingPrice) {
2
+ if (cost === 0) {
3
+ throw new Error("Cost cannot be zero.");
4
+ }
5
+
6
+ if (sellingPrice >= cost) {
7
+ return 0;
8
+ }
9
+
10
+ return ((cost - sellingPrice) / cost) * 100;
11
+ }
@@ -0,0 +1,7 @@
1
+ export default function margin(cost, sellingPrice) {
2
+ if (sellingPrice === 0) {
3
+ throw new Error("Selling price cannot be zero.");
4
+ }
5
+
6
+ return ((sellingPrice - cost) / sellingPrice) * 100;
7
+ }
@@ -0,0 +1,3 @@
1
+ export default function markup(cost, percent) {
2
+ return cost * (1 + percent / 100);
3
+ }
@@ -0,0 +1,7 @@
1
+ export default function percentage(value, total) {
2
+ if (total === 0) {
3
+ throw new Error("Total cannot be zero.");
4
+ }
5
+
6
+ return (value / total) * 100;
7
+ }
@@ -0,0 +1,7 @@
1
+ export default function percentageChange(oldValue, newValue) {
2
+ if (oldValue === 0) {
3
+ throw new Error("Old value cannot be zero.");
4
+ }
5
+
6
+ return ((newValue - oldValue) / oldValue) * 100;
7
+ }
@@ -0,0 +1,10 @@
1
+ export default function percentageDifference(a, b) {
2
+ if (a === 0 && b === 0) {
3
+ return 0;
4
+ }
5
+
6
+ return (
7
+ (Math.abs(a - b) / ((Math.abs(a) + Math.abs(b)) / 2)) *
8
+ 100
9
+ );
10
+ }
@@ -0,0 +1,7 @@
1
+ export default function profitPercentage(cost, sellingPrice) {
2
+ if (cost === 0) {
3
+ throw new Error("Cost cannot be zero.");
4
+ }
5
+
6
+ return ((sellingPrice - cost) / cost) * 100;
7
+ }
@@ -0,0 +1,7 @@
1
+ export default function relativeChange(a, b) {
2
+ if (b === 0) {
3
+ throw new Error("Reference value cannot be zero.");
4
+ }
5
+
6
+ return ((a - b) / b) * 100;
7
+ }
@@ -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
+ }