UNPKG

onix-core

Version:
130 lines 5.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sprintf = void 0; const tslib_1 = require("tslib"); const toSafeInteger_1 = tslib_1.__importDefault(require("lodash/toSafeInteger")); const padStart_1 = tslib_1.__importDefault(require("lodash/padStart")); const padEnd_1 = tslib_1.__importDefault(require("lodash/padEnd")); const Justify_1 = require("./Justify"); // formatBaseX() const formatBaseX = (value, base, prefixBaseX, leftJustify, minWidth, precision, zeroPad) => { // Note: casts negative numbers to positive ones const number = value >>> 0; const prefix = prefixBaseX && number && { '2': '0b', '8': '0', '10': '', '16': '0x' }[base] || ''; const result = prefix + padStart_1.default(number.toString(base), precision || 0, '0'); return Justify_1.justify(result, prefix, leftJustify, minWidth, !!zeroPad); }; // formatString() const formatString = (value, leftJustify, minWidth, precision, zeroPad) => { if (precision) { value = value.slice(0, precision); } return Justify_1.justify(value, '', leftJustify, minWidth, !!zeroPad); }; const sprintf = (format, ...a) => { const regex = /%%|%(\d+\$)?([-+#0 ]*)(\*\d+\$|\*|\d+)?(\.(\*\d+\$|\*|\d+))?([scboxXuidfegEG])/g; let i = 0; // finalFormat() var doFormat = function (substring, valueIndex, flags, minWidth, _, precision, type) { if (substring == '%%') return '%'; // parse flags var leftJustify = false, positivePrefix = '', zeroPad = false, prefixBaseX = false; for (var j = 0; flags && j < flags.length; j++) switch (flags.charAt(j)) { case ' ': positivePrefix = ' '; break; case '+': positivePrefix = '+'; break; case '-': leftJustify = true; break; case '0': zeroPad = true; break; case '#': prefixBaseX = true; break; } // parameters may be null, undefined, empty-string or real valued // we want to ignore null, undefined and empty-string values if (!minWidth) { minWidth = 0; } else if (typeof minWidth === "string") { if (minWidth === '*') { minWidth = +a[i++]; } else if (minWidth.charAt(0) == '*') { minWidth = +a[toSafeInteger_1.default(minWidth.slice(1, -1))]; } } else { minWidth = +minWidth; } // Note: undocumented perl feature: if (minWidth < 0) { minWidth = -minWidth; leftJustify = true; } minWidth = toSafeInteger_1.default(minWidth); if (!isFinite(minWidth)) { throw new Error('sprintf: (minimum-)width must be finite'); } if (!precision) { precision = 'fFeE'.indexOf(type) > -1 ? 6 : ((type == 'd') ? 0 : undefined); } else if (typeof precision === "string") { if (precision == '*') { precision = +a[i++]; } else if (precision.charAt(0) == '*') { precision = +a[toSafeInteger_1.default(precision.slice(1, -1))]; } precision = toSafeInteger_1.default(precision); } else { precision = toSafeInteger_1.default(+precision); } // grab value using valueIndex if required? var value = valueIndex ? a[toSafeInteger_1.default(valueIndex.toString().slice(0, -1)) - 1] : a[i++]; switch (type) { case 's': return formatString(String(value), leftJustify, minWidth, precision, zeroPad); case 'c': return formatString(String.fromCharCode(+value), leftJustify, minWidth, precision, zeroPad); case 'b': return formatBaseX(value, 2, prefixBaseX, leftJustify, minWidth, precision, zeroPad); case 'o': return formatBaseX(value, 8, prefixBaseX, leftJustify, minWidth, precision, zeroPad); case 'x': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad); case 'X': return formatBaseX(value, 16, prefixBaseX, leftJustify, minWidth, precision, zeroPad).toUpperCase(); case 'u': return formatBaseX(value, 10, prefixBaseX, leftJustify, minWidth, precision, zeroPad); case 'i': case 'd': { var number = toSafeInteger_1.default(+value); var prefix = number < 0 ? '-' : positivePrefix; value = prefix + padEnd_1.default(String(Math.abs(number)), precision, '0'); return Justify_1.justify(value, prefix, leftJustify, minWidth, zeroPad); } case 'e': case 'E': case 'f': case 'F': case 'g': case 'G': { const number = +value; const prefix = number < 0 ? '-' : positivePrefix; const method = ['toExponential', 'toFixed', 'toPrecision']['efg'.indexOf(type.toLowerCase())]; const textTransform = ['toString', 'toUpperCase']['eEfFgG'.indexOf(type) % 2]; const abs = Math.abs(number); value = prefix + abs[method](precision); const just = Justify_1.justify(value, prefix, leftJustify, minWidth, zeroPad); return just[textTransform](); } default: return substring; } }; return format.replace(regex, doFormat); }; exports.sprintf = sprintf; //# sourceMappingURL=Sprintf.js.map