Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
78 lines (61 loc) · 1.66 KB
/
Copy pathutils.js
File metadata and controls
78 lines (61 loc) · 1.66 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const money = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
const gbp = new Intl.NumberFormat("en-GB", {
style: "currency",
currency: "GBP",
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
const number = new Intl.NumberFormat("en-US");
const toNumber = (value) =>
typeof value === "string"
? Number(value.replace(/[^0-9.-]+/g, ""))
: Number(value);
const colorize = (value, formatted) => {
if (value > 0) return `\x1b[32m${formatted}\x1b[0m`;
if (value < 0) return `\x1b[31m${formatted}\x1b[0m`;
return formatted;
};
const sparkline = (data) => {
if (data.length < 2) return "";
const MAX_LEN = 20;
const points = data.slice(-MAX_LEN);
const min = Math.min(...points);
const max = Math.max(...points);
const range = max - min || 1;
const lines = ["⎽", "⎼", "─", "⎻", "⎺"];
let line = "";
for (let i = 0; i < points.length; i++) {
const n = points[i];
const idx = Math.min(
lines.length - 1,
Math.floor(((n - min) / range) * lines.length)
);
const char = lines[idx];
if (i === 0) {
line += char;
continue;
}
const delta = n - points[i - 1];
if (delta > 0) line += `\x1b[32m${char}\x1b[0m`;
else if (delta < 0) line += `\x1b[31m${char}\x1b[0m`;
else line += char;
}
const last = points[points.length - 1];
const prev = points[points.length - 2];
if (last > prev) return `${line} \x1b[32m▲\x1b[0m`;
if (last < prev) return `${line} \x1b[31m▼\x1b[0m`;
return `${line} ▶`;
};
module.exports = {
money,
gbp,
number,
toNumber,
colorize,
sparkline
};