Skip to content

Commit

Permalink
fix: places should be optional for $trunc and $round (#368)
Browse files Browse the repository at this point in the history
* fix: updated truncate function to default to 0 places

Updated truncate function to default to 0 places as per mongo documentation.

https://www.mongodb.com/docs/manual/reference/operator/aggregation/trunc/

* test: added optional places unit tests for $round and $trunc
  • Loading branch information
praoshealth authored Sep 25, 2023
1 parent 265ab27 commit 14a73ba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/operators/expression/arithmetic/_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/
export function truncate(
num: number,
places: number,
roundOff: boolean
places: number = 0,
roundOff: boolean = false
): number {
const sign = Math.abs(num) === num ? 1 : -1;
num = Math.abs(num);
Expand Down
13 changes: 13 additions & 0 deletions test/operators/expression/arithmetic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ support.runTest("operators/expression/arithmetic", {
[[34.32, -1], 30],
[[-45.39, -1], -50],
// rounded to the whole integer
[[19.25], 19],
[[28.73], 29],
[[34.32], 34],
[[-45.39], -45],
[[10.4], 10],
[[10.6], 11],
[[10.7], 11],
[[11.4], 11],
[[11.9], 12],
[[19.25, 0], 19],
[[28.73, 0], 29],
[[34.32, 0], 34],
Expand Down Expand Up @@ -137,6 +146,10 @@ support.runTest("operators/expression/arithmetic", {
[[34.32, -1], 30],
[[-45.39, -1], -40],
// truncate to the whole integer
[[19.25], 19],
[[28.73], 28],
[[34.32], 34],
[[-45.39], -45],
[[19.25, 0], 19],
[[28.73, 0], 28],
[[34.32, 0], 34],
Expand Down

0 comments on commit 14a73ba

Please sign in to comment.