@sap-cloud-sdk/util
Version:
SAP Cloud SDK for JavaScript general utilities
67 lines • 2.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.encodeBase64 = encodeBase64;
exports.trimLeft = trimLeft;
exports.trimRight = trimRight;
exports.trim = trim;
exports.removeFileExtension = removeFileExtension;
const string_formatter_1 = require("./string-formatter");
/**
* Encode a string to a base64 encoded string.
* @param str - String to encode.
* @returns Base64 encoded string.
*/
function encodeBase64(str) {
return Buffer.from(str).toString('base64');
}
/**
* Remove whitespace from the left side of a string.
* @param string - String to trim.
* @returns String without whitespace on the left side.
*/
function trimLeft(string) {
const subStrings = string.split(string_formatter_1.unixEOL);
const leftTrimmed = subStrings[0].trimStart();
if (!leftTrimmed) {
subStrings.shift();
}
else {
subStrings[0] = leftTrimmed;
}
return subStrings.join(string_formatter_1.unixEOL);
}
/**
* Remove whitespace from the right side of a string.
* @param string - String to trim.
* @returns String without whitespace on the right side.
*/
function trimRight(string) {
const subStrings = string.split(string_formatter_1.unixEOL);
const rightTrimmed = subStrings[subStrings.length - 1].trimEnd();
if (!rightTrimmed) {
subStrings.pop();
}
else {
subStrings[subStrings.length - 1] = rightTrimmed;
}
return subStrings.join(string_formatter_1.unixEOL);
}
/**
* Remove whitespace from the left and right side of a string.
* @param string - String to trim.
* @returns String without outer whitespace.
*/
function trim(string) {
return trimRight(trimLeft(string));
}
/**
* Remove file extension from a string, e.g. remove 'test.jpg' would return 'test'.
* @param fileName - File name to remove the file extension from.
* @returns File name without extension.
*/
function removeFileExtension(fileName) {
return fileName.includes('.')
? fileName.split('.').slice(0, -1).join('.')
: fileName;
}
//# sourceMappingURL=string.js.map