From 03a4d8675b47764a847edf765dad10c70d97e765 Mon Sep 17 00:00:00 2001 From: Hunter Wilhelm <71348224+hunterwilhelm@users.noreply.github.com> Date: Sat, 20 Dec 2025 17:18:17 -0700 Subject: [PATCH] Fixes FIX-230: feature: enable users to pass arrays to Decimal.sum --- decimal.d.ts | 1 + decimal.global.d.ts | 1 + decimal.js | 3 ++- decimal.mjs | 3 ++- doc/API.html | 15 +++++++++------ test/modules/sum.js | 8 ++++++++ 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/decimal.d.ts b/decimal.d.ts index 526a896..982a1ee 100644 --- a/decimal.d.ts +++ b/decimal.d.ts @@ -270,6 +270,7 @@ export declare class Decimal { static sqrt(n: Decimal.Value): Decimal; static sub(x: Decimal.Value, y: Decimal.Value): Decimal; static sum(...n: Decimal.Value[]): Decimal; + static sum(n: Decimal.Value[]): Decimal; static tan(n: Decimal.Value): Decimal; static tanh(n: Decimal.Value): Decimal; static trunc(n: Decimal.Value): Decimal; diff --git a/decimal.global.d.ts b/decimal.global.d.ts index a3162a3..7a8f13a 100644 --- a/decimal.global.d.ts +++ b/decimal.global.d.ts @@ -291,6 +291,7 @@ export declare class Decimal { static sqrt(n: DecimalValue): Decimal; static sub(x: DecimalValue, y: DecimalValue): Decimal; static sum(...n: Decimal.Value[]): Decimal; + static sum(n: Decimal.Value[]): Decimal; static tan(n: DecimalValue): Decimal; static tanh(n: DecimalValue): Decimal; static trunc(n: DecimalValue): Decimal; diff --git a/decimal.js b/decimal.js index f7ecafb..b930140 100644 --- a/decimal.js +++ b/decimal.js @@ -4856,11 +4856,12 @@ * Only the result is rounded, not the intermediate calculations. * * arguments {number|string|bigint|Decimal} + * arguments {(number|string|bigint|Decimal)[]} * */ function sum() { var i = 0, - args = arguments, + args = arguments.length === 1 && Array.isArray(arguments[0]) ? arguments[0] : arguments, x = new this(args[i]); external = false; diff --git a/decimal.mjs b/decimal.mjs index 18dc640..2f93c2f 100644 --- a/decimal.mjs +++ b/decimal.mjs @@ -4851,11 +4851,12 @@ function sub(x, y) { * Only the result is rounded, not the intermediate calculations. * * arguments {number|string|bigint|Decimal} + * arguments {(number|string|bigint|Decimal)[]} * */ function sum() { var i = 0, - args = arguments, + args = arguments.length === 1 && Array.isArray(arguments[0]) ? arguments[0] : arguments, x = new this(args[i]); external = false; diff --git a/doc/API.html b/doc/API.html index e11f004..f3ec562 100644 --- a/doc/API.html +++ b/doc/API.html @@ -861,22 +861,25 @@ a.equals(b) // true -
sum.sum(x [, y, ...]) ⇒ Decimal
+
sum.sum(x [, y, ...]) ⇒ Decimal
+ .sum([x, y, ...]) ⇒ Decimal

x: number|string|bigInt|Decimal
- y: number|string|bigInt|Decimal + y: number|string|bigInt|Decimal
+ [x, y, ...]: Array<number|string|bigInt|Decimal>

- Returns a new Decimal whose value is the sum of the arguments, - rounded to precision significant digits using - rounding mode rounding.
+ Returns a new Decimal whose value is the sum of the arguments (either passed as + variadic arguments or as an array), rounded to precision + significant digits using rounding mode rounding.
Only the result is rounded, not the intermediate summations.

 x = 5
 y = '16'
 z = new Decimal(-11)
-Decimal.sum(x, y, z)           // '10'
+Decimal.sum(x, y, z) // '10' +Decimal.sum([x, y, z]) // '10' diff --git a/test/modules/sum.js b/test/modules/sum.js index 6dda6f0..9efc1c7 100644 --- a/test/modules/sum.js +++ b/test/modules/sum.js @@ -14,6 +14,8 @@ T('sum', function () { t(1, 0, '-1'); t(0, new Decimal('-10'), 0, 0, 0, 0, 0, 10); t(11, -11); + t([11, -11]); + t([0]); t(1, '2', new Decimal(3), new Decimal('4'), -10); t(new Decimal(-10), '9', new Decimal(0.01), 0.99); @@ -24,6 +26,7 @@ T('sum', function () { t(10, 0); t(0, 0, 0, 0, 0, 0, 10); t(11, -1); + t([11, -1]); t(1, '2', new Decimal(3), new Decimal('4')); t('9', new Decimal(0.01), 0.99); @@ -61,4 +64,9 @@ T('sum', function () { t(0, new Decimal('-Infinity'), '9', new Decimal(0), 11); t(0, '9', new Decimal(0), 11, -Infinity); t(4, new Decimal(-Infinity), 0, '9', new Decimal(0), -Infinity, 2); + t([4, new Decimal(-Infinity), 0, '9', new Decimal(0), -Infinity, 2]); + + expected = new Decimal(100e3); + + t(Array.from({ length: 100e3 }).fill(1)); }); \ No newline at end of file