math-utils-simple 2.0.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": "2.0.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
@@ -54,4 +54,19 @@ export { default as roundToNearest } from "./rounding/roundToNearest.js";
54
54
  export { default as snap } from "./rounding/snap.js";
55
55
  export { default as isBetween } from "./rounding/isBetween.js";
56
56
  export { default as wrap } from "./rounding/wrap.js";
57
- export { default as normalize } from "./rounding/normalize.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
+ }