Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: standard functions #95

Merged
merged 2 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
}
Loading