Transformations: Add an All Unique Values Reducer (#48653)

This commit is contained in:
Josiah (Jay) Goodson 2022-05-04 14:58:46 -04:00 committed by GitHub
parent dac8abfc2c
commit 570ff074f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -97,6 +97,15 @@ describe('Stats Calculators', () => {
expect(stats.delta).toEqual(300);
});
it('should calculate unique values', () => {
const stats = reduceField({
field: createField('x', [1, 2, 2, 3, 1]),
reducers: [ReducerID.uniqueValues],
});
expect(stats.uniqueValues).toEqual([1, 2, 3]);
});
it('consistently check allIsNull/allIsZero', () => {
const empty = createField('x');
const allNull = createField('x', [null, null, null, null]);

View File

@ -25,6 +25,7 @@ export enum ReducerID {
allIsZero = 'allIsZero',
allIsNull = 'allIsNull',
allValues = 'allValues',
uniqueValues = 'uniqueValues',
}
// Internal function
@ -237,6 +238,15 @@ export const fieldReducers = new Registry<FieldReducerInfo>(() => [
standard: false,
reduce: (field: Field) => ({ allValues: field.values.toArray() }),
},
{
id: ReducerID.uniqueValues,
name: 'All unique values',
description: 'Returns an array with all unique values',
standard: false,
reduce: (field: Field) => ({
uniqueValues: [...new Set(field.values.toArray())],
}),
},
]);
export function doStandardCalcs(field: Field, ignoreNulls: boolean, nullAsZero: boolean): FieldCalcs {