Backup_Senica/RVO15/flow/helper/utils.js
2025-10-16 02:24:14 +02:00

161 lines
3.9 KiB
JavaScript
Executable file

const fs = require('fs').promises;
function bytesToInt(bytes, numberOfBytes) {
let buffer = [];
if (Array.isArray(bytes)) {
buffer = bytes.slice(0);
if (numberOfBytes != undefined) {
buffer = bytes.slice(bytes.length - numberOfBytes);
}
}
else buffer.push(bytes);
let result = 0;
for (let i = 0; i < buffer.length; i++) {
result = (result << 8) | buffer[i];
}
return result >>> 0; //ensure it's an unsigned 32-bit number
}
function resizeArray(arr, newSize, defaultValue) {
while (newSize > arr.length)
arr.push(defaultValue);
arr.length = newSize;
}
longToByteArray = function(/*long*/long) {
// we want to represent the input as a 8-bytes array
var byteArray = [0, 0, 0, 0, 0, 0, 0, 0];
for (var index = 0; index < byteArray.length; index++) {
var byte = long & 0xff;
byteArray[index] = byte;
long = (long - byte) / 256;
}
return byteArray;
};
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
/*
sleep(2000).then(() => {
// Do something after the sleep!
});
*/
function sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
function isEmptyObject(obj) {
for (var name in obj) {
return false;
}
return true;
}
function emptyJsObject(jsObject) {
Object.keys(jsObject).forEach(key => delete jsObject[key]);
}
function convertUTCDateToLocalDate(date) {
var newDate = new Date(date);
newDate.setMinutes(date.getMinutes() + date.getTimezoneOffset());
return newDate;
}
function addZeroBefore(n) {
return (n < 10 ? '0' : '') + n;
}
/**
* Asynchronously writes data to a file.
*
* @param {string} filePath The path to the file.
* @param {string} data The data to write to the file.
* @param {boolean} [append=false] If true, appends the data to the file. If false, it overwrites the file.
*/
async function writeToFile(filePath, data, append = false) {
if (typeof data !== 'string') data = JSON.stringify(data, null, 2);
try {
if (append) {
// Append the data to the end of the file. This is the simplest way to append.
await fs.appendFile(filePath, data, 'utf8');
console.log(`Successfully appended data to ${filePath} using fs.appendFile.`);
} else {
// Overwrite the file with the new data.
await fs.writeFile(filePath, data, 'utf8');
console.log(`Successfully wrote (overwrote) data to ${filePath} using fs.writeFile.`);
}
} catch (error) {
console.error(`Error writing to file ${filePath}:`, error);
}
}
/**
* Checks if an item is present in an array and adds it if it's not.
* * @param {Array} arr The array to check.
* @param {*} item The item to add.
* @returns {Array} The modified array.
*/
const addToArrayIfUnique = (arr, item) => {
if (!arr.includes(item)) {
arr.push(item);
}
return arr;
};
var convertBase = function() {
function convertBase(baseFrom, baseTo) {
return function(num) {
return parseInt(num, baseFrom).toString(baseTo);
};
}
// binary to decimal
convertBase.bin2dec = convertBase(2, 10);
// binary to hexadecimal
convertBase.bin2hex = convertBase(2, 16);
// decimal to binary
convertBase.dec2bin = convertBase(10, 2);
// decimal to hexadecimal
convertBase.dec2hex = convertBase(10, 16);
// hexadecimal to binary
convertBase.hex2bin = convertBase(16, 2);
// hexadecimal to decimal
convertBase.hex2dec = convertBase(16, 10);
return convertBase;
}();
module.exports = {
bytesToInt,
longToByteArray,
addDays,
addZeroBefore,
resizeArray,
isEmptyObject,
emptyJsObject,
sleep,
convertUTCDateToLocalDate,
writeToFile,
addToArrayIfUnique
}