Skip to content

Commit

Permalink
fix: standard functions
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
koladilip committed Jun 14, 2024
1 parent 8849705 commit f763a67
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
32 changes: 21 additions & 11 deletions src/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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)) {
Expand Down
70 changes: 61 additions & 9 deletions test/scenarios/standard_functions/data.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
8 changes: 1 addition & 7 deletions test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}

0 comments on commit f763a67

Please sign in to comment.