diff --git a/src/operators.ts b/src/operators.ts index bcb3b3c..3e27f60 100644 --- a/src/operators.ts +++ b/src/operators.ts @@ -1,3 +1,5 @@ +import { VARS_PREFIX } from './constants'; + function startsWithStrict(val1, val2): string { return `(typeof ${val1} === 'string' && ${val1}.startsWith(${val2}))`; } @@ -118,57 +120,57 @@ export const binaryOperators = { }; export const standardFunctions = { - sum: `function sum(arr) { + sum: `function ${VARS_PREFIX}sum(arr) { if(!Array.isArray(arr)) { throw new Error('Expected an array'); } return arr.reduce((a, b) => a + b, 0); }`, - max: `function max(arr) { + max: `function ${VARS_PREFIX}max(arr) { if(!Array.isArray(arr)) { throw new Error('Expected an array'); } return Math.max(...arr); }`, - min: `function min(arr) { + min: `function ${VARS_PREFIX}min(arr) { if(!Array.isArray(arr)) { throw new Error('Expected an array'); } return Math.min(...arr); }`, - avg: `function avg(arr) { + avg: `function ${VARS_PREFIX}avg(arr) { if(!Array.isArray(arr)) { throw new Error('Expected an array'); } - return sum(arr) / arr.length; + return ${VARS_PREFIX}sum(arr) / arr.length; }`, - length: `function length(arr) { + length: `function ${VARS_PREFIX}length(arr) { if(!Array.isArray(arr) && typeof arr !== 'string') { throw new Error('Expected an array or string'); } return arr.length; }`, - stddev: `function stddev(arr) { + stddev: `function ${VARS_PREFIX}stddev(arr) { if(!Array.isArray(arr)) { throw new Error('Expected an array'); } - const mu = avg(arr); + const mu = ${VARS_PREFIX}avg(arr); const diffSq = arr.map((el) => (el - mu) ** 2); - return Math.sqrt(avg(diffSq)); + return Math.sqrt(${VARS_PREFIX}avg(diffSq)); }`, - first: `function first(arr) { + first: `function ${VARS_PREFIX}first(arr) { if(!Array.isArray(arr)) { throw new Error('Expected an array'); } return arr[0]; }`, - last: `function last(arr) { + last: `function ${VARS_PREFIX}last(arr) { if(!Array.isArray(arr)) { throw new Error('Expected an array'); } return arr[arr.length - 1]; }`, - index: `function index(arr, i) { + index: `function ${VARS_PREFIX}index(arr, i) { if(!Array.isArray(arr)) { throw new Error('Expected an array'); } @@ -177,7 +179,7 @@ export const standardFunctions = { } return arr[i]; }`, - keys: 'function keys(obj) { return Object.keys(obj); }', + keys: `function ${VARS_PREFIX}keys(obj) { return Object.keys(obj); }`, }; export function isStandardFunction(name: string): boolean { diff --git a/src/translator.ts b/src/translator.ts index cb6dc35..a81652f 100644 --- a/src/translator.ts +++ b/src/translator.ts @@ -519,7 +519,17 @@ 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, + '=', + VARS_PREFIX, + expr.id, + '(', + expr.parent ?? result, + ',', + functionArgsStr, + ');', + ); code.push('}'); } else { code.push(result, '=', functionName, '(', functionArgsStr, ');'); diff --git a/test/scenarios/standard_functions/template.jt b/test/scenarios/standard_functions/template.jt index fd6ff03..46a4f9e 100644 --- a/test/scenarios/standard_functions/template.jt +++ b/test/scenarios/standard_functions/template.jt @@ -1,5 +1,6 @@ const arr = .arr; const obj = .obj; +const keys = obj.keys(); { sum: .arr.sum(), sum2: (arr.index(0) + arr.index(-1)) * arr.length() / 2, @@ -10,5 +11,5 @@ const obj = .obj; length: arr.length(), first: arr.first(), last: arr.last(), - keys: obj.keys(), + keys, } \ No newline at end of file