Skip to content

Commit 0927040

Browse files
committed
🐛 fixed bug in InvertedIndexMap
1 parent fe9f43b commit 0927040

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

src/lang.spec.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,4 +1190,84 @@ describe("Lang.InvertedIndexMap", () => {
11901190
expect(idx.count({ type: "A" })).toBe(1);
11911191
expect(idx.count({ type: "B" })).toBe(3);
11921192
});
1193+
1194+
test("query with undefined field values", () => {
1195+
interface Person {
1196+
id: string;
1197+
name: string;
1198+
age: number;
1199+
city: string;
1200+
}
1201+
1202+
const idx = new Lang.InvertedIndexMap<Person>(
1203+
(r) => r.id,
1204+
["name", "age", "city"],
1205+
);
1206+
1207+
const people = [
1208+
{ id: "p1", name: "Alice", age: 30, city: "New York" },
1209+
{ id: "p2", name: "Bob", age: 25, city: "Boston" },
1210+
{ id: "p3", name: "Charlie", age: 30, city: "Chicago" },
1211+
{ id: "p4", name: "David", age: 35, city: "New York" },
1212+
];
1213+
1214+
people.forEach((p) => idx.add(p));
1215+
1216+
// Undefined field values should be ignored
1217+
expect(idx.query({ name: "Alice", age: undefined })).toEqual([people[0]]);
1218+
1219+
expect(idx.query({ name: undefined, age: 30 })).toEqual([
1220+
people[0],
1221+
people[2],
1222+
]);
1223+
1224+
// Multiple undefined fields
1225+
expect(
1226+
idx.query({
1227+
name: undefined,
1228+
age: undefined,
1229+
city: "New York",
1230+
}),
1231+
).toEqual([people[0], people[3]]);
1232+
1233+
// All fields undefined should return all records
1234+
expect(
1235+
idx
1236+
.query({
1237+
name: undefined,
1238+
age: undefined,
1239+
city: undefined,
1240+
})
1241+
.sort((a, b) => a.id.localeCompare(b.id)),
1242+
).toEqual(people);
1243+
1244+
// Mix of undefined and non-existent fields
1245+
expect(
1246+
idx.query({
1247+
name: undefined,
1248+
nonExistent: "value" as any,
1249+
city: "Boston",
1250+
} as any),
1251+
).toEqual([people[1]]);
1252+
1253+
// Count should work same with undefined fields
1254+
expect(
1255+
idx.count({
1256+
name: undefined,
1257+
age: 30,
1258+
city: undefined,
1259+
}),
1260+
).toBe(2);
1261+
1262+
// Filter function should still work with undefined query fields
1263+
expect(
1264+
idx.query(
1265+
{
1266+
name: undefined,
1267+
age: undefined,
1268+
},
1269+
(p) => p.city === "New York",
1270+
),
1271+
).toEqual([people[0], people[3]]);
1272+
});
11931273
});

src/lang.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,10 @@ export class InvertedIndexMap<R extends Record<keyof R, unknown>> {
813813
}
814814
}
815815
}
816-
return sortedArrToIndexSet(intersected ?? []);
816+
if (intersected === null) {
817+
return LenIndexSet(this.data.length);
818+
}
819+
return sortedArrToIndexSet(intersected);
817820
}
818821

819822
query(q: Partial<R>, filter?: (r: R) => boolean): R[] {

0 commit comments

Comments
 (0)