-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.js
executable file
·118 lines (100 loc) · 3.22 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
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
var C = require('cli-color');
module.exports = new Utils();
function Utils() { }
Utils.prototype.appName = function() {
return require('path').basename(process.argv[1]);
};
Utils.prototype.formatProfile = function(profile) {
var result = profile.username + '@' + profile.host;
var opts = [];
if (profile.identity) {
opts.push('using identity');
}
if (profile.password) {
opts.push('using password');
}
if (!profile.password && typeof profile.password !== 'undefined') {
opts.push('using interactive password');
}
if (opts.length) {
result = result + ' (' + opts.join(', ') + ')';
}
if (profile.default) {
result = result + ' ' + C.magentaBright('[default]');
}
return result;
};
Utils.prototype.formatTunnel = function(name, tunnel) {
return C.cyan(name) + ': ' + C.white('source port') + ' = ' + C.bold(tunnel.sourcePort) + ", " + C.white('host') + ' = ' + C.bold(tunnel.host) + ', ' + C.white('dest port') + ' = ' + C.bold(tunnel.destPort);
};
Utils.prototype.parseTunnelString = function(tunnelString) {
if (tunnelString && tunnelString.split(':').length) {
var arr = tunnelString.split(':');
return { sourcePort: tunnelString[0].trim(), host: tunnelString[1].trim(), destPort: tunnelString[2].trim() };
}
return {};
};
Utils.prototype.rejectValue = function(cliObj, message) {
var result = [];
result.push(C.red(message));
result.push(' ' + C.magentaBright.bold(cliObj._name) + ' : ' + cliObj._title);
return result.join('\n');
};
Utils.prototype.isNumber = function(val) {
return ! isNaN (val-0) && val !== null && val !== "" && val !== false;
};
Utils.prototype.findMatches = function(find, str) {
var matches = [];
var match;
while (match = find.exec(str)) {
matches.push(match[1]);
}
return matches;
};
Utils.prototype.unique = function(ary) {
var u = {}, a = [];
for(var i = 0, l = ary.length; i < l; ++i){
if(u.hasOwnProperty(ary[i])) {
continue;
}
a.push(ary[i]);
u[ary[i]] = 1;
}
return a;
};
Utils.prototype.replaceAll = function(find, replace, str) {
return str.replace(new RegExp(find, 'gi'), replace);
};
Utils.prototype.clone = function(obj) {
return clone(obj);
function clone(obj) {
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;
// Handle Date
if (obj instanceof Date) {
var copy = new Date();
copy.setTime(obj.getTime());
return copy;
}
// Handle Array
if (obj instanceof Array) {
var copy = [];
for (var i = 0, len = obj.length; i < len; i++) {
copy[i] = clone(obj[i]);
}
return copy;
}
// Handle Object
if (obj instanceof Object) {
var copy = {};
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
}
};
Utils.prototype.toJson = function(obj) {
return JSON.stringify(obj, null, 4);
};