-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.js
43 lines (37 loc) · 836 Bytes
/
functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"use strict";
const chars = "1234567890 abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
function dev(avg, array) {
return Math.sqrt(array.map(t => (t - avg) ** 2).reduce((a, t) => a + t, 0) / array.length);
}
function randomChar() {
return chars[Math.floor(Math.random() * chars.length)];
}
function r(obj) {
const keys = Object.keys(obj);
for(const key of keys) {
switch(typeof obj[key]) {
case "object":
r(obj[key]);
break;
case "number":
obj[key] = Math.random();
break;
case "string":
obj[key] = new Array(Math.round(obj[key].length * (Math.random() * 3))).fill().map(randomChar).join("");
break;
}
}
}
function randomize(data) {
try {
const o = JSON.parse(data);
r(o);
return JSON.stringify(o);
} catch(e) {
return data;
}
}
module.exports = {
dev,
randomize
};