node-console-progress-bar-tqdm
Version:
Progress bar in console for Node.js in the style of TQDM Python library
134 lines • 4.23 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.scaleUnit = exports.handleUnit = exports.formatTimeDelta = exports.hasFd = exports.hasLength = exports.isIterator = exports.isAsyncIterable = exports.isIterable = exports.isObject = exports.hasOwnProperty = exports.pluralService = void 0;
const pluralKeys = ['zero', 'one', 'two', 'few', 'many', 'other'];
exports.pluralService = new Intl.PluralRules('en-US');
const _hasOwnProperty = Object.prototype.hasOwnProperty;
function hasOwnProperty(obj, key) {
return _hasOwnProperty.call(obj, key);
}
exports.hasOwnProperty = hasOwnProperty;
function isObject(x) {
return x !== null && typeof x == 'object';
}
exports.isObject = isObject;
function isIterable(x) {
if (!isObject(x)) {
return false;
}
return typeof x[Symbol.iterator] == 'function';
}
exports.isIterable = isIterable;
function isAsyncIterable(x) {
if (!isObject(x)) {
return false;
}
return typeof x[Symbol.asyncIterator] == 'function';
}
exports.isAsyncIterable = isAsyncIterable;
function isIterator(x) {
if (!isObject(x)) {
return false;
}
return typeof x.next == 'function';
}
exports.isIterator = isIterator;
function hasLength(x) {
if (!isObject(x)) {
return false;
}
return typeof x.length == 'number';
}
exports.hasLength = hasLength;
function hasFd(x) {
if (!isObject(x)) {
return false;
}
return typeof x.fd == 'number';
}
exports.hasFd = hasFd;
function formatTimeDelta(time, showFractions = false) {
const dt = new Date(time).toISOString();
const matches = /(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+).(\d+)Z/.exec(dt);
if (!matches) {
return '';
}
let res = '';
const [, years1970Str, monthsStr, daysStr, hoursStr, minutesStr, secondsStr, fracStr,] = matches;
const years = parseInt(years1970Str) - 1970;
if (years) {
const name = exports.pluralService.select(years) == 'one' ? 'year' : 'years';
res += `${years} ${name}, `;
}
const months = parseInt(monthsStr) - 1;
if (months) {
const name = exports.pluralService.select(months) == 'one' ? 'month' : 'months';
res += `${months} ${name}, `;
}
const days = parseInt(daysStr) - 1;
if (days) {
const name = exports.pluralService.select(days) == 'one' ? 'day' : 'days';
res += `${days} ${name}, `;
}
const hours = parseInt(hoursStr);
if (hours) {
res += `${hoursStr}:`;
}
res += `${minutesStr}:${secondsStr}`;
if (showFractions) {
res += `.${fracStr}`;
}
return res;
}
exports.formatTimeDelta = formatTimeDelta;
function handleUnit(unit) {
const res = {};
if (typeof unit == 'string') {
for (const unitKey of pluralKeys) {
res[unitKey] = unit;
}
}
else if (Array.isArray(unit)) {
res['one'] = unit[0];
for (const unitKey of pluralKeys) {
if (!hasOwnProperty(res, unitKey)) {
res[unitKey] = unit[1];
}
}
}
else if (isObject(unit)) {
Object.assign(res, unit);
const defaultVal = res['many'] ?? res['few'] ?? res['one'] ?? res['other'] ?? 'it';
for (const unitKey of pluralKeys) {
if (!hasOwnProperty(res, unitKey)) {
res[unitKey] = defaultVal;
}
}
}
return res;
}
exports.handleUnit = handleUnit;
function scaleUnit(x) {
const sign = Math.sign(x);
const val = Math.abs(x);
if (val < 1000) {
return x.toString();
}
const digits = Math.trunc(Math.log10(val)) + 1;
if (digits >= 3 && digits <= 6) {
const res = sign * Math.round(val * 100 / 1e3) / 100;
return `${res.toFixed(2)}k`;
}
if (digits > 6 && digits <= 9) {
const res = sign * Math.round(val * 100 / 1e6) / 100;
return `${res.toFixed(2)}M`;
}
if (digits > 9 && digits <= 12) {
const res = sign * Math.round(val * 100 / 1e9) / 100;
return `${res.toFixed(2)}G`;
}
const res = sign * Math.round(val * 100 / 1e12) / 100;
return `${res.toFixed(2)}T`;
}
exports.scaleUnit = scaleUnit;
//# sourceMappingURL=utils.js.map
;