From 5ddbf63040ea3855769f9da62111b95734621719 Mon Sep 17 00:00:00 2001 From: Nik Jmaeff Date: Sat, 24 Dec 2022 21:03:48 -0800 Subject: [PATCH] Add default value to Decimal constructor - Adding a default value allows Decimal to be constructed without arguments and not throw an error. Javascript primitives like String and Number can be constructed without an argument and this property is useful when using the type with a library like class-transformer. - Assign a default value of zero as the decimal with no constructor argument. --- decimal.d.ts | 4 ++-- decimal.global.d.ts | 4 ++-- decimal.js | 1 + decimal.mjs | 1 + test/modules/Decimal.js | 1 - test/modules/intPow.js | 1 - 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/decimal.d.ts b/decimal.d.ts index 0fbc7b2..e402317 100644 --- a/decimal.d.ts +++ b/decimal.d.ts @@ -57,13 +57,13 @@ export declare class Decimal { readonly e: number; readonly s: number; - constructor(n: Decimal.Value); + constructor(n?: Decimal.Value); absoluteValue(): Decimal; abs(): Decimal; ceil(): Decimal; - + clampedTo(min: Decimal.Value, max: Decimal.Value): Decimal; clamp(min: Decimal.Value, max: Decimal.Value): Decimal; diff --git a/decimal.global.d.ts b/decimal.global.d.ts index 6b28256..49d0f50 100644 --- a/decimal.global.d.ts +++ b/decimal.global.d.ts @@ -78,13 +78,13 @@ export declare class Decimal { readonly e: number; readonly s: number; - constructor(n: DecimalValue); + constructor(n?: DecimalValue); absoluteValue(): Decimal; abs(): Decimal; ceil(): Decimal; - + clampedTo(min: Decimal.Value, max: Decimal.Value): Decimal; clamp(min: Decimal.Value, max: Decimal.Value): Decimal; diff --git a/decimal.js b/decimal.js index aa6526a..f4eb329 100644 --- a/decimal.js +++ b/decimal.js @@ -4283,6 +4283,7 @@ * */ function Decimal(v) { + if (typeof v === 'undefined') v = 0 var e, i, t, x = this; diff --git a/decimal.mjs b/decimal.mjs index 909300c..9e8ec16 100644 --- a/decimal.mjs +++ b/decimal.mjs @@ -4279,6 +4279,7 @@ function clone(obj) { * */ function Decimal(v) { + if (typeof v === 'undefined') v = 0 var e, i, t, x = this; diff --git a/test/modules/Decimal.js b/test/modules/Decimal.js index 24c5897..0865574 100644 --- a/test/modules/Decimal.js +++ b/test/modules/Decimal.js @@ -278,7 +278,6 @@ T('Decimal', function () { t('123.456789', '123.456789'); t('123.456789', '+123.456789'); - tx(function () {new Decimal(void 0)}, "void 0"); tx(function () {new Decimal('undefined')}, "'undefined'"); tx(function () {new Decimal(null)}, "null"); tx(function () {new Decimal('null')}, "'null'"); diff --git a/test/modules/intPow.js b/test/modules/intPow.js index b772fea..57f939b 100644 --- a/test/modules/intPow.js +++ b/test/modules/intPow.js @@ -540,7 +540,6 @@ T('integer pow', function () { t('1.494189395849269188211255039709933309086424259778445906419464942576097148044216376789735318980392750336285644804638743600807550074206128272345650029255016954321611264002141919840462369550905098763723254901675135787504979910497931539962354019230845564318816091666473025536e+126', '-32698949771.110178432792', 12); t('1.1504940871276742926708823617505372960241390892442547940194260102743306128298973371802547471453755938653944600792141533514422490556963535378521856840746722206160260148176604222079226186281680715577736316488196108040509176925372372483300649927835887344415603493563915019264675154039059770309142781930141352329958156926976e+15', '-8.7358521345995835476', 16); - tx(function () {new Decimal('12.345').pow(void 0)}, ".pow(void 0)"); tx(function () {new Decimal('12.345').pow(null)}, ".pow(null)"); tx(function () {new Decimal('12.345').pow(true)}, ".pow(true)"); tx(function () {new Decimal('12.345').pow(false)}, ".pow(false)");