This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
logger4njs/lib/conversion.js
2022-05-02 19:31:47 +02:00

48 lines
No EOL
940 B
JavaScript

/** properHex
*
* @param {string} val original value
*
* @returns {{hex: Array<number>, str: string}}
*/
let properHex = (val) => {
let retval = {
hex: [],
str: ""
};
if (Buffer.isBuffer(val))
{
retval.str = val.toString('hex').toUpperCase().replace(/([0-9A-F]{2})/g, '$1 ');
retval.str = "[ " + retval.str.substring(0, retval.str.length - 1) + " ]";
retval.hex = val;
return retval;
}
if (typeof val === 'string')
{
let b = Buffer.from(val);
return properHex(b);
}
}
/** dictionaryConversion
*
* @param {string} origin
* @param {Map} dict
*
* @returns {string}
*
*/
let dictionaryConversion = (origin, dict) => {
let retval = "";
dict.forEach((v, k, m) => {
// if k is found in origin replace it with v
if (origin.indexOf(k) > -1) {
retval = origin.replace(k, v);
}
});
return retval;
}
module.exports = {
properHex,
dictionaryConversion
}