Skip to content

Commit

Permalink
improve “null” handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Dosant committed Sep 17, 2020
1 parent f6c31fd commit 0f9a807
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import {
ValueClickTriggerEventScope,
} from './url_drilldown_scope';

const createPoint = ({ field, value }: { field: string; value: string }) => ({
const createPoint = ({
field,
value,
}: {
field: string;
value: string | null | number | boolean;
}) => ({
table: {
columns: [
{
Expand Down Expand Up @@ -99,4 +105,25 @@ describe('VALUE_CLICK_TRIGGER', () => {
`);
});
});

describe('handles undefined, null or missing values', () => {
test('undefined or missing values are removed from the result scope', () => {
const point = createPoint({ field: undefined } as any);
const eventScope = getEventScope({
data: { data: [point] },
}) as ValueClickTriggerEventScope;

expect('key' in eventScope).toBeFalsy();
expect('value' in eventScope).toBeFalsy();
});

test('null value stays in the result scope', () => {
const point = createPoint({ field: 'field', value: null });
const eventScope = getEventScope({
data: { data: [point] },
}) as ValueClickTriggerEventScope;

expect(eventScope.value).toBeNull();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ export type UrlDrilldownEventScope = ValueClickTriggerEventScope | RangeSelectTr
export type EventScopeInput = ActionContext;
export interface ValueClickTriggerEventScope {
key?: string;
value: string | number | boolean;
value: Primitive;
negate: boolean;
points: Array<{ key?: string; value: string | number | boolean }>;
points: Array<{ key?: string; value: Primitive }>;
}
export interface RangeSelectTriggerEventScope {
key: string;
Expand Down Expand Up @@ -144,7 +144,7 @@ function getEventScopeFromValueClickTriggerContext(
const points = eventScopeInput.data.data.map(({ table, value, column: columnIndex }) => {
const column = table.columns[columnIndex];
return {
value: toPrimitiveOrUndefined(value) as string | number | boolean,
value: toPrimitiveOrUndefined(value) as Primitive,
key: toPrimitiveOrUndefined(column?.meta?.aggConfigParams?.field) as string | undefined,
};
});
Expand Down Expand Up @@ -186,10 +186,12 @@ export function getMockEventScope([trigger]: UrlTrigger[]): UrlDrilldownEventSco
}
}

function toPrimitiveOrUndefined(v: unknown): string | number | boolean | undefined {
if (typeof v === 'number' || typeof v === 'boolean' || typeof v === 'string') return v;
type Primitive = string | number | boolean | null;
function toPrimitiveOrUndefined(v: unknown): Primitive | undefined {
if (typeof v === 'number' || typeof v === 'boolean' || typeof v === 'string' || v === null)
return v;
if (typeof v === 'object' && v instanceof Date) return v.toISOString();
if (typeof v === 'undefined' || v === null) return undefined;
if (typeof v === 'undefined') return undefined;
return String(v);
}

Expand Down

0 comments on commit 0f9a807

Please sign in to comment.