Skip to content

Commit

Permalink
fix(core/reports): fix first and last functions
Browse files Browse the repository at this point in the history
  • Loading branch information
robzan8 committed Apr 28, 2023
1 parent 852e3f8 commit 6af2d3f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
4 changes: 2 additions & 2 deletions projects/core/models/src/utils/expression-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ Returns an array containing all the values that the specified field takes in the
The values are converted to strings.
An optional expression can be added to filter which forms to consider in the computation.

`FIRST(forms, expression, dateField = "created_at")`
`FIRST(forms, expression, dateField = $created_at)`
Evaluates the expression in the first form by date.

`LAST(forms, expression, dateField = "created_at")`
`LAST(forms, expression, dateField = $created_at)`
Evaluates the expression in the last form by date.

`FILTER_BY(forms, expression)`
Expand Down
17 changes: 16 additions & 1 deletion projects/core/models/src/utils/expression-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,24 @@ import {
FILTER_BY,
ADD_DAYS,
DAYS_DIFF,
FIRST,
LAST,
} from './expression-utils';

const forms: MainForm[] = [
{
created_at: '0000-00-00',
dog_name: 'dog1',
dog_puppies: 3,
dog_point: 0.1,
to_check: true,
reps: {'rep_1': [{dog_type: 'dogtype1', score: 3}]},
},
{},
{
created_at: '2023-02-19',
},
{
created_at: '9999-99-99',
dog_name: 'dog2',
to_check: true,
dog_puppies: 2,
Expand Down Expand Up @@ -138,3 +144,12 @@ describe('date functions', () => {
expect(DAYS_DIFF('1998-12-31', '1999-12-31')).toBe(-365);
});
});

describe('FIRST/LAST', () => {
it('FIRST', () => {
expect(FIRST(forms, 'dog_name')).toBe('dog1');
});
it('LAST', () => {
expect(LAST(forms, (f: any) => f.dog_name, 'created_at')).toBe('dog2');
});
});
32 changes: 20 additions & 12 deletions projects/core/models/src/utils/expression-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -861,15 +861,19 @@ export function PERCENT(value1: number, value2: number): string {
if (typeof(expression) === 'string') {
expression = createFunction(expression);
}
forms = (forms || []).filter(f => f != null).sort((a, b) => {
const dateA = new Date(b[date] as string).getTime();
const dateB = new Date(a[date] as string).getTime();
return dateA - dateB;
});
forms = (forms || []).filter(f => f != null && f[date] != null);
if (forms.length === 0) {
return undefined;
}
return expression(forms[0]);
let form = forms[0];
let minDate = form[date] as string;
for (let i = 1; i < forms.length; i++) {
if (forms[i][date] as string < minDate) {
form = forms[i];
minDate = form[date] as string;
}
}
return expression(form);
}

/**
Expand All @@ -879,15 +883,19 @@ export function LAST(forms: (Form | MainForm)[], expression: Func|string, date =
if (typeof(expression) === 'string') {
expression = createFunction(expression);
}
forms = (forms || []).filter(f => f != null).sort((a, b) => {
const dateA = new Date(b[date] as string).getTime();
const dateB = new Date(a[date] as string).getTime();
return dateA - dateB;
});
forms = (forms || []).filter(f => f != null && f[date] != null);
if (forms.length === 0) {
return undefined;
}
return expression(forms[forms.length - 1]);
let form = forms[forms.length - 1];
let maxDate = form[date] as string;
for (let i = forms.length - 2; i >= 0; i--) {
if (forms[i][date] as string > maxDate) {
form = forms[i];
maxDate = form[date] as string;
}
}
return expression(form);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions projects/core/reports/src/xls-report/hindikit-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,8 @@ const functionArgs: {[name: string]: string[]} = {
COUNT_REPS: ["arg", "func(form)?"],
ALL_VALUES_OF: ["arg", "field", "func(form)?"],
PERCENT: ["arg", "arg"],
FIRST: ["arg", "field", "arg?"],
LAST: ["arg", "field", "arg?"],
FIRST: ["arg", "func(form)", "field?"],
LAST: ["arg", "func(form)", "field?"],
MAP: ["arg", "func(elem)"],
INCLUDES: ["arg", "arg"],
FILTER_BY: ["arg", "func(form)"],
Expand Down

0 comments on commit 6af2d3f

Please sign in to comment.