Skip to content

Commit

Permalink
filter doesn't throw an error when all data is filtered out
Browse files Browse the repository at this point in the history
  • Loading branch information
nshiab committed Feb 27, 2025
1 parent ff1422a commit e13647b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
48 changes: 25 additions & 23 deletions src/helpers/convertForJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,32 @@ export default function convertForJS(rows: {
}[], types: {
[key: string]: string;
}) {
const firstObjectKeys = Object.keys(rows[0]);
for (const key of Object.keys(types)) {
if (!firstObjectKeys.includes(key)) {
continue;
}
if (types[key] === "DATE") {
for (const row of rows) {
row[key] = row[key] === null
? null
: new Date(`${row[key]}T00:00:00.000Z`);
}
} else if (types[key] === "TIMESTAMP") {
for (const row of rows) {
row[key] = row[key] === null
? null
: new Date((row[key] as string).replace(" ", "T") + "Z");
}
} else if (types[key] === "BIGINT" || types[key] === "HUGEINT") {
for (const row of rows) {
row[key] = row[key] === null ? null : Number(row[key]);
if (rows[0] !== undefined) {
const firstObjectKeys = Object.keys(rows[0]);
for (const key of Object.keys(types)) {
if (!firstObjectKeys.includes(key)) {
continue;
}
} else if (types[key] === "GEOMETRY") {
for (const row of rows) {
row[key] = row[key] === null ? null : "<Geometry>";
if (types[key] === "DATE") {
for (const row of rows) {
row[key] = row[key] === null
? null
: new Date(`${row[key]}T00:00:00.000Z`);
}
} else if (types[key] === "TIMESTAMP") {
for (const row of rows) {
row[key] = row[key] === null
? null
: new Date((row[key] as string).replace(" ", "T") + "Z");
}
} else if (types[key] === "BIGINT" || types[key] === "HUGEINT") {
for (const row of rows) {
row[key] = row[key] === null ? null : Number(row[key]);
}
} else if (types[key] === "GEOMETRY") {
for (const row of rows) {
row[key] = row[key] === null ? null : "<Geometry>";
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions test/unit/methods/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,14 @@ Deno.test("should filter the rows based on booleans", async () => {
assertEquals(data, [{ name: "Nael", value: true }]);
await sdb.done();
});

Deno.test("should not throw an error when all data has been filtered out", async () => {
const sdb = new SimpleDB();
const table = sdb.newTable("data");
await table.loadData(["test/data/files/employees.csv"]);
await table.filter(`Name = 'Nael'`);
const data = await table.getData();

assertEquals(data, []);
await sdb.done();
});

0 comments on commit e13647b

Please sign in to comment.