UNPKG

@accounter/israeli-vat-scraper

Version:
53 lines (52 loc) 1.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UserPrompt = void 0; class UserPrompt { _status = {}; errors = []; constructor() { return; } update = (locations, status, logger) => { let current = this._status; for (let i = 0; i < locations.length; i++) { const location = locations[i]; current[location] ??= ['', {}]; if (i === locations.length - 1) { if (current[location][0] === 'Error') { break; } current[location][0] = status; break; } current = current[location][1]; } this.doLog(logger); return; }; doLog = (logger) => { let message = ''; const recursivePrint = (data, prefix) => { const entries = Object.entries(data); if (entries.length > 0) { entries.sort((a, b) => (a[0] > b[0] ? 1 : -1)); for (const [key, value] of entries) { message += `${prefix}${key}: ${value[0]}\n`; recursivePrint(value[1], prefix + ' '); } } }; recursivePrint(this._status, ''); logger.clear(); logger.log(message); }; addError = (location, error, logger) => { this.update(location, 'Error', logger); this.errors.push([location.join('-'), error]); }; printErrors = (logger) => { this.errors.sort((a, b) => (a[0] > b[0] ? 1 : b[0] > a[0] ? -1 : 0)); logger.error(this.errors.map(e => `${e[0]}: ${JSON.stringify(e[1], null, ' ')}`)); }; } exports.UserPrompt = UserPrompt;