From 14a73ba32c2b34a90d53226fd859070f6fae911b Mon Sep 17 00:00:00 2001 From: Greg <132590402+praoshealth@users.noreply.github.com> Date: Mon, 25 Sep 2023 13:58:57 -0500 Subject: [PATCH] fix: places should be optional for $trunc and $round (#368) * 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 --- src/operators/expression/arithmetic/_internal.ts | 4 ++-- test/operators/expression/arithmetic.test.ts | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/operators/expression/arithmetic/_internal.ts b/src/operators/expression/arithmetic/_internal.ts index 6d8768b2..50733c9b 100644 --- a/src/operators/expression/arithmetic/_internal.ts +++ b/src/operators/expression/arithmetic/_internal.ts @@ -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); diff --git a/test/operators/expression/arithmetic.test.ts b/test/operators/expression/arithmetic.test.ts index bfe5ee80..7e9096d4 100644 --- a/test/operators/expression/arithmetic.test.ts +++ b/test/operators/expression/arithmetic.test.ts @@ -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], @@ -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],