diff --git a/src/engine.ts b/src/engine.ts index fbf8b68..2edcae6 100644 --- a/src/engine.ts +++ b/src/engine.ts @@ -3,7 +3,7 @@ import { JsonTemplateLexer } from './lexer'; import { JsonTemplateParser } from './parser'; import { JsonTemplateReverseTranslator } from './reverse_translator'; import { JsonTemplateTranslator } from './translator'; -import { EngineOptions, Expression, FlatMappingPaths } from './types'; +import { EngineOptions, Expression, FlatMappingPaths, TemplateInput } from './types'; import { CreateAsyncFunction, convertToObjectMapping, isExpression } from './utils'; export class JsonTemplateEngine { @@ -21,10 +21,7 @@ export class JsonTemplateEngine { return Function(DATA_PARAM_KEY, BINDINGS_PARAM_KEY, this.translate(templateOrExpr, options)); } - private static compileAsAsync( - templateOrExpr: string | Expression | FlatMappingPaths[], - options?: EngineOptions, - ): Function { + private static compileAsAsync(templateOrExpr: TemplateInput, options?: EngineOptions): Function { return CreateAsyncFunction( DATA_PARAM_KEY, BINDINGS_PARAM_KEY, @@ -49,10 +46,7 @@ export class JsonTemplateEngine { return convertToObjectMapping(flatMappingAST); } - static create( - templateOrExpr: string | Expression | FlatMappingPaths[], - options?: EngineOptions, - ): JsonTemplateEngine { + static create(templateOrExpr: TemplateInput, options?: EngineOptions): JsonTemplateEngine { return new JsonTemplateEngine(this.compileAsAsync(templateOrExpr, options)); } @@ -63,10 +57,7 @@ export class JsonTemplateEngine { return new JsonTemplateEngine(this.compileAsSync(templateOrExpr, options)); } - static parse( - template: string | Expression | FlatMappingPaths[], - options?: EngineOptions, - ): Expression { + static parse(template: TemplateInput, options?: EngineOptions): Expression { if (isExpression(template)) { return template as Expression; } @@ -78,10 +69,7 @@ export class JsonTemplateEngine { return this.parseMappingPaths(template as FlatMappingPaths[], options); } - static translate( - template: string | Expression | FlatMappingPaths[], - options?: EngineOptions, - ): string { + static translate(template: TemplateInput, options?: EngineOptions): string { return this.translateExpression(this.parse(template, options)); } diff --git a/src/parser.ts b/src/parser.ts index 73041c4..e673fc6 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -1,3 +1,4 @@ +/* eslint-disable import/no-cycle */ import { BINDINGS_PARAM_KEY, DATA_PARAM_KEY, EMPTY_EXPR } from './constants'; import { JsonTemplateEngine } from './engine'; import { JsonTemplateLexerError, JsonTemplateParserError } from './errors'; @@ -305,7 +306,7 @@ export class JsonTemplateParser { } return { pathType: PathType.UNKNOWN, - inferredPathType: this.options?.defaultPathType || PathType.RICH, + inferredPathType: this.options?.defaultPathType ?? PathType.RICH, }; } diff --git a/src/translator.ts b/src/translator.ts index f3add5f..1d90e16 100644 --- a/src/translator.ts +++ b/src/translator.ts @@ -509,7 +509,7 @@ export class JsonTemplateTranslator { code.push(`if(${functionName} && typeof ${functionName} === 'function'){`); code.push(result, '=', functionName, '(', functionArgsStr, ');'); code.push('} else {'); - code.push(result, '=', expr.id, '(', expr.parent || result, ',', functionArgsStr, ');'); + code.push(result, '=', expr.id, '(', expr.parent ?? result, ',', functionArgsStr, ');'); code.push('}'); } else { code.push(result, '=', functionName, '(', functionArgsStr, ');'); diff --git a/src/types.ts b/src/types.ts index 53c283f..ecd16d8 100644 --- a/src/types.ts +++ b/src/types.ts @@ -283,3 +283,5 @@ export type FlatMappingAST = FlatMappingPaths & { inputExpr: PathExpression; outputExpr: PathExpression; }; + +export type TemplateInput = string | Expression | FlatMappingPaths[];