forked from kaiko/zazler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolbox.js
More file actions
30 lines (22 loc) · 1.54 KB
/
toolbox.js
File metadata and controls
30 lines (22 loc) · 1.54 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
if (!Object.values) Object.values = o => Object.keys(o).map(k => o[k]);
Object.map = (o, fn) => { let i = 0, r = {}; for (let k in o) r[k] = fn(o[k], k, i++); return r; }
Object.mapDeep = (o, fn) => { let i = 0, r = {}; for (let k in o) r[k] = (typeof o[k] === 'object' && o[k] !== null) ? Object.mapDeep(o[k], fn) : fn(o[k], k, i++); return r; }
Object.mapA = async (o, fn) => { let i = 0, r = {}; for (let k in o) r[k] = await fn(o[k], k, i++); return r; }
Set.fromArray = a => a.reduce((s, e) => s.add(e), new Set());
Map.prototype.setWith = function (key, val, fn) {
if (this.has(key)) this.set(key, fn(this.get(key), key));
else this.set(key, val);
}
Map.fromArray = (ls, key, valFn) => ls.reduce((m, i) => m.set(i[key], valFn ? valFn(i) : i), new Map())
breakOn =
(str, on) => (pos => pos === -1 ? [str] : [str.substr(0,pos), str.substr(pos+1) ])( str.indexOf(on) )
udec = decodeURIComponent
module.exports = { breakOn,
trace: (v, mark) => { console.log(mark ? mark + '\n' + v + '\n/' + mark : v); return v; },
zipObject: (lsKeys, lsValues) => lsKeys.reduce((k,o,i) => Object.assign(o, {[k]: lsValues[i]}), {}),
getBody: req => new Promise(ok => { let b = ''; req.on('data', c => b += c); req.on('end', () => ok(b)) }),
btoa: s => new Buffer(s, 'base64').toString('binary'),
parseBool: s => ['false', 'no', '0'].includes(s.toString().toLowerCase()) ? false : !!s,
uriArgs: q => !q ? {} :
q.split('&').map(a => breakOn(a, '=')).map(([k,v]) => [udec(k), udec(v)]).reduce( (a, [k,v]) => Object.assign(a, {[k]: v}), {} )
}