data-utils-aravind 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/index.js +59 -0
- package/package.json +14 -0
- package/readme.md +19 -0
package/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
function sum(arr) {
|
|
2
|
+
return arr.reduce((a, b) => a + b, 0);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function mean(arr) {
|
|
6
|
+
return sum(arr) / arr.length;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
function median(arr) {
|
|
10
|
+
const sorted = [...arr].sort((a, b) => a - b);
|
|
11
|
+
const mid = Math.floor(sorted.length / 2);
|
|
12
|
+
|
|
13
|
+
return sorted.length % 2 !== 0
|
|
14
|
+
? sorted[mid]
|
|
15
|
+
: (sorted[mid - 1] + sorted[mid]) / 2;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function mode(arr) {
|
|
19
|
+
const freq = {};
|
|
20
|
+
let max = 0;
|
|
21
|
+
let result;
|
|
22
|
+
|
|
23
|
+
for (let num of arr) {
|
|
24
|
+
freq[num] = (freq[num] || 0) + 1;
|
|
25
|
+
if (freq[num] > max) {
|
|
26
|
+
max = freq[num];
|
|
27
|
+
result = num;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return result;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function variance(arr) {
|
|
34
|
+
const avg = mean(arr);
|
|
35
|
+
return mean(arr.map(x => (x - avg) ** 2));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function standardDeviation(arr) {
|
|
39
|
+
return Math.sqrt(variance(arr));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function min(arr) {
|
|
43
|
+
return Math.min(...arr);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function max(arr) {
|
|
47
|
+
return Math.max(...arr);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
module.exports = {
|
|
51
|
+
sum,
|
|
52
|
+
mean,
|
|
53
|
+
median,
|
|
54
|
+
mode,
|
|
55
|
+
variance,
|
|
56
|
+
standardDeviation,
|
|
57
|
+
min,
|
|
58
|
+
max
|
|
59
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "data-utils-aravind",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Simple data analysis utility functions",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"start": "node index.js",
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"keywords": ["data", "statistics", "mean", "median"],
|
|
11
|
+
"author": "Pon Aravind V J",
|
|
12
|
+
"license": "ISC",
|
|
13
|
+
"type": "commonjs"
|
|
14
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Simple Data Utils
|
|
2
|
+
|
|
3
|
+
A lightweight JavaScript library for basic statistical operations.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
- Sum
|
|
7
|
+
- Mean
|
|
8
|
+
- Median
|
|
9
|
+
- Mode
|
|
10
|
+
- Variance
|
|
11
|
+
- Standard Deviation
|
|
12
|
+
- Min / Max
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
const stats = require('data-utils-aravind');
|
|
18
|
+
|
|
19
|
+
console.log(stats.mean([1,2,3]));
|