From 7a9b319513e39f11f2e4db46599ffb86d41914da Mon Sep 17 00:00:00 2001 From: kuznya Date: Sat, 10 Aug 2024 20:34:50 +0300 Subject: [PATCH] refact consoleApp --- js/consoleApp.js | 87 ++++++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/js/consoleApp.js b/js/consoleApp.js index afee762..6c9f39e 100644 --- a/js/consoleApp.js +++ b/js/consoleApp.js @@ -1,63 +1,62 @@ class cLog { - constructor (area) { - this.area = area; - } + constructor(area) { + this.area = area; + } - topprint(s) { - const v = this.area.value; - this.area.value = v ? `${s}\n${v}` : s; - } + topprint(s) { + const v = this.area.value; + this.area.value = v ? `${s}\n${v}` : s; + } - print(s) { - const v = this.area.value; - this.area.value = v ? `${v}\n${s}` : s; - } + print(s) { + const v = this.area.value; + this.area.value = v ? `${v}\n${s}` : s; + } - putlines(lines) { - this.area.value = lines.join('\n') - } + putlines(lines) { + this.area.value = lines.join('\n') + } - clear() { - this.area.value = ''; - } + clear() { + this.area.value = ''; + } } - const write = (s) => { document.write(s); }; - const elem = (id) => document.getElementById(id); + +const write = (s) => document.write(s); +const elem = (id) => document.getElementById(id); class ConsoleApp { - constructor(renderHeader) { - this.logAreaId = 'logArea'; - this.renderHeader = renderHeader ? renderHeader : () => {}; - this.drawApp(); - this.log = new cLog(elem(this.logAreaId)); - } + constructor(renderHeader) { + this.logAreaId = 'logArea'; + this.renderHeader = renderHeader ? renderHeader : () => {}; + this.drawApp(); + this.log = new cLog(elem(this.logAreaId)); + } - drawApp () { - write('
\n'); - write(this.renderHeader()); - write(`\n`); - write('
\n'); - } + drawApp() { + write('
\n'); + write(this.renderHeader()); + write(`\n`); + write('
\n'); + } - print(s) { - this.log.print(s) - } + print(s) { + this.log.print(s) + } - formatNumber (value, places) { - const len = value.toString().length; - const spaces = Math.max(0, places - len); - return `${' '.repeat(spaces)}${value}`; - }; + formatNumber(value, places) { + const len = value.toString().length; + const spaces = Math.max(0, places - len); + return `${' '.repeat(spaces)}${value}`; + }; - formatDict (dict) { + formatDict(dict) { const aa = []; for (const k in dict) { - if (!dict.hasOwnProperty(k)) { - continue; - } + if (!dict.hasOwnProperty(k)) { continue; } const value = dict[k]; - let repr = typeof value === 'string' ? `"${value}"` : value; + const repr = typeof value === 'string' ? `"${value}"` : value; aa.push(`${k}: ${repr}`); } return `{${aa.join(', ')}}`;