Skip to content

Commit

Permalink
set
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkirtzel committed Dec 17, 2024
1 parent 3f84690 commit f821cd4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions packages/types/src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface ValueConfig {
key?: string;
loop?: Loop;
map?: Map;
set?: Value[];
validate?: Validate;
value?: WalkerOS.PropertyType;
}
Expand Down
10 changes: 10 additions & 0 deletions packages/utils/src/__tests__/mapping.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ describe('getMappingValue', () => {
).toStrictEqual([event.event]);
});

test('set', () => {
const event = getEvent('order complete');

expect(
getMappingValue(event, {
set: ['event', 'data', { value: 'static' }, { fn: () => 'fn' }],
}),
).toStrictEqual(['order complete', event.data, 'static', 'fn']);
});

test('map', () => {
const event = createEvent();

Expand Down
13 changes: 6 additions & 7 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 { isArray, isDefined, isObject } from './is';
import { isArray, isDefined, isObject, isString } from './is';
import { castToProperty } from './property';

export function getMappingEvent(
Expand Down Expand Up @@ -77,8 +77,8 @@ function processMappingValue(
return mappings.reduce((acc, mappingItem) => {
if (acc) return acc; // A valid result was already found

const { condition, consent, fn, key, loop, map, validate, value } =
typeof mappingItem == 'string'
const { condition, consent, fn, key, loop, map, set, validate, value } =
isString(mappingItem)
? ({ key: mappingItem } as Mapping.ValueConfig)
: mappingItem;

Expand All @@ -100,7 +100,6 @@ function processMappingValue(
if (loop) {
const [scope, itemMapping] = loop;

// Retrieve the array from the event
const data =
scope === 'this' ? [obj] : getMappingValue(obj, scope, options);

Expand All @@ -111,9 +110,7 @@ function processMappingValue(
)
.filter(isDefined);
}
}

if (map) {
} else if (map) {
mappingValue = Object.entries(map).reduce(
(mappedObj, [mapKey, mapValue]) => {
const result = getMappingValue(obj, mapValue, options);
Expand All @@ -123,6 +120,8 @@ function processMappingValue(
},
{} as WalkerOS.AnyObject,
);
} else if (set) {
mappingValue = set.map((item) => processMappingValue(obj, item, options));
}

// Validate the value
Expand Down

0 comments on commit f821cd4

Please sign in to comment.