Skip to content

Commit

Permalink
fix: standard function names in generating code (#66)
Browse files Browse the repository at this point in the history
* fix: object index filters

* fix: checking of standard functions

* fix: standard function names in generating code
  • Loading branch information
koladilip authored Jun 7, 2024
1 parent 08852cf commit 79269f3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
28 changes: 15 additions & 13 deletions src/operators.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { VARS_PREFIX } from './constants';

function startsWithStrict(val1, val2): string {
return `(typeof ${val1} === 'string' && ${val1}.startsWith(${val2}))`;
}
Expand Down Expand Up @@ -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');
}
Expand All @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, ');');
Expand Down
3 changes: 2 additions & 1 deletion test/scenarios/standard_functions/template.jt
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -10,5 +11,5 @@ const obj = .obj;
length: arr.length(),
first: arr.first(),
last: arr.last(),
keys: obj.keys(),
keys,
}

0 comments on commit 79269f3

Please sign in to comment.