From 545d896f181cb5721a4b2a693057f592717f1659 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Tue, 3 May 2022 12:16:50 -0400 Subject: [PATCH] tweak: output an object instead of an array --- src/example-import.js | 2 +- src/index.js | 43 ++++++++++++++++++++++++++++++++++++++----- src/tests.js | 4 +++- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/example-import.js b/src/example-import.js index 59646f3..286b2f6 100644 --- a/src/example-import.js +++ b/src/example-import.js @@ -2,5 +2,5 @@ import { transformer } from "./index.js"; let printer = (_) => null; let getSimpleSquiggleOutput = (string) => transformer(string, printer); -let result = getSimpleSquiggleOutput("(1 to 10)/(1 to 20)"); +let result = getSimpleSquiggleOutput("(1 to 10)/(1 to 20)").squiggleString; console.log(result); diff --git a/src/index.js b/src/index.js index afeeb52..8f95336 100644 --- a/src/index.js +++ b/src/index.js @@ -229,7 +229,7 @@ let preprocessor = (string, print = console.log) => { }; // preprocessor("1.2 to 10.5 * 1.1 to 20 * 1 to 2.5 * 1 to 5"); -let customToStringHandlerLognormals = (node, options) => { +let customToStringHandlerToGuesstimateSyntax = (node, options) => { if (isArgLognormal(node)) { let factors = getFactors(node); // print(node); @@ -239,6 +239,33 @@ let customToStringHandlerLognormals = (node, options) => { } }; +let toPrecision2 = (f) => f.toPrecision(2); +let toShortGuesstimateString = (node) => { + if (isArgLognormal(node)) { + let factors = getFactors(node); + // print(node); + // print(factors); + let ninetyPercentCI = to90PercentCI(factors[0], factors[1]); + return `${toPrecision2(ninetyPercentCI[0])} to ${toPrecision2( + ninetyPercentCI[1] + )}`; + } else { + return null; + } +}; + +let to90CIArray = (node) => { + if (isArgLognormal(node)) { + let factors = getFactors(node); + // print(node); + // print(factors); + let ninetyPercentCI = to90PercentCI(factors[0], factors[1]); + return [ninetyPercentCI[0], ninetyPercentCI[1]]; + } else { + return null; + } +}; + export function transformer(string, print = console.log) { string = preprocessor(string, print); let transformerOutput = transformerInner(string); @@ -253,8 +280,14 @@ export function transformer(string, print = console.log) { transformerOutput = transformerInner(string); stringNew = transformerOutput.toString(); } - let stringNewAs90PercentCI = transformerOutput.toString({ - handler: customToStringHandlerLognormals, - }); - return [stringNew, stringNewAs90PercentCI]; + let squiggleString = stringNew; + let shortGuesstimateString = toShortGuesstimateString(transformerOutput); + let array90CI = to90CIArray(transformerOutput); + // console.log(transformerOutput); + let result = { + squiggleString: squiggleString, + shortGuesstimateString: shortGuesstimateString, + array90CI: array90CI, + }; + return result; } diff --git a/src/tests.js b/src/tests.js index c22303f..394b90d 100644 --- a/src/tests.js +++ b/src/tests.js @@ -14,7 +14,9 @@ let testTransformer = (string) => { let result = transformer(string, print); print(""); console.groupEnd(); - console.log(`=> ${result}`); + console.log(`=> ${result.squiggleString}`); + print(""); + print(result); print("-".repeat(52)); console.log(""); };