Skip to content

Commit

Permalink
refactor common function (#28)
Browse files Browse the repository at this point in the history
Co-authored-by: Nachiket Mistry <[email protected]>
  • Loading branch information
nachiketmistry and Nachiket Mistry authored Dec 5, 2023
1 parent f58c7de commit 197b8b8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
3 changes: 1 addition & 2 deletions src/FancyLogger.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
25 changes: 4 additions & 21 deletions src/StatedREPL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
12 changes: 6 additions & 6 deletions src/TemplateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsonPointerString, MetaInfo[]>;
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions src/utils/stringify.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 197b8b8

Please sign in to comment.