math-utils-simple 1.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/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # math-utils-simple
2
+
3
+ A simple math utility library for JavaScript.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm install math-utils-simple
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "math-utils-simple",
3
+ "version": "1.0.0",
4
+ "description": "A simple math utility library for npm",
5
+ "type": "module",
6
+ "main": "./src/index.js",
7
+ "exports": {
8
+ ".": "./src/index.js"
9
+ },
10
+ "scripts": {
11
+ "test": "node --test"
12
+ },
13
+ "keywords": ["math", "utils", "npm", "library"],
14
+ "author": "Umar Farooq",
15
+ "license": "MIT"
16
+ }
package/src/add.js ADDED
@@ -0,0 +1,3 @@
1
+ export default function add(a, b) {
2
+ return a + b;
3
+ }
package/src/average.js ADDED
@@ -0,0 +1,11 @@
1
+ export default function average(numbers) {
2
+ if (!Array.isArray(numbers)) {
3
+ throw new Error("Input must be an array");
4
+ }
5
+ if (numbers.length === 0) {
6
+ throw new Error("Cannot calculate average of an empty array");
7
+ }
8
+
9
+ const sum = numbers.reduce((acc, num) => acc + num, 0);
10
+ return sum / numbers.length;
11
+ }
package/src/divide.js ADDED
@@ -0,0 +1,6 @@
1
+ export default function divide(a, b) {
2
+ if (b === 0) {
3
+ throw new Error("Cannot divide by zero");
4
+ }
5
+ return a / b;
6
+ }
@@ -0,0 +1,14 @@
1
+ export default function factorial(n) {
2
+ if (n < 0) {
3
+ throw new Error("Factorial is not defined for negative numbers");
4
+ }
5
+ if (!Number.isInteger(n)) {
6
+ throw new Error("Factorial is only defined for integers");
7
+ }
8
+
9
+ let result = 1;
10
+ for (let i = 2; i <= n; i++) {
11
+ result *= i;
12
+ }
13
+ return result;
14
+ }
package/src/gcd.js ADDED
@@ -0,0 +1,12 @@
1
+ export default function gcd(a, b) {
2
+ a = Math.abs(a);
3
+ b = Math.abs(b);
4
+
5
+ while (b !== 0) {
6
+ const temp = b;
7
+ b = a % b;
8
+ a = temp;
9
+ }
10
+
11
+ return a;
12
+ }
package/src/index.js ADDED
@@ -0,0 +1,10 @@
1
+ export { default as add } from "./add.js";
2
+ export { default as subtract } from "./subtract.js";
3
+ export { default as multiply } from "./multiply.js";
4
+ export { default as divide } from "./divide.js";
5
+ export { default as power } from "./power.js";
6
+ export { default as sqrt } from "./sqrt.js";
7
+ export { default as factorial } from "./factorial.js";
8
+ export { default as gcd } from "./gcd.js";
9
+ export { default as lcm } from "./lcm.js";
10
+ export { default as average } from "./average.js";
package/src/lcm.js ADDED
@@ -0,0 +1,8 @@
1
+ import gcd from "./gcd.js";
2
+
3
+ export default function lcm(a, b) {
4
+ if (a === 0 || b === 0) {
5
+ return 0;
6
+ }
7
+ return Math.abs(a * b) / gcd(a, b);
8
+ }
@@ -0,0 +1,3 @@
1
+ export default function multiply(a, b) {
2
+ return a * b;
3
+ }
package/src/power.js ADDED
@@ -0,0 +1,3 @@
1
+ export default function power(base, exponent) {
2
+ return base ** exponent;
3
+ }
package/src/sqrt.js ADDED
@@ -0,0 +1,6 @@
1
+ export default function sqrt(n) {
2
+ if (n < 0) {
3
+ throw new Error("Cannot calculate square root of a negative number");
4
+ }
5
+ return Math.sqrt(n);
6
+ }
@@ -0,0 +1,3 @@
1
+ export default function subtract(a, b) {
2
+ return a - b;
3
+ }