encoding-helper/index.js
2020-03-25 17:03:40 +01:00

53 lines
1.1 KiB
JavaScript

/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of Encoding-Helper which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, March 2020
*/
// @ts-nocheck
'use strict';
/**
* encode a string to base64
*
* @param {string} str string to encode
* @param {string} encoding encoding the string is in
* @returns {string} base64
*/
function to_b64 (str, encoding = 'utf-8') {
const buf = Buffer.from (str, encoding);
return buf.toString ('base64');
}
/**
* encode a string to utf-8
*
* @param {string} str string to encode
* @param {string} encoding encoding the string is in
* @returns {string} utf-8
*/
function to_utf8 (str, encoding) {
const buf = Buffer.from (str, encoding);
return buf.toString ('utf-8');
}
/**
* encode a string to hex
*
* @param {string} str string to encode
* @param {string} encoding encoding the string is in
* @returns {string} hex
*/
function to_hex (str, encoding = 'utf-8') {
const buf = Buffer.from (str, encoding);
return buf.toString ('hex');
}
module.exports = {
to_b64,
to_hex,
to_utf8
};