This repository has been archived by the owner on Jan 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
62 lines (60 loc) · 1.62 KB
/
index.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
'use strict'
let request = require('request')
module.exports = {
Chain: require('./dist/lib/Chain'),
Time: require('./dist/lib/Time'),
Disk: require('o.disk'),
i18n: require('o.i18n'),
toArray: require('nesne').toArray,
req(ep = '', data, method, headers = {}, opts) {
if(typeof this == 'string') {
ep = this.vars({ep})
}
method = method ? method.toLowerCase() : (data ? 'post' : 'get')
data = data ? {form: data} : {}
return new Promise((res, rej) => request[method]({url: ep, ...data, headers, ...opts}, (err, h, body) => {
try{
body = JSON.parse(body)
}catch(e) {
}
err ? rej(err) : res(body, h.headers)
}))
}
}
Object.assign(global.String.prototype, {
vars(vars) {
vars = typeof vars == 'object' ? vars : arguments
return Object.keys(vars).reduce((m, v) => {
return m.replace(new RegExp('(' + v + '[₺|$|₸|₼])+'), vars[v])
}, this)
},
replaceAll(f, r) {
let s = this
for(var i in f) {
while(s.indexOf(f[i]) > -1) {
s = s.replace(f[i], r[i])
}
}
return s
},
of(obj) {
return this == '*' ? obj : this.split(',').reduce((o, i) => { o[i] = obj[i] || null; return o }, {})
},
from(obj) {
let r = (this == '*' ? Object.keys(obj) : this.split(',')).map(i => obj[i])
return (this.indexOf(',') == -1 && this != '*') ? r[0] : r
},
obj(arr, def) {
return this.split(',').reduce(
def instanceof Array
? (n, i, j) => {
n[i] = arr[j] || def[j]
return n
}
: (n, i, j) => {
n[i] = arr[j] || def
return n
}
, {})
}
})