Skip to content

Commit

Permalink
getMappingValue data
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Dec 17, 2024
1 parent 3525485 commit 589ee6d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
4 changes: 1 addition & 3 deletions packages/sources/node/src/lib/destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ function resolveMappingData(
): Destination.Data {
if (!data) return;

return Array.isArray(data)
? data.map((item) => getMappingValue(event, item))
: getMappingValue(event, data);
return getMappingValue(event, data);
}

export async function destinationPush(
Expand Down
4 changes: 1 addition & 3 deletions packages/sources/walkerjs/src/lib/destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ function resolveMappingData(
): Destination.Data {
if (!data) return;

return Array.isArray(data)
? data.map((item) => getMappingValue(event, item))
: getMappingValue(event, data);
return getMappingValue(event, data);
}

export function destinationPush(
Expand Down
3 changes: 1 addition & 2 deletions packages/types/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface EventMapping {
mappingKey?: string;
}

export type Data = Value | Values;
export type Value = ValueType | Array<ValueType>;
export type Values = Array<Value>;
export type ValueType = string | ValueConfig;
Expand All @@ -47,8 +48,6 @@ export type Condition = (
instance?: WalkerOS.Instance,
) => boolean;

export type Data = Value | Values;

export type Fn = (
event: WalkerOS.PartialEvent,
mapping: Value,
Expand Down
17 changes: 15 additions & 2 deletions packages/utils/src/core/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Mapping, WalkerOS } from '@elbwalker/types';
import { getGrantedConsent } from './consent';
import { getByPath } from './byPath';
import { isDefined, isObject } from './is';
import { isArray, isDefined, isObject } from './is';
import { castToProperty } from './property';

export function getMappingEvent(
Expand All @@ -22,7 +22,7 @@ export function getMappingEvent(
| Mapping.EventConfig<unknown>[],
) => {
if (!eventMapping) return;
eventMapping = Array.isArray(eventMapping) ? eventMapping : [eventMapping];
eventMapping = isArray(eventMapping) ? eventMapping : [eventMapping];

return eventMapping.find(
(eventMapping) =>
Expand Down Expand Up @@ -51,6 +51,19 @@ export function getMappingEvent(
}

export function getMappingValue(
obj: WalkerOS.PartialEvent | WalkerOS.AnyObject,
data: Mapping.Data,
options: Mapping.Options = {},
): WalkerOS.Property | undefined {
const mappings = isArray(data) ? data : [data];

for (const mapping of mappings) {
const result = processMappingValue(obj, mapping, options);
if (isDefined(result)) return result;
}
}

function processMappingValue(
obj: WalkerOS.PartialEvent | WalkerOS.AnyObject,
mapping: Mapping.Value,
options: Mapping.Options = {},
Expand Down
1 change: 1 addition & 0 deletions website/src/components/organisms/destination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ function resolveMappingData(
): Destination.Data {
if (!data) return;

// @TODO update
return Array.isArray(data)
? data.map((item) => getMappingValue(event, item))
: getMappingValue(event, data);
Expand Down
1 change: 1 addition & 0 deletions website/src/components/templates/destination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ function resolveMappingData(
): Destination.Data {
if (!data) return;

// @TODO update
return Array.isArray(data)
? data.map((item) => getMappingValue(event, item))
: getMappingValue(event, data);
Expand Down

0 comments on commit 589ee6d

Please sign in to comment.