-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
fxrc.js
84 lines (67 loc) · 1.87 KB
/
fxrc.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
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
79
80
81
82
83
84
globalThis.csv = (sep = ",") => x => {
if (!Array.isArray(x) || x.length < 1 || typeof x[0] !== "object") return;
const kx = Object.keys(x[0]);
return kx.join(sep) + "\n" + x
.map(obj => kx
.map(k => typeof obj[k] === "string" && obj[k].includes("\n") ? `"${obj[k]}"` : obj[k])
.join(sep))
.join("\n");
}
globalThis.mdtable = x => {
if (!Array.isArray(x) || x.length < 1 || typeof x[0] !== "object") return;
const kx = Object.keys(x[0]);
const EOL = "\n";
const START = "| ";
const SEP = " | ";
const END = " |" + EOL;
const header = START + kx.join(SEP) + END;
const line = header.replace(/[^| \n]/g, "-");
const rows = x
.map(obj => START + kx.map(k => obj[k] || "").join(SEP) + END)
.join("");
return header + line + rows;
}
Object.pick = function pick(obj, keys) {
if (typeof obj !== "object") {
return {};
}
var res = {};
if (typeof keys === 'string') {
if (keys in obj) {
res[keys] = obj[keys];
}
return res;
}
var len = keys.length;
var idx = -1;
while (++idx < len) {
var key = keys[idx];
if (key in obj) {
res[key] = obj[key];
}
}
return res;
};
Object.omit = function omit(obj, props, fn) {
if (typeof obj !== "object") {
return {};
}
if (typeof props === 'function') {
fn = props;
props = [];
}
if (typeof props === 'string') {
props = [props];
}
var isFunction = typeof fn === 'function';
var keys = Object.keys(obj);
var res = {};
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var val = obj[key];
if (!props || (props.indexOf(key) === -1 && (!isFunction || fn(val, key, obj)))) {
res[key] = val;
}
}
return res;
};