UNPKG

node-red-contrib-services-mentor

Version:

Mentor Services

192 lines (169 loc) 6.28 kB
/* Usage // example usage: checkInternet(function(isConnected) { if (isConnected) { // connected to the internet } else { // not connected to the internet } }); */ module.exports.checkInternet = (cb) => { require('dns').lookup('google.com',function(err) { if (err && err.code == "ENOTFOUND") { cb(false); } else { cb(true); } }) }; module.exports.encode = (texto) => { var indx1; var limite; var texto_codificado = ""; var code = Buffer.from(texto, 'utf-8'); texto_codificado = new Buffer.from(""); indx1 = 1; limite = 3; for (indx = 0; indx < code.length; indx += 1) { if (indx1 == limite) { indx1 = 1; limite += 1; } if (indx1 % 2 == 0) { tem_cod = code.readUInt8(indx) + 1 ; } else { tem_cod = code.readUInt8(indx) - 1 ; } buffer1 = Buffer.from(texto_codificado); buffer2 = Buffer.from([tem_cod]); if (indx % 2 == 0) { texto_codificado = Buffer.concat([buffer1,buffer2]); } else { texto_codificado = Buffer.concat([buffer2,buffer1]); } indx1 += 1; } code = texto_codificado; texto_codificado = Buffer.from(""); for (indx = 0; indx < code.length; indx += 1) { tem_cod = code.readUInt8(indx); buffer1 = Buffer.from(texto_codificado); buffer2 = Buffer.from([tem_cod]); if (indx % 2 == 0) { texto_codificado = Buffer.concat([buffer2,buffer1]); } else { texto_codificado = Buffer.concat([buffer1,buffer2]); } } code = texto_codificado; texto_codificado = Buffer.from(""); for (indx = 0; indx < code.length; indx += 1) { tem_cod = code.readUInt8(indx); buffer1 = Buffer.from(texto_codificado); buffer2 = Buffer.from([tem_cod]); if (indx % 2 == 0) { texto_codificado = Buffer.concat([buffer1,buffer2]); } else { texto_codificado = Buffer.concat([buffer2,buffer1]); } } delete buffer1; delete buffer2; delete code; return texto_codificado.toString(); }; // helper functions /** * @description This function returns the time as an object in three different formats * @returns {} time as object with parameters: * @returns {} obj.timeObject - Javascript Date Object * @returns String obj.year - year-month-date, hours:minutes:sec:ms * @returns String obj.time - hours:minutes:sec:ms */ module.exports.timestamp = () => { const myDate = new Date(); let month = myDate.getMonth() + 1; switch (month) { case 1: month = 'Jan'; break; case 2: month = 'Feb'; break; case 3: month = 'Mar'; break; case 4: month = 'Apr'; break; case 5: month = 'May'; break; case 6: month = 'Jun'; break; case 7: month = 'Jul'; break; case 8: month = 'Aug'; break; case 9: month = 'Sep'; break; case 10: month = 'Oct'; break; case 11: month = 'Nov'; break; case 12: month = 'Dec'; break; default: month = 'Jan'; break; } return { format_Object: myDate, format_year: myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-' + myDate.getDate() + ', ' + myDate.getHours() + ':' + myDate.getMinutes() + ':' + myDate.getSeconds() + ':' + myDate.getMilliseconds(), format_time: myDate.getHours() + ':' + myDate.getMinutes() + ':' + myDate.getSeconds() + ':' + myDate.getMilliseconds(), format_NodeRed: myDate.getDate() + ' ' + month + ' ' + myDate.getHours() + ':' + myDate.getMinutes() + ':' + myDate.getSeconds(), }; }; /** * @description * Checks if the given value is an numeric value or a string representing a numeric value * @param {anyVal} n - A value that has to be tested * @returns {bool} true in case of a numeric value else false * console.log(isNumeric(true)) //false * console.log(isNumeric(1)) //true * console.log(isNumeric(1.01)) //true * console.log(isNumeric(1e10)) //true * console.log(isNumeric('1')) //true * console.log(isNumeric('1.4')) //true * console.log(isNumeric('1e5')) //true * console.log(isNumeric('a')) //false * console.log(isNumeric('1a')) //false */ module.exports.isNumeric = (n) => { return !isNaN(parseFloat(n)) && isFinite(n); }; /** * @description * Checks if the given value is an float value * @param {anyVal} n - A value that has to be tested * @returns {bool} true in case of a float value else false * console.log(isFloat(1.01)) //true * console.log(isFloat(-1.01)) //true * console.log(isFloat(1)) //false * console.log(isFloat(1e10)) //false * console.log(isFloat('1.4')) //false * console.log(isFloat('-1.4'))//false * console.log(isFloat('1e5')) //false * console.log(isFloat('a')) //false * console.log(isFloat('1a')) //false * console.log(isFloat(true)) //false */ module.exports.isFloat = (value) => { if (typeof value === 'number') { return (value % 1 !== 0); } return false; }; /** * @description Creates an array with the length 'len' and fill it with 'val' * @param {Number} - Length of the return Array * @param {String|Number|Object|Array|bool|etc} - Value of each digit * @returns {Array} Array with the length 'len' and fill it with 'val' * @example * FilledArray(3,'0'); //returns ['0','0','0'] */ module.exports.FilledArray = (len, val) => { const array = []; for (let i = 0; i < len; i++) { array[i] = val; } return array; };