commit 545d896f181cb5721a4b2a693057f592717f1659
parent f412a7a18395796a714bca15bde4132c6e529211
Author: NunoSempere <nuno.sempere@protonmail.com>
Date: Tue, 3 May 2022 12:16:50 -0400
tweak: output an object instead of an array
Diffstat:
3 files changed, 42 insertions(+), 7 deletions(-)
diff --git 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
@@ -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
@@ -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("");
};