Skip to content

Commit

Permalink
fix(core/models): ALL_VALUES_OF is not extracting values from main forms
Browse files Browse the repository at this point in the history
  • Loading branch information
trik committed Mar 31, 2022
1 parent 662d6a1 commit 3da14e6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
19 changes: 19 additions & 0 deletions projects/core/models/src/utils/expression-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {ALL_VALUES_OF, MainForm} from './expression-utils';

describe('ALL_VALUES_OF', () => {
it('should get all values by key in dataset', () => {
const result = ALL_VALUES_OF(forms, 'dog_name');
const dogs = 4;
expect(result.length).toBe(dogs);
for (let i = 1; i <= dogs; i++) {
expect(result).toContain(`dog${i}`);
}
});
});

const forms: MainForm[] = [
{dog_name: 'dog1', reps: {'rep_1': [{dog_name: 'dog4'}]}},
{},
{dog_name: 'dog2'},
{dog_name: 'dog3'},
];
28 changes: 18 additions & 10 deletions projects/core/models/src/utils/expression-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ export class AjfExpressionUtils {
};
}

const nonNullInstances = (reps: Instances | undefined): reps is Instances => reps != null;

/**
* UTILITY FUNCION
* This function provide a deep copy builder of array of main forms.
Expand Down Expand Up @@ -669,16 +671,22 @@ export function getCoordinate(source: any, zoom?: number): [number, number, numb
* Calculates all the possible results that a field has taken
*/
export function ALL_VALUES_OF(mainforms: MainForm[], fieldName: string): string[] {
const allreps = (mainforms || [])
.slice(0)
.map(m => m.reps)
.filter(c => c != null)
.map((i: unknown) =>
Object.keys(i as Instances)
.map(k => (i as Instances)[k])
.flat(),
)
.flat();
const forms = [...(mainforms || [])];
const allreps = [
...forms.map(form => {
const {reps, ...v} = form;
return v;
}),
...forms
.map(m => m.reps)
.filter(nonNullInstances)
.map(i =>
Object.keys(i)
.map(k => i[k])
.flat(),
)
.flat(),
];

return [...new Set(allreps.filter(f => f[fieldName] != null).map(f => `${f[fieldName]}`))];
}
Expand Down

0 comments on commit 3da14e6

Please sign in to comment.