From f763a671424de9fe6eac4fc8bea49a713ccddfcd Mon Sep 17 00:00:00 2001 From: Dilip Kola Date: Fri, 14 Jun 2024 16:57:12 +0530 Subject: [PATCH] fix: standard functions when standard function is using another one then it won't work in some cases as dependent function will be part of source code. This is fixed by using nested function. --- src/operators.ts | 32 +++++++---- test/scenarios/standard_functions/data.ts | 70 ++++++++++++++++++++--- test/types.ts | 8 +-- 3 files changed, 83 insertions(+), 27 deletions(-) diff --git a/src/operators.ts b/src/operators.ts index 3e27f60..7573a9f 100644 --- a/src/operators.ts +++ b/src/operators.ts @@ -119,13 +119,27 @@ export const binaryOperators = { '**': (val1, val2): string => `${val1}**${val2}`, }; -export const standardFunctions = { - sum: `function ${VARS_PREFIX}sum(arr) { +function getSumFn(prefix: string = ''): string { + return `function ${prefix}sum(arr) { if(!Array.isArray(arr)) { throw new Error('Expected an array'); } return arr.reduce((a, b) => a + b, 0); - }`, + }`; +} + +function getAvgFn(prefix: string = ''): string { + return `function ${prefix}avg(arr) { + if(!Array.isArray(arr)) { + throw new Error('Expected an array'); + } + ${getSumFn()} + return sum(arr) / arr.length; + }`; +} + +export const standardFunctions = { + sum: getSumFn(VARS_PREFIX), max: `function ${VARS_PREFIX}max(arr) { if(!Array.isArray(arr)) { throw new Error('Expected an array'); @@ -138,12 +152,7 @@ export const standardFunctions = { } return Math.min(...arr); }`, - avg: `function ${VARS_PREFIX}avg(arr) { - if(!Array.isArray(arr)) { - throw new Error('Expected an array'); - } - return ${VARS_PREFIX}sum(arr) / arr.length; - }`, + avg: getAvgFn(VARS_PREFIX), length: `function ${VARS_PREFIX}length(arr) { if(!Array.isArray(arr) && typeof arr !== 'string') { throw new Error('Expected an array or string'); @@ -154,9 +163,10 @@ export const standardFunctions = { if(!Array.isArray(arr)) { throw new Error('Expected an array'); } - const mu = ${VARS_PREFIX}avg(arr); + ${getAvgFn()} + const mu = avg(arr); const diffSq = arr.map((el) => (el - mu) ** 2); - return Math.sqrt(${VARS_PREFIX}avg(diffSq)); + return Math.sqrt(avg(diffSq)); }`, first: `function ${VARS_PREFIX}first(arr) { if(!Array.isArray(arr)) { diff --git a/test/scenarios/standard_functions/data.ts b/test/scenarios/standard_functions/data.ts index 3064221..4ca27d8 100644 --- a/test/scenarios/standard_functions/data.ts +++ b/test/scenarios/standard_functions/data.ts @@ -1,16 +1,68 @@ import { Scenario } from '../../types'; +const input = { + arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], + obj: { + foo: 1, + bar: 2, + baz: 3, + quux: 4, + }, +}; + export const data: Scenario[] = [ { - input: { - arr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], - obj: { - foo: 1, - bar: 2, - baz: 3, - quux: 4, - }, - }, + template: '.arr.avg()', + input, + output: 5.5, + }, + { + template: '.arr.first()', + input, + output: 1, + }, + { + template: '.arr.index(0)', + input, + output: 1, + }, + { + template: '.arr.index(9)', + input, + output: 10, + }, + { + template: '.arr.last()', + input, + output: 10, + }, + { + template: '.arr.length()', + input, + output: 10, + }, + { + template: '.arr.min()', + input, + output: 1, + }, + { + template: '.arr.max()', + input, + output: 10, + }, + { + template: '.arr.stddev()', + input, + output: 2.8722813232690143, + }, + { + template: '.arr.sum()', + input, + output: 55, + }, + { + input, output: { sum: 55, sum2: 55, diff --git a/test/types.ts b/test/types.ts index 8832881..d45672c 100644 --- a/test/types.ts +++ b/test/types.ts @@ -14,12 +14,6 @@ export type Scenario = { export namespace Scenario { export function getTemplatePath(scenario: Scenario): string { - if (scenario.templatePath) { - return scenario.templatePath; - } - if (scenario.mappingsPath) { - return scenario.mappingsPath; - } - return 'template.jt'; + return scenario.templatePath || scenario.mappingsPath || scenario.template || 'template.jt'; } }