From 197b8b8e1733c9d976bd3bd67147caaafb2769cb Mon Sep 17 00:00:00 2001 From: Nachiket Mistry Date: Tue, 5 Dec 2023 11:34:51 -0800 Subject: [PATCH] refactor common function (#28) Co-authored-by: Nachiket Mistry --- src/FancyLogger.ts | 3 +-- src/StatedREPL.ts | 25 ++++--------------------- src/TemplateProcessor.ts | 12 ++++++------ src/utils/stringify.ts | 25 +++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 29 deletions(-) create mode 100644 src/utils/stringify.ts diff --git a/src/FancyLogger.ts b/src/FancyLogger.ts index 2292c221..5658ab24 100644 --- a/src/FancyLogger.ts +++ b/src/FancyLogger.ts @@ -1,5 +1,4 @@ -import ConsoleLogger, {LOG_LEVELS} from "./ConsoleLogger.js"; -import TemplateProcessor from "./TemplateProcessor.js"; +import { LOG_LEVELS } from "./ConsoleLogger.js"; export default class FancyLogger { static async getLogger() { diff --git a/src/StatedREPL.ts b/src/StatedREPL.ts index 025cf1b7..46ddb1ba 100755 --- a/src/StatedREPL.ts +++ b/src/StatedREPL.ts @@ -15,6 +15,7 @@ import repl from 'repl'; import CliCore from './CliCore.js'; import colorizeJson from "json-colorizer"; import chalk from 'chalk'; +import { circularReplacer, stringifyTemplateJSON } from './utils/stringify.js'; export default class StatedREPL { @@ -127,29 +128,11 @@ export default class StatedREPL { } static printFunc(key, value) { - if(value === undefined){ - return null; - } - if (value?._jsonata_lambda || value?._stated_function__) { - return "{function:}"; - } - if (key === 'compiledExpr__') { - return "--compiled expression--"; - } - if(null !== value) { - const {_idleTimeout, _onTimeout} = value; - if (_idleTimeout !== undefined && _onTimeout !== undefined) { - return "--interval/timeout--"; - } - if(value instanceof Set){ - return Array.from(value); - } - } - return value; + return circularReplacer(key, value); } - static stringify(o:any, printFunction:(k:any,v:any)=>any=StatedREPL.printFunc){ - return JSON.stringify(o, printFunction, 2); + static stringify(o: any, printFunction?: (k: any, v: any) => any) { + return stringifyTemplateJSON(o, printFunction) } static colorize(s:string):string{ diff --git a/src/TemplateProcessor.ts b/src/TemplateProcessor.ts index b78b9d54..a544e3e3 100644 --- a/src/TemplateProcessor.ts +++ b/src/TemplateProcessor.ts @@ -23,9 +23,9 @@ import path from 'path'; import fs from 'fs'; import ConsoleLogger, {StatedLogger} from "./ConsoleLogger.js"; import FancyLogger from "./FancyLogger.js"; -import {LOG_LEVELS} from "./ConsoleLogger.js"; -import StatedREPL from "./StatedREPL.js"; +import { LOG_LEVELS } from "./ConsoleLogger.js"; import {TimerManager} from "./TimerManager.js"; +import { stringifyTemplateJSON } from './utils/stringify.js'; type MetaInfoMap = Record; @@ -816,7 +816,7 @@ export default class TemplateProcessor { private logOutput() { if (this.isEnabled("debug")) { this.logger.debug(`----------------TEMPLATE OUTPUT (${this.uniqueId})-----------------`) - this.logger.debug(StatedREPL.stringify(this.output)); + this.logger.debug(stringifyTemplateJSON(this.output)); } } @@ -1000,9 +1000,9 @@ export default class TemplateProcessor { this.logger.error(`Error evaluating expression at ${jsonPtr}`); this.logger.error(error); this.logger.debug(`Expression: ${expr__}`); - this.logger.debug(`Target: ${StatedREPL.stringify(target)}`); - this.logger.debug(`Target: ${StatedREPL.stringify(target)}`); - this.logger.debug(`Result: ${StatedREPL.stringify(evaluated)}`); + this.logger.debug(`Target: ${stringifyTemplateJSON(target)}`); + this.logger.debug(`Target: ${stringifyTemplateJSON(target)}`); + this.logger.debug(`Result: ${stringifyTemplateJSON(evaluated)}`); const _error = new Error(error.message); _error.name = "JSONata evaluation exception"; throw _error; diff --git a/src/utils/stringify.ts b/src/utils/stringify.ts new file mode 100644 index 00000000..e52f7e15 --- /dev/null +++ b/src/utils/stringify.ts @@ -0,0 +1,25 @@ +export const circularReplacer = (key: any, value: any) => { + if (value === undefined) { + return null; + } + if (value?._jsonata_lambda || value?._stated_function__) { + return "{function:}"; + } + if (key === 'compiledExpr__') { + return "--compiled expression--"; + } + if (null !== value) { + const { _idleTimeout, _onTimeout } = value; + if (_idleTimeout !== undefined && _onTimeout !== undefined) { + return "--interval/timeout--"; + } + if (value instanceof Set) { + return Array.from(value); + } + } + return value; +} + +export const stringifyTemplateJSON = (o: any, printFunction = circularReplacer) => { + return JSON.stringify(o, printFunction, 2); +} \ No newline at end of file