rudder-json-template-engine
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -423,9 +423,17 @@ "use strict"; | ||
} | ||
scanPunctuatorForRepeatedTokens() { | ||
let start = this.idx, ch1 = this.codeChars[this.idx], ch2 = this.codeChars[this.idx + 1]; | ||
if (ch1 === ch2 && '|&*.=>?<'.includes(ch1)) { | ||
this.idx += 2; | ||
scanPunctuatorForRepeatedTokens(validSymbols, numRepeations = 2) { | ||
let start = this.idx; | ||
let ch = this.codeChars[this.idx]; | ||
let tokenVal = ch; | ||
for (let i = 1; i < numRepeations; i++) { | ||
if (this.codeChars[this.idx + i] !== ch) { | ||
return; | ||
} | ||
tokenVal += ch; | ||
} | ||
if (validSymbols.includes(ch)) { | ||
this.idx += numRepeations; | ||
return { | ||
type: types_1.TokenType.PUNCT, | ||
value: ch1 + ch2, | ||
value: tokenVal, | ||
range: [start, this.idx], | ||
@@ -460,3 +468,4 @@ }; | ||
this.scanPunctuatorForEquality() || | ||
this.scanPunctuatorForRepeatedTokens() || | ||
this.scanPunctuatorForRepeatedTokens('?', 3) || | ||
this.scanPunctuatorForRepeatedTokens('|&*.=>?<', 2) || | ||
this.scanSingleCharPunctuators()); | ||
@@ -463,0 +472,0 @@ } |
@@ -28,7 +28,8 @@ import { Expression } from './types'; | ||
private parseObjectFilter; | ||
private combineObjectFilters; | ||
private parseObjectFiltersExpr; | ||
private parseConditionalExpr; | ||
private parseArrayFiltersExpr; | ||
private parseCoalescingExpr; | ||
private combineExpressionsAsBinaryExpr; | ||
private parseArrayCoalesceExpr; | ||
private parseCoalesceExpr; | ||
private parseLogicalORExpr; | ||
@@ -35,0 +36,0 @@ private parseLogicalANDExpr; |
@@ -69,3 +69,3 @@ "use strict"; | ||
case types_1.OperatorType.ASSIGNMENT: | ||
return this.parseCoalescingExpr(); | ||
return this.parseCoalesceExpr(); | ||
case types_1.OperatorType.COALESCING: | ||
@@ -315,19 +315,2 @@ return this.parseLogicalORExpr(); | ||
} | ||
combineObjectFilters(objectFilters) { | ||
if (objectFilters.length <= 1) { | ||
return objectFilters; | ||
} | ||
const expr1 = objectFilters.shift(); | ||
const expr2 = this.combineObjectFilters(objectFilters); | ||
return [ | ||
{ | ||
type: types_1.SyntaxType.OBJECT_FILTER_EXPR, | ||
filter: { | ||
type: types_1.SyntaxType.LOGICAL_AND_EXPR, | ||
op: '&&', | ||
args: [expr1.filter, expr2[0].filter], | ||
}, | ||
}, | ||
]; | ||
} | ||
parseObjectFiltersExpr() { | ||
@@ -343,3 +326,3 @@ const objectFilters = []; | ||
else { | ||
objectFilters.push(expr); | ||
objectFilters.push(expr.filter); | ||
} | ||
@@ -351,3 +334,10 @@ this.lexer.expect('}'); | ||
} | ||
return [...this.combineObjectFilters(objectFilters), ...indexFilters]; | ||
if (!objectFilters.length) { | ||
return indexFilters; | ||
} | ||
const objectFilter = { | ||
type: types_1.SyntaxType.OBJECT_FILTER_EXPR, | ||
filter: this.combineExpressionsAsBinaryExpr(objectFilters, types_1.SyntaxType.LOGICAL_AND_EXPR, '&&'), | ||
}; | ||
return [objectFilter, ...indexFilters]; | ||
} | ||
@@ -396,3 +386,21 @@ parseConditionalExpr() { | ||
} | ||
parseCoalescingExpr() { | ||
combineExpressionsAsBinaryExpr(values, type, op) { | ||
if (!values?.length) { | ||
throw new errors_1.JsonTemplateParserError('expected at least 1 expression'); | ||
} | ||
if (values.length === 1) { | ||
return values[0]; | ||
} | ||
return { | ||
type, | ||
op, | ||
args: [values.shift(), this.combineExpressionsAsBinaryExpr(values, type, op)], | ||
}; | ||
} | ||
parseArrayCoalesceExpr() { | ||
this.lexer.ignoreTokens(1); | ||
const expr = this.parseArrayExpr(); | ||
return this.combineExpressionsAsBinaryExpr(expr.elements, types_1.SyntaxType.LOGICAL_COALESCE_EXPR, '??'); | ||
} | ||
parseCoalesceExpr() { | ||
let expr = this.parseNextExpr(types_1.OperatorType.COALESCING); | ||
@@ -403,3 +411,3 @@ if (this.lexer.match('??')) { | ||
op: this.lexer.value(), | ||
args: [expr, this.parseCoalescingExpr()], | ||
args: [expr, this.parseCoalesceExpr()], | ||
}; | ||
@@ -846,2 +854,5 @@ } | ||
} | ||
if (this.lexer.match('???')) { | ||
return this.parseArrayCoalesceExpr(); | ||
} | ||
if (this.lexer.match('.') && this.lexer.matchINT(1) && !this.lexer.match('.', 2)) { | ||
@@ -848,0 +859,0 @@ return this.parseFloatingNumber(); |
# Changelog | ||
## [0.2.3](https://github.com/rudderlabs/rudder-json-template-engine/compare/v0.2.2...v0.2.3) (2022-12-01) | ||
### Features | ||
* add support for array coalescing ([#15](https://github.com/rudderlabs/rudder-json-template-engine/issues/15)) ([2f3bf0f](https://github.com/rudderlabs/rudder-json-template-engine/commit/2f3bf0fba589edc403b76e5b1da9437fbe91524c)) | ||
## [0.2.2](https://github.com/rudderlabs/rudder-json-template-engine/compare/v0.2.1...v0.2.2) (2022-11-30) | ||
@@ -4,0 +11,0 @@ |
{ | ||
"name": "rudder-json-template-engine", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"homepage": "https://github.com/rudderlabs/rudder-workflow-engine", | ||
@@ -5,0 +5,0 @@ "description": "A library for evaluating JSON template expressions.", |
112526
2870