forked from nklerk/nl.nielsdeklerk.neeo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.js
94 lines (81 loc) · 2.77 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
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
'use strict'
const Homey = require('homey');
module.exports.isArray = function(a) {
return (!!a) && (a.constructor === Array);
};
module.exports.isObject = function(a) {
return (!!a) && (a.constructor === Object);
};
module.exports.mathRound = function (value, decimals) {
return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
}
module.exports.getLocalIp = function() {
const interfaces = require('os').networkInterfaces();
for (const iA in interfaces) {
const iface = interfaces[iA];
for (const alias of iface) {
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
return alias.address;
}
}
return '0.0.0.0';
}
/* module.exports.httpGetAndForget = function (method, host, port, path, content){
console.log ("/////////////////////////////////////////////");
console.log ("// module.exports.httpGetAndForget //////////");
console.log ("/////////////////////////////////////////////");
httpRequest({hostname: host, port: port, path: path, method: method, headers: {'Content-Type': 'application/json'}}, content);
} */
/* function httpRequest(options, content, callback){
console.log ("///////////////////////////////////////////////////////////////");
console.log ("// function httpRequest(options, content, callback){ //////////");
console.log ("///////////////////////////////////////////////////////////////");
let responseData = '';
const http = require('http');
const req = http.request(options, function(response) {
response.setEncoding('utf8');
response.on('data', (body) => {
responseData = responseData + body;
});
response.on('end', () => {
if (callback) {
callback(response, responseData);
}
});
});
req.on('error', (e) => {
console.log ('problem with request: ' + e.message);
});
if (content) {
req.write(JSON.stringify(content));
}
req.end();
req.on('end', () => {
req = undefined;
});
}
module.exports.httpRequest = httpRequest; */
module.exports.stringCleanForMatch = function (textstring){
textstring = textstring.toLowerCase();
textstring = textstring.replace(/(\s|\t| |,|\(|\))/gm,"");
return(textstring);
}
module.exports.stringNormalizeName = function (textstring){
textstring = textstring.toString();
const t1 = textstring.substring(0,1).toUpperCase();
const t2 = textstring.substring(1,textstring.length).toLowerCase();
return t1 + t2;
}
module.exports.stringToBoolean = function (textstring){
if (textstring === "true") {textstring = true};
if (textstring === "false") {textstring = false};
return textstring;
}
module.exports.percentage = function (value, range) {
if (value >= 0 && value <=1) {
return ((range[1]-range[0]) * value) + range[0];
} else {
console.log ('[TOOLS]\tERROR CONVERTING % to Value expected number 0 to 1 but got: '+value);
return 0;
}
}