@splode/obake
Version:
Check merchants for deals.
44 lines • 1.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.prettyPercent = exports.parsePrice = exports.elide = void 0;
/**
* elide returns a new string trimmed from the beginning up to n characters. Finally, the string is terminated with an
* ellipsis. For example `Foobar...`
*
* @param str The string to trim
* @param max The number of characters to keep before trimming
* @returns The truncated string
*/
function elide(str, max) {
if (max === void 0) { max = 24; }
return str.substr(0, max) + "...";
}
exports.elide = elide;
/**
* parsePrice attempts to parse a price represented in string format into a float. Prices with and without leading `"$""`
* are supported.
*
* @param ps The price string
* @returns The price
*/
function parsePrice(ps) {
var priceString = ps;
if (ps[0] === "$") {
priceString = ps.substring(1);
}
return parseFloat(priceString);
}
exports.parsePrice = parsePrice;
/**
* prettyPercent returns a human-friendly string representation of a fraction. For example, `1/2` would return `"50%""`
*
* @param numerator The fraction's numerator
* @param denominator The fraction's denominator
* @returns A string representing the percentage
*/
function prettyPercent(numerator, denominator) {
var p = 100 - (numerator / denominator) * 100;
return p.toFixed(2) + "%";
}
exports.prettyPercent = prettyPercent;
//# sourceMappingURL=strings.js.map