Skip to content

Commit

Permalink
filter
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Dec 19, 2024
1 parent 777daa7 commit 5d7b5d9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/sources/datalayer/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable prefer-rest-params */
import type { DataLayer } from '../types';
import { sourceDataLayer } from '..';
import { isArray } from '@elbwalker/utils';
import { isArray, isObject } from '@elbwalker/utils';

describe('source dataLayer', () => {
const elb = jest.fn(); //.mockImplementation(console.log);
Expand Down Expand Up @@ -62,6 +62,27 @@ describe('source dataLayer', () => {
});
});

test('filter', () => {
const mockFn = jest.fn();
sourceDataLayer({
elb,
filter: (event) => {
mockFn(event);
return isObject(event) && event.event !== 'foo';
},
});

let event = { event: 'foo' };
dataLayer.push(event);
expect(elb).toHaveBeenCalledTimes(0);
expect(mockFn).toHaveBeenCalledWith(event);

event = { event: 'bar' };
dataLayer.push(event);
expect(elb).toHaveBeenCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith(event);
});

test('prefix', () => {
sourceDataLayer({ elb, prefix: 'foo' });
dataLayer.push({ event: 'bar baz' });
Expand Down
3 changes: 3 additions & 0 deletions packages/sources/datalayer/src/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export function push(config: Config, ...args: unknown[]) {

config.processing = true;
entries.forEach((obj) => {
// Filter out unwanted events
if (config.filter && !config.filter(obj)) return;

// Map the incoming event to a WalkerOS event
const mappedObj = objToEvent(filterValues(obj), config);

Expand Down
1 change: 1 addition & 0 deletions packages/sources/datalayer/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare global {
export type DataLayer = Array<unknown>;
export interface Config {
elb: WalkerOS.Elb | WalkerOS.AnyFunction;
filter?: (event: unknown) => boolean;
mapping?: Mapping;
name?: string;
prefix: string;
Expand Down

0 comments on commit 5d7b5d9

Please sign in to comment.