@ts-common/azure-js-dev-tools
Version:
Developer dependencies for TypeScript related projects
97 lines • 3.55 kB
JavaScript
;
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceAll = exports.main = exports.readEntireString = exports.getLines = exports.padLeft = exports.contains = exports.endsWith = exports.startsWith = exports.arrayContains = void 0;
function arrayContains(array, el) {
return array.indexOf(el) !== -1;
}
exports.arrayContains = arrayContains;
function startsWith(value, prefix) {
return !!(value && prefix && value.indexOf(prefix) === 0);
}
exports.startsWith = startsWith;
function endsWith(value, suffix) {
return !!(value && suffix && value.length >= suffix.length && value.lastIndexOf(suffix) === value.length - suffix.length);
}
exports.endsWith = endsWith;
function contains(values, searchString) {
return arrayContains(values, searchString);
}
exports.contains = contains;
/**
* Pad the left side of the provided value to the targetLength using the provided padding.
* @param value The value to pad.
* @param targetLength The length to pad to.
* @param padding The string to use to pad the value. Defaults to " ".
* @returns The padded value.
*/
function padLeft(value, targetLength, padding) {
if (!padding) {
padding = " ";
}
if (typeof value === "number") {
value = value.toString();
}
if (!value) {
value = "";
}
while (value.length < targetLength) {
value = padding + value;
}
return value;
}
exports.padLeft = padLeft;
/**
* Get the lines of the provided text.
* @param text The text to get the lines of.
*/
function getLines(text) {
return text == undefined ? [] : text.split(/\r?\n/);
}
exports.getLines = getLines;
/**
* Capture and return the text from the provided readableStream.
* @param readableStream The stream to read from.
*/
function readEntireString(readableStream) {
return new Promise(function (resolve, reject) {
if (!readableStream) {
resolve(undefined);
}
else {
var result_1 = "";
readableStream.on("data", function (data) { return result_1 += data.toString(); });
readableStream.on("error", function (error) { return reject(error); });
var closed_1 = false;
var onClose = function () {
if (!closed_1) {
closed_1 = true;
resolve(result_1);
}
};
readableStream.on("close", onClose);
readableStream.on("end", onClose);
}
});
}
exports.readEntireString = readEntireString;
function main(mainFunction) {
return Promise.resolve(typeof mainFunction === "function" ? mainFunction() : mainFunction);
}
exports.main = main;
/**
* Replace all of the instances of searchValue in value with the provided replaceValue.
* @param {string | undefined} value The value to search and replace in.
* @param {string} searchValue The value to search for in the value argument.
* @param {string} replaceValue The value to replace searchValue with in the value argument.
* @returns {string | undefined} The value where each instance of searchValue was replaced with replacedValue.
*/
function replaceAll(value, searchValue, replaceValue) {
return !value || !searchValue ? value : value.split(searchValue).join(replaceValue || "");
}
exports.replaceAll = replaceAll;
//# sourceMappingURL=common.js.map