-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.js
62 lines (57 loc) · 1.09 KB
/
Utils.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
/** Constants */
const DEFAULT_OPTION = {
host: 'openapi.band.us',
port: '443',
method: 'GET',
};
/** Load libraries */
const https = require('https');
/**
* @param options
* @param callback
*/
function httpGet(options, callback) {
//Request https GET/POST
https.get({
...DEFAULT_OPTION,
...options
}, response => {
let data = '';
response.on('data', chunk => data += chunk.toString());
response.on('end', () => {
let json = JSON.parse(data);
callback(json.result_code, json.result_data);
});
});
}
/**
* @param {string} haystack
* @param {string[]} needles
* @returns {boolean} str contains any needle
*/
function containsAny(haystack, needles) {
return needles.some(item => {
return haystack.indexOf(item) > -1;
});
}
/**
* @param {string} str
* @returns {number} hash created by string
*/
function createHash(str) {
let hash = 0, i, chr;
if (str.length === 0) {
return hash;
}
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0;
}
return hash;
}
module.exports = {
httpGet,
containsAny,
createHash
};