@ne1410s/text
Version:
Lightweight ES5 script to provide text utilities
107 lines (104 loc) • 3.05 kB
JavaScript
;
var ESCAPE_RGX = /[-\/\\^$*+?.()|[\]{}]/g;
/**
* Removes leading character(s) from a body of text.
* @param text The body text.
* @param trim The leading character(s) to trim.
* @returns The trimmed text.
*/
function trimStart(text, trim) {
return text.replace(new RegExp("^".concat(trim.replace(ESCAPE_RGX, '\\$&'), "+")), '');
}
/**
* Removes trailing character(s) from a body of text.
* @param text The body text.
* @param trim The trailing character(s) to trim.
* @returns The trimmed text.
*/
function trimEnd(text, trim) {
return text.replace(new RegExp("".concat(trim.replace(ESCAPE_RGX, '\\$&'), "+$")), '');
}
/**
* Removes leading and trailing character(s) from a body of text.
* @param text The body text.
* @param trim The leading and trailing character(s) to trim.
* @returns The trimmed text.
*/
function trimBoth(text, trim) {
return trimEnd(trimStart(text, trim), trim);
}
/**
* Converts text to an array buffer.
* @param text The text.
* @returns An array buffer.
*/
function textToBuffer(text) {
return Uint8Array.from(Array.from(text).map(function (letter) { return letter.charCodeAt(0); })).buffer;
}
/**
* Converts an array buffer to the corresponding text.
* @param buffer An array buffer.
* @returns The corresponding text.
*/
function bufferToText(buffer) {
return String.fromCharCode.apply(null, new Uint8Array(buffer));
}
/**
* Encodes text as base 64.
* @param text The text.
* @returns The encoded base 64.
*/
function textToBase64(text) {
return Buffer.from(text, 'binary').toString('base64');
}
/**
* Decodes base 64 to text.
* @param base64 The base 64.
* @returns The decoded text.
*/
function base64ToText(base64) {
return Buffer.from(base64, 'base64').toString('binary');
}
/**
* Encodes an array buffer as base 64.
* @param buffer An array buffer.
* @returns The encoded base 64.
*/
function bufferToBase64(buffer) {
return textToBase64(bufferToText(buffer));
}
/**
* Encodes an array buffer as base 64 url.
* @param buffer An array buffer.
* @returns The encoded base 64 url.
*/
function bufferToBase64Url(buffer) {
return textToBase64Url(bufferToText(buffer));
}
/**
* Encodes text as base 64 url.
* @param text The text.
* @returns The encoded base 64 url.
*/
function textToBase64Url(text) {
return textToBase64(text).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
}
/**
* Encodes an object as base 64 url, from its JSON.
* @param object The object.
* @returns The encoded base 64 url.
*/
function objectToBase64Url(object) {
return textToBase64Url(JSON.stringify(object));
}
exports.base64ToText = base64ToText;
exports.bufferToBase64 = bufferToBase64;
exports.bufferToBase64Url = bufferToBase64Url;
exports.bufferToText = bufferToText;
exports.objectToBase64Url = objectToBase64Url;
exports.textToBase64 = textToBase64;
exports.textToBase64Url = textToBase64Url;
exports.textToBuffer = textToBuffer;
exports.trimBoth = trimBoth;
exports.trimEnd = trimEnd;
exports.trimStart = trimStart;