-
Notifications
You must be signed in to change notification settings - Fork 4
/
tools.js
66 lines (52 loc) · 1.54 KB
/
tools.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
var md5 = require('md5');
exports.urlencode = async (params) => {
let result = [];
Object.keys(params).sort().map(key => {
params[key] = encodeURIComponent(params[key]);
params[key] = params[key].replace(/\%20/g, '+');
result.push(key + "=" + params[key]);
});
return await result.join('&');
};
exports.signature = async (headers, body) => {
var merge = {};
if (typeof(body) != "object") {
body = {};
};
Object.keys(body).map(key => {
merge[key] = body[key];
});
if (typeof(headers) != "object") {
headers = {};
};
Object.keys(headers).map(key => {
merge[key] = headers[key];
});
var signature = [];
Object.keys(merge).sort().map(key => {
merge[key] = encodeURIComponent(merge[key]);
merge[key] = merge[key].replace(/\%20/g, '+');
signature.push(key + "=" + merge[key]);
});
headers.signature = md5(signature.join('&'));
return {
'body': body,
'headers': headers
};
};
exports.csvtojson = async (csv) => {
csv = csv.split('"').join('');
let data = [];
let lines = await csv.split('"').join('').split('\n');
let headers = lines[0].toLowerCase().split(',').map(key => key.trim());
lines.splice(0, 1);
lines.map(line => {
var tmp = {};
line = line.split(',').map(key => key.trim());
for (let i = 0; i < headers.length; i++) {
tmp[headers[i]] = line[i];
};
data.push(tmp);
});
return await data;
};