forked from dilame/instagram-private-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
124 lines (103 loc) · 3.46 KB
/
helpers.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var Helpers = {};
var fs = require('fs');
var path = require("path");
var touch = require("touch");
var isStream = require("is-stream");
var validUrl = require('valid-url');
var _ = require("lodash");
var emailTester = /^[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+\/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-?\.?[a-zA-Z0-9])*\.[a-zA-Z](-?[a-zA-Z0-9])+$/;
Helpers.validateEmail = function(email) {
if(!email) return false;
if(email.length>254) return false;
var valid = emailTester.test(email);
if(!valid) return false;
var parts = email.split("@");
if(parts[0].length>64) return false;
var domainParts = parts[1].split(".");
if(domainParts.some(function(part) { return part.length>63; }))
return false;
return true;
}
Helpers.generateUUID = function () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
}).toLowerCase();
};
Helpers.getRandomArbitrary = function(min, max) {
return Math.random() * (max - min) + min;
}
Helpers.buildRankToken = function (accountId) {
return accountId + '_' + Helpers.generateUUID();
};
Helpers.isValidUrl = validUrl.isUri;
Helpers.ensureExistenceOfJSONFilePath = function(path) {
try {
touch.sync(path);
JSON.parse(fs.readFileSync(path));
} catch (e) {
fs.unlinkSync(path);
}
touch.sync(path);
}
Helpers.resolveDirectoryPath = function (directory) {
directory = path.resolve(directory);
if(!fs.statSync(directory).isDirectory())
throw new Error("Path `"+directory+"` is not directory!");
return directory;
}
Helpers.fileExists = function (path) {
try {
return fs.statSync(path).isFile()
} catch(e) {
return false;
}
}
Helpers.pathToStream = function (streamOrPath) {
var stream = _.isString(streamOrPath) ?
fs.createReadStream(path.resolve(streamOrPath)) :
streamOrPath;
if(!isStream(stream))
throw new Error("Argument is not posible to convert to stream!");
return stream;
}
Helpers.pathToBuffer = function(bufferOrPath){
return new Promise(function(resolve){
if(!_.isString(bufferOrPath)){
return callback(null,bufferOrPath)
}else{
fs.readFile(path.resolve(bufferOrPath),callback)
}
function callback(err,buffer){
if(err) throw err;
if(!Buffer.isBuffer(buffer))
throw new Error("Argument is not posible to convert to buffer!");
return resolve(buffer);
}
})
}
Helpers.isStream = function (stream) {
return isStream(stream);
}
Helpers.dataToRequestOption = function (data, filename) {
var raw, options = {};
if(_.isString(filename))
options.filename = filename;
if(data instanceof Buffer){
raw = data;
} else if(isStream(data)) {
raw = data;
} else if(_.isString(data)) {
raw = Helpers.pathToStream(data);
} else if(_.isObject(data)) {
raw = Helpers.dataToRequestOption(data.value).value;
options = _.defaults(options, _.omit(data, 'value'));
} else {
throw new Error("Invalid data passed as argument for request!")
}
return {value: raw, options: options}
}
Helpers.extractUrl = function (text) {
return text.match(/((?:https\:\/\/)|(?:http\:\/\/)|(?:www\.))?([a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(?:\??)[a-zA-Z0-9\-\._\?\,\'\/\\\+&%\$#\=~]+)/g);
}
module.exports = Helpers;