mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-02-27 00:04:37 +08:00
70 lines
1.5 KiB
JavaScript
70 lines
1.5 KiB
JavaScript
import uaparser from "ua-parser-js";
|
|
|
|
export function roundTo2(num) {
|
|
return Math.round((num + Number.EPSILON) * 100) / 100;
|
|
}
|
|
|
|
export function escapeRegExp(str) {
|
|
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
}
|
|
|
|
export function stdDev(array) {
|
|
const n = array.length;
|
|
const mean = array.reduce((a, b) => a + b) / n;
|
|
return Math.sqrt(
|
|
array.map((x) => Math.pow(x - mean, 2)).reduce((a, b) => a + b) / n
|
|
);
|
|
}
|
|
|
|
export function mean(array) {
|
|
try {
|
|
return (
|
|
array.reduce((previous, current) => (current += previous)) / array.length
|
|
);
|
|
} catch (e) {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
export function kogasa(cov) {
|
|
return (
|
|
100 * (1 - Math.tanh(cov + Math.pow(cov, 3) / 3 + Math.pow(cov, 5) / 5))
|
|
);
|
|
}
|
|
|
|
export function identity(value) {
|
|
return Object.prototype.toString
|
|
.call(value)
|
|
.replace(/^\[object\s+([a-z]+)\]$/i, "$1")
|
|
.toLowerCase();
|
|
}
|
|
|
|
export function base64UrlEncode(string) {
|
|
return Buffer.from(string).toString("base64url");
|
|
}
|
|
|
|
export function base64UrlDecode(string) {
|
|
return Buffer.from(string, "base64url").toString();
|
|
}
|
|
|
|
export function buildAgentLog(req) {
|
|
const agent = uaparser(req.headers["user-agent"]);
|
|
|
|
const agentLog = {
|
|
ip:
|
|
req.headers["cf-connecting-ip"] ||
|
|
req.headers["x-forwarded-for"] ||
|
|
req.ip ||
|
|
"255.255.255.255",
|
|
agent: `${agent.os.name} ${agent.os.version} ${agent.browser.name} ${agent.browser.version}`,
|
|
};
|
|
|
|
const {
|
|
device: { vendor, model, type },
|
|
} = agent;
|
|
if (vendor) {
|
|
agentLog.device = `${vendor} ${model} ${type}`;
|
|
}
|
|
|
|
return agentLog;
|
|
}
|