Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/methods/roundQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ export default function roundQuery(
columns: string[],
options: { method?: "round" | "ceiling" | "floor"; decimals?: number }
) {
let query = `UPDATE ${table}`
let query = `UPDATE ${table} SET`
const method = options.method?.toUpperCase() ?? "ROUND"
const decimals = options.decimals ?? 0

if (method === "ROUND") {
for (const column of columns) {
query += `\nSET "${column}" = ${method}("${column}", ${decimals})`
query += `\n"${column}" = ${method}("${column}", ${decimals}),`
}
} else {
for (const column of columns) {
query += `\nSET "${column}" = ${method}("${column}")`
query += `\n"${column}" = ${method}("${column}"),`
}
}

return query
return query.slice(0, query.length - 1)
}
10 changes: 5 additions & 5 deletions test/data/files/dataManyDecimals.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
key1
1.04355
3.243943535
8.1
10
key1,key2
1.04355,2.945
3.243943535,4.9898
8.1,34.5
10,100
23 changes: 20 additions & 3 deletions test/unit/methods/round.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe("round", () => {
await simpleNodeDB.loadData("dataInteger", [
"test/data/files/dataManyDecimals.csv",
])
await simpleNodeDB.selectColumns("dataInteger", ["key1"])

await simpleNodeDB.round("dataInteger", ["key1"])

Expand All @@ -30,7 +31,7 @@ describe("round", () => {
await simpleNodeDB.loadData("dataDecimals", [
"test/data/files/dataManyDecimals.csv",
])

await simpleNodeDB.selectColumns("dataDecimals", ["key1"])
await simpleNodeDB.round("dataDecimals", ["key1"], {
decimals: 3,
})
Expand All @@ -47,7 +48,7 @@ describe("round", () => {
await simpleNodeDB.loadData("dataFloor", [
"test/data/files/dataManyDecimals.csv",
])

await simpleNodeDB.selectColumns("dataFloor", ["key1"])
await simpleNodeDB.round("dataFloor", ["key1"], {
method: "floor",
})
Expand All @@ -65,7 +66,7 @@ describe("round", () => {
await simpleNodeDB.loadData("dataCeil", [
"test/data/files/dataManyDecimals.csv",
])

await simpleNodeDB.selectColumns("dataCeil", ["key1"])
await simpleNodeDB.round("dataCeil", ["key1"], {
method: "ceiling",
})
Expand All @@ -78,4 +79,20 @@ describe("round", () => {
{ key1: 10 },
])
})
it("should round multiple columns", async () => {
await simpleNodeDB.loadData("dataMultipleColumns", [
"test/data/files/dataManyDecimals.csv",
])
await simpleNodeDB.round("dataMultipleColumns", ["key1", "key2"], {
decimals: 2,
})
const data = await simpleNodeDB.getData("dataMultipleColumns")

assert.deepStrictEqual(data, [
{ key1: 1.04, key2: 2.95 },
{ key1: 3.24, key2: 4.99 },
{ key1: 8.1, key2: 34.5 },
{ key1: 10, key2: 100 },
])
})
})