Skip to content

Commit 83c12f6

Browse files
committed
feat: add restoreMap function
1 parent 4df6886 commit 83c12f6

File tree

2 files changed

+146
-8
lines changed

2 files changed

+146
-8
lines changed

packages/shared/filter-commons/src/__tests__/condition-utils.spec.ts

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ jest.mock("mendix/filters/builders");
22

33
import { AndCondition } from "mendix/filters";
44
import { equals, literal } from "mendix/filters/builders";
5-
import { compactArray, fromCompactArray, reduceMap, tag } from "../condition-utils";
5+
import { compactArray, fromCompactArray, reduceMap, restoreMap, tag } from "../condition-utils";
66

77
describe("condition-utils", () => {
88
describe("compactArray", () => {
@@ -135,4 +135,134 @@ describe("condition-utils", () => {
135135
});
136136
});
137137
});
138+
139+
describe("restoreMap", () => {
140+
it("restores map with undefined values from undefined condition", () => {
141+
const originalInput = { x: undefined, y: undefined };
142+
const [condition, metadata] = reduceMap(originalInput);
143+
144+
const restored = restoreMap(condition, metadata);
145+
146+
expect(restored).toEqual(originalInput);
147+
});
148+
149+
it("restores map with single condition", () => {
150+
const tagCondition = tag("test");
151+
const originalInput = { a: tagCondition, b: undefined, c: undefined };
152+
const [condition, metadata] = reduceMap(originalInput);
153+
154+
const restored = restoreMap(condition, metadata);
155+
156+
expect(restored).toEqual(originalInput);
157+
});
158+
159+
it("restores map with multiple conditions", () => {
160+
const tagCondition1 = tag("test1");
161+
const tagCondition2 = tag("test2");
162+
const originalInput = { x: tagCondition1, y: undefined, z: tagCondition2, w: undefined };
163+
const [condition, metadata] = reduceMap(originalInput);
164+
165+
const restored = restoreMap(condition, metadata);
166+
167+
expect(restored).toEqual(originalInput);
168+
});
169+
170+
it("restores empty map", () => {
171+
const originalInput = {};
172+
const [condition, metadata] = reduceMap(originalInput);
173+
174+
const restored = restoreMap(condition, metadata);
175+
176+
expect(restored).toEqual(originalInput);
177+
});
178+
179+
it("restores map with mixed condition types", () => {
180+
const tagCondition = tag("tag-test");
181+
const equalsCondition = equals(literal("field"), literal("value"));
182+
const originalInput = {
183+
tag: tagCondition,
184+
equals: equalsCondition,
185+
empty: undefined,
186+
another: undefined
187+
};
188+
const [condition, metadata] = reduceMap(originalInput);
189+
190+
const restored = restoreMap(condition, metadata);
191+
192+
expect(restored).toEqual(originalInput);
193+
});
194+
195+
it("handles manual metadata for single condition case", () => {
196+
const tagCondition = tag("manual-test");
197+
const metadata = JSON.stringify({
198+
length: 1,
199+
keys: ["first", "second", "third"],
200+
0: "second"
201+
});
202+
203+
const restored = restoreMap(tagCondition, metadata);
204+
205+
expect(restored).toEqual({
206+
first: undefined,
207+
second: tagCondition,
208+
third: undefined
209+
});
210+
});
211+
212+
it("handles manual metadata for multiple conditions case", () => {
213+
const tagCondition1 = tag("manual1");
214+
const tagCondition2 = tag("manual2");
215+
const andCondition = {
216+
name: "and",
217+
type: "function",
218+
args: [tagCondition1, tagCondition2]
219+
} as AndCondition;
220+
const metadata = JSON.stringify({
221+
length: 2,
222+
keys: ["a", "b", "c", "d"],
223+
0: "a",
224+
1: "c"
225+
});
226+
227+
const restored = restoreMap(andCondition, metadata);
228+
229+
expect(restored).toEqual({
230+
a: tagCondition1,
231+
b: undefined,
232+
c: tagCondition2,
233+
d: undefined
234+
});
235+
});
236+
237+
it("handles edge case with undefined condition and non-zero length metadata", () => {
238+
const metadata = JSON.stringify({
239+
length: 1,
240+
keys: ["test"],
241+
0: "test"
242+
});
243+
244+
const restored = restoreMap(undefined, metadata);
245+
246+
expect(restored).toEqual({
247+
test: undefined
248+
});
249+
});
250+
251+
it("handles edge case with non-and condition but multiple length metadata", () => {
252+
const tagCondition = tag("single");
253+
const metadata = JSON.stringify({
254+
length: 2,
255+
keys: ["a", "b"],
256+
0: "a",
257+
1: "b"
258+
});
259+
260+
const restored = restoreMap(tagCondition, metadata);
261+
262+
expect(restored).toEqual({
263+
a: undefined,
264+
b: undefined
265+
});
266+
});
267+
});
138268
});

packages/shared/filter-commons/src/condition-utils.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,28 +171,36 @@ export function reduceMap(input: Record<string, FilterCondition | undefined>): [
171171
return [and(...conditions), metaJson];
172172
}
173173
}
174-
/*
174+
175175
export function restoreMap(
176176
cond: FilterCondition | undefined,
177177
metaJson: string
178178
): Record<string, FilterCondition | undefined> {
179179
const meta = JSON.parse(metaJson) as ReduceMapMeta;
180180
const result: Record<string, FilterCondition | undefined> = {};
181-
const keys = Object.keys(meta).filter(key => key !== "length");
182-
for (const key of keys) {
181+
182+
for (const key of meta.keys) {
183183
result[key] = undefined;
184184
}
185185

186186
if (meta.length === 0) {
187187
return result;
188188
}
189189

190-
if (meta.length === 1) {
191-
const key = keys[0];
192-
result[key] = cond;
190+
if (meta.length === 1 && meta[0]) {
191+
result[meta[0]] = cond;
192+
return result;
193+
}
194+
195+
if (cond && isAnd(cond)) {
196+
cond.args.forEach((c, i) => {
197+
result[meta[i]] = c;
198+
});
193199
return result;
194200
}
195-
} */
201+
202+
return result;
203+
}
196204

197205
export function fromCompactArray(cond: FilterCondition): Array<FilterCondition | undefined> {
198206
const tag = isAnd(cond) ? cond.args[0] : cond;

0 commit comments

Comments
 (0)