From 04cc718fe54e8dde83656688b427fa2a0831464f Mon Sep 17 00:00:00 2001 From: Samori Gorse Date: Fri, 3 Jan 2025 22:43:37 +0100 Subject: [PATCH] feat: Refactoring --- lib/decimal.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/decimal.js b/lib/decimal.js index 08beaa1..cdb4c25 100755 --- a/lib/decimal.js +++ b/lib/decimal.js @@ -49,19 +49,23 @@ var Decimal = function (num) { // Helper to normalize zero values this.normalizeZero = function () { - if ( - this.internal === '0' || - this.internal === '-0' || - this.internal === '00' || - this.internal === '000' - ) { + if (this.internal === '-0') { return '0'; } + + if (this.internal.split('').every((char) => char === '0')) { + return '0'; + } + return this.internal; }; + this.zeroOperands = function (a, b) { + return (a === '0' || a === '-0') && (b === '0' || b === '-0'); + }; + this.add = function (target) { - if ((this.internal === '0' || this.internal === '-0') && (target === '0' || target === '-0')) { + if (this.zeroOperands(this.internal, target)) { return Decimal('0'); } @@ -82,14 +86,14 @@ var Decimal = function (num) { }; this.sub = function (target) { - if ((this.internal === '0' || this.internal === '-0') && (target === '0' || target === '-0')) { + if (this.zeroOperands(this.internal, target)) { return Decimal('0'); } return Decimal(this.add(target * -1)); }; this.mul = function (target) { - if ((this.internal === '0' || this.internal === '-0') && (target === '0' || target === '-0')) { + if (this.zeroOperands(this.internal, target)) { return Decimal('0'); }