From c5eae3e451bc8b26fd0cf93b099d25a437351647 Mon Sep 17 00:00:00 2001 From: Corey Pyle Date: Thu, 19 Oct 2023 16:56:43 -0400 Subject: [PATCH 1/2] JavaScript (v3): libs - Update and refactor Scenario logging. --- javascriptv3/example_code/libs/logger.js | 51 ++++++++++++++----- javascriptv3/example_code/libs/package.json | 1 + .../libs/scenario/scenario-example.js | 6 +-- .../example_code/libs/scenario/scenario.js | 17 +++---- javascriptv3/package-lock.json | 6 +++ 5 files changed, 57 insertions(+), 24 deletions(-) diff --git a/javascriptv3/example_code/libs/logger.js b/javascriptv3/example_code/libs/logger.js index 73c1657df32..ec7ddda5451 100644 --- a/javascriptv3/example_code/libs/logger.js +++ b/javascriptv3/example_code/libs/logger.js @@ -3,7 +3,13 @@ * SPDX-License-Identifier: Apache-2.0 */ +import wrap from "fast-word-wrap"; + export class Logger { + constructor(lineLength = 80) { + this.lineLength = lineLength; + } + /** * @param {string} message */ @@ -12,6 +18,37 @@ export class Logger { return Promise.resolve(); } + hr() { + return "\n", "*".repeat(this.lineLength), "\n"; + } + + /** + * @param {string} message + */ + box(message) { + const linePrefix = "* "; + const lineSuffix = " *"; + + const maxContentLength = this.lineLength - (linePrefix + lineSuffix).length; + const chunks = message + .split("\n") + .map((l) => l && wrap(l, maxContentLength).split("\n")) + .flat(); + + return ` +${"*".repeat(this.lineLength)} +${chunks + .map( + (c) => + `${linePrefix}${ + c + " ".repeat(maxContentLength - c.length) + }${lineSuffix}`, + ) + .join("\n")} +${"*".repeat(this.lineLength)} +`; + } + /** * Log a horizontal rule to the console. If a message is provided, * log a section header. @@ -19,19 +56,9 @@ export class Logger { */ logSeparator(message) { if (!message) { - console.log("\n", "*".repeat(80), "\n"); + console.log(this.hr()); } else { - console.log( - "\n", - "*".repeat(80), - "\n", - "** ", - message, - " ".repeat(80 - message.length - 8), - "**\n", - "*".repeat(80), - "\n", - ); + console.log(this.box(message)); } } } diff --git a/javascriptv3/example_code/libs/package.json b/javascriptv3/example_code/libs/package.json index 35bf2c164e5..d4240152349 100644 --- a/javascriptv3/example_code/libs/package.json +++ b/javascriptv3/example_code/libs/package.json @@ -12,6 +12,7 @@ "@aws-sdk/client-lambda": "^3.168.0", "@types/archiver": "^5.3.1", "archiver": "^5.3.1", + "fast-word-wrap": "^1.1.0", "uuid": "^9.0.0" } } diff --git a/javascriptv3/example_code/libs/scenario/scenario-example.js b/javascriptv3/example_code/libs/scenario/scenario-example.js index cfb6576c485..87778302332 100644 --- a/javascriptv3/example_code/libs/scenario/scenario-example.js +++ b/javascriptv3/example_code/libs/scenario/scenario-example.js @@ -12,7 +12,7 @@ import { const greet = new ScenarioOutput( "greet", - "Hi! This is a scenario. It can handle your " + "inputs and outputs.", + "Hi! This is a scenario. It can handle your inputs and outputs.", ); const describeInput = new ScenarioOutput( @@ -29,7 +29,7 @@ const getFoods = new ScenarioInput("foods", "Choose your favorite foods.", { const getAge = new ScenarioInput( "age", "Select your age range (my apologies if you're younger or older than the provided ranges):", - { type: "select", choices: ["18-30", "31-50", "50-100"] }, + { type: "select", choices: ["18-30", "31-50", "51-100"] }, ); const getName = new ScenarioInput("name", "What's your name?", { @@ -48,7 +48,7 @@ const describeOutput = new ScenarioOutput( const dynamicOutput = new ScenarioOutput( "dynamicOutput", /** - * @param {{ "foods": string[] }} c + * @param {{ name: string, age: string, foods: string[] }} c */ (c) => `Hi, ${c.name}! You are between the ages of ${c.age}. ` + diff --git a/javascriptv3/example_code/libs/scenario/scenario.js b/javascriptv3/example_code/libs/scenario/scenario.js index db18b7da19c..facc216a6a1 100644 --- a/javascriptv3/example_code/libs/scenario/scenario.js +++ b/javascriptv3/example_code/libs/scenario/scenario.js @@ -15,13 +15,6 @@ export class Step { this.name = name; } - /** - * Alias for "name". - */ - get key() { - return this.name; - } - /** * @param {Record} context */ @@ -34,7 +27,7 @@ export class ScenarioOutput extends Step { /** * @param {string} name * @param {string | (context: Record) => string} value - * @param {{ slow: boolean }} options + * @param {{ slow: boolean, header: boolean }} options */ constructor(name, value, options = { slow: true }) { super(name); @@ -53,7 +46,13 @@ export class ScenarioOutput extends Step { const paddingTop = "\n"; const paddingBottom = "\n"; const logger = this.options.slow ? this.slowLogger : this.logger; - await logger.log(paddingTop + output + paddingBottom); + const message = paddingTop + output + paddingBottom; + + if (this.options.header) { + this.logger.logSeparator(message); + } else { + await logger.log(message); + } } } diff --git a/javascriptv3/package-lock.json b/javascriptv3/package-lock.json index 80356ff6625..26c901dc255 100644 --- a/javascriptv3/package-lock.json +++ b/javascriptv3/package-lock.json @@ -3947,6 +3947,7 @@ "@aws-sdk/client-lambda": "^3.168.0", "@types/archiver": "^5.3.1", "archiver": "^5.3.1", + "fast-word-wrap": "^1.1.0", "uuid": "^9.0.0" } }, @@ -49420,6 +49421,11 @@ "dev": true, "license": "MIT" }, + "node_modules/fast-word-wrap": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/fast-word-wrap/-/fast-word-wrap-1.1.0.tgz", + "integrity": "sha512-1CYI4rbsLfdU928Rmfz+zVCxYqj9MeZRD3tqYV7Msc1TQ/zIXFZOD3OkVHU/iheoQ5xuVpVIFoEcwggBOC9sTw==" + }, "node_modules/fast-xml-parser": { "version": "4.2.4", "funding": [ From be588824c1ae2efd811543c643252aac98d57400 Mon Sep 17 00:00:00 2001 From: Corey Pyle Date: Fri, 20 Oct 2023 11:31:48 -0400 Subject: [PATCH 2/2] JavaScript (v3): Refactor logger box. --- javascriptv3/example_code/libs/logger.js | 25 ++++++++++++------------ 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/javascriptv3/example_code/libs/logger.js b/javascriptv3/example_code/libs/logger.js index ec7ddda5451..8012a009b59 100644 --- a/javascriptv3/example_code/libs/logger.js +++ b/javascriptv3/example_code/libs/logger.js @@ -19,7 +19,7 @@ export class Logger { } hr() { - return "\n", "*".repeat(this.lineLength), "\n"; + return ["\n", "*".repeat(this.lineLength), "\n"].join(""); } /** @@ -35,18 +35,17 @@ export class Logger { .map((l) => l && wrap(l, maxContentLength).split("\n")) .flat(); - return ` -${"*".repeat(this.lineLength)} -${chunks - .map( - (c) => - `${linePrefix}${ - c + " ".repeat(maxContentLength - c.length) - }${lineSuffix}`, - ) - .join("\n")} -${"*".repeat(this.lineLength)} -`; + /** + * @param {string} c + */ + const fill = (c) => c + " ".repeat(maxContentLength - c.length); + + /** + * @param {string} c + */ + const line = (c) => `${linePrefix}${fill(c)}${lineSuffix}`; + + return [this.hr(), chunks.map(line).join("\n"), this.hr()].join(""); } /**