///<reference path=".d.ts"/>
"use strict";
var uuid = require("node-uuid");
var Fiber = require("fibers");
var Table = require("cli-table");
function createGUID(useBraces) {
if (useBraces === void 0) { useBraces = true; }
var output;
if (useBraces) {
output = "{" + uuid.v4() + "}";
}
else {
output = uuid.v4();
}
return output;
}
exports.createGUID = createGUID;
function stringReplaceAll(string, find, replace) {
return string.split(find).join(replace);
}
exports.stringReplaceAll = stringReplaceAll;
function isRequestSuccessful(request) {
return request.statusCode >= 200 && request.statusCode < 300;
}
exports.isRequestSuccessful = isRequestSuccessful;
function isResponseRedirect(response) {
return _.contains([301, 302, 303, 307, 308], response.statusCode);
}
exports.isResponseRedirect = isResponseRedirect;
function formatListOfNames(names, conjunction) {
Eif (conjunction === void 0) { conjunction = "or"; }
Iif (names.length <= 1) {
return names[0];
}
else {
return _.initial(names).join(", ") + " " + conjunction + " " + names[names.length - 1];
}
}
exports.formatListOfNames = formatListOfNames;
function getRelativeToRootPath(rootPath, filePath) {
var relativeToRootPath = filePath.substr(rootPath.length);
return relativeToRootPath;
}
exports.getRelativeToRootPath = getRelativeToRootPath;
function versionCompare(version1, version2) {
version1 = version1.split("-")[0];
version2 = version2.split("-")[0];
var v1array = _.map(version1.split("."), function (x) { return parseInt(x, 10); }), v2array = _.map(version2.split("."), function (x) { return parseInt(x, 10); });
if (v1array.length !== v2array.length) {
throw new Error("Version strings are not in the same format");
}
for (var i = 0; i < v1array.length; ++i) {
if (v1array[i] !== v2array[i]) {
return v1array[i] > v2array[i] ? 1 : -1;
}
}
return 0;
}
exports.versionCompare = versionCompare;
function isInteractive() {
return process.stdout.isTTY && process.stdin.isTTY;
}
exports.isInteractive = isInteractive;
function toBoolean(str) {
return str === "true";
}
exports.toBoolean = toBoolean;
function block(operation) {
if (isInteractive()) {
process.stdin.setRawMode(false);
}
operation();
if (isInteractive()) {
process.stdin.setRawMode(true);
}
}
exports.block = block;
function isNumber(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
exports.isNumber = isNumber;
function fromWindowsRelativePathToUnix(windowsRelativePath) {
return windowsRelativePath.replace(/\\/g, "/");
}
exports.fromWindowsRelativePathToUnix = fromWindowsRelativePathToUnix;
function isNullOrWhitespace(input) {
Iif (!input) {
return true;
}
return input.replace(/\s/gi, "").length < 1;
}
exports.isNullOrWhitespace = isNullOrWhitespace;
function getCurrentEpochTime() {
var dateTime = new Date();
return dateTime.getTime();
}
exports.getCurrentEpochTime = getCurrentEpochTime;
function sleep(ms) {
var fiber = Fiber.current;
setTimeout(function () { return fiber.run(); }, ms);
Fiber.yield();
}
exports.sleep = sleep;
function createTable(headers, data) {
var table = new Table({
head: headers,
chars: { "mid": "", "left-mid": "", "mid-mid": "", "right-mid": "" }
});
_.forEach(data, function (row) { return table.push(row); });
return table;
}
exports.createTable = createTable;
function remove(array, predicate, numberOfElements) {
numberOfElements = numberOfElements || 1;
var index = _.findIndex(array, predicate);
if (index === -1) {
return new Array();
}
return array.splice(index, numberOfElements);
}
exports.remove = remove;
function trimSymbol(str, symbol) {
while (str.charAt(0) === symbol) {
str = str.substr(1);
}
while (str.charAt(str.length - 1) === symbol) {
str = str.substr(0, str.length - 1);
}
return str;
}
exports.trimSymbol = trimSymbol;
function appendZeroesToVersion(version, requiredVersionLength) {
var zeroesToAppend = requiredVersionLength - version.split(".").length;
for (var index = 0; index < zeroesToAppend; index++) {
version += ".0";
}
return version;
}
exports.appendZeroesToVersion = appendZeroesToVersion;
|