UNPKG

appcenter-cli

Version:

Command line tool for Visual Studio App Center

114 lines (113 loc) 2.84 kB
"use strict"; // // Utility functions used to encode and decode values // stored in the token cache as keys or values. // Object.defineProperty(exports, "__esModule", { value: true }); exports.decodeObject = exports.encodeObject = exports.unescape = exports.escape = void 0; const _ = require("lodash"); // // Replace ':' chars with '\:' and // replace '\' chars with '\\' // function escape(s) { let result = ""; _.each(s, function (ch) { switch (ch) { case ":": result += "\\:"; break; case "\\": result += "\\\\"; break; default: result += ch; } }); return result; } exports.escape = escape; // // Reverse of escape - converts \: and \\ back // to their single character equivalents. // function unescape(s) { let result = ""; let afterSlash = false; _.each(s, function (ch) { if (!afterSlash) { if (ch === "\\") { afterSlash = true; } else { result += ch; } } else { result += ch; afterSlash = false; } }); if (afterSlash) { result += "\\"; } return result; } exports.unescape = unescape; function encodeObject(obj) { return _.chain(obj) .toPairs() .sortBy(function (p) { return p[0]; }) .map(function (p) { if (_.isBoolean(p[1])) { return [p[0], p[1].toString()]; } if (_.isDate(p[1])) { return [p[0], p[1].toISOString()]; } return [p[0], p[1] ? p[1].toString() : ""]; }) .map(function (p) { return p.map(escape); }) .map(function (p) { return p.join(":"); }) .value() .join("::"); } exports.encodeObject = encodeObject; function endsWith(s, ending) { return s.substring(s.length - ending.length) === ending; } function partToKeyValue(part) { const parts = part.split(":"); const value = parts.reduce((accumulator, value, index, array) => { if (accumulator[1] !== null && endsWith(accumulator[1], "\\")) { accumulator[1] += ":" + value; } else if (accumulator[0] === null) { accumulator[0] = value; } else if (endsWith(accumulator[0], "\\")) { accumulator[0] += ":" + value; } else { accumulator[1] = value; } return accumulator; }, [null, null]); return value; } function decodeObject(key) { return _.chain(key.split("::")) .map(partToKeyValue) .map(function (pairs) { return pairs.map(unescape); }) .fromPairs() .value(); } exports.decodeObject = decodeObject;