Skip to content

Commit

Permalink
Only change BigNumber.DEBUG during validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamefrede committed Jan 10, 2024
1 parent b37a5d9 commit 3d37495
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
8 changes: 6 additions & 2 deletions src/asserts/big-number-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ module.exports = function bigNumberAssert({ validateSignificantDigits = true } =
throw new Error('BigNumber is not installed');
}

BigNumber.DEBUG = !!validateSignificantDigits;

/**
* Class name.
*/
Expand All @@ -39,7 +37,11 @@ module.exports = function bigNumberAssert({ validateSignificantDigits = true } =
*/

this.validate = value => {
const bigNumberDebug = BigNumber.DEBUG;

try {
BigNumber.DEBUG = !!validateSignificantDigits;

const number = new BigNumber(value);

if (Number.isNaN(number.toNumber())) {
Expand All @@ -51,6 +53,8 @@ module.exports = function bigNumberAssert({ validateSignificantDigits = true } =
}

throw new Violation(this, value);
} finally {
BigNumber.DEBUG = bigNumberDebug;
}

return true;
Expand Down
8 changes: 6 additions & 2 deletions src/asserts/big-number-equal-to-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ module.exports = function bigNumberEqualToAssert(value, { validateSignificantDig
throw new Error('BigNumber is not installed');
}

BigNumber.DEBUG = !!validateSignificantDigits;

/**
* Extend `Assert` with `BigNumberAssert`.
*/
Expand All @@ -55,7 +53,11 @@ module.exports = function bigNumberEqualToAssert(value, { validateSignificantDig
*/

this.validate = value => {
const bigNumberDebug = BigNumber.DEBUG;

try {
BigNumber.DEBUG = !!validateSignificantDigits;

Assert.bigNumber({ validateSignificantDigits }).validate(value);

const number = new BigNumber(value);
Expand All @@ -72,6 +74,8 @@ module.exports = function bigNumberEqualToAssert(value, { validateSignificantDig
}

throw new Violation(this, value, context);
} finally {
BigNumber.DEBUG = bigNumberDebug;
}

return true;
Expand Down
8 changes: 6 additions & 2 deletions src/asserts/big-number-greater-than-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ module.exports = function bigNumberGreaterThanAssert(threshold, { validateSignif
throw new Error('BigNumber is not installed');
}

BigNumber.DEBUG = !!validateSignificantDigits;

/**
* Extend `Assert` with `BigNumberAssert`.
*/
Expand All @@ -55,7 +53,11 @@ module.exports = function bigNumberGreaterThanAssert(threshold, { validateSignif
*/

this.validate = value => {
const bigNumberDebug = BigNumber.DEBUG;

try {
BigNumber.DEBUG = !!validateSignificantDigits;

Assert.bigNumber({ validateSignificantDigits }).validate(value);

const number = new BigNumber(value);
Expand All @@ -72,6 +74,8 @@ module.exports = function bigNumberGreaterThanAssert(threshold, { validateSignif
}

throw new Violation(this, value, context);
} finally {
BigNumber.DEBUG = bigNumberDebug;
}

return true;
Expand Down
8 changes: 6 additions & 2 deletions src/asserts/big-number-greater-than-or-equal-to-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ module.exports = function bigNumberGreaterThanOrEqualToAssert(threshold, { valid
throw new Error('BigNumber is not installed');
}

BigNumber.DEBUG = !!validateSignificantDigits;

/**
* Extend `Assert` with `BigNumberAssert`.
*/
Expand All @@ -54,7 +52,11 @@ module.exports = function bigNumberGreaterThanOrEqualToAssert(threshold, { valid
*/

this.validate = value => {
const bigNumberDebug = BigNumber.DEBUG;

try {
BigNumber.DEBUG = !!validateSignificantDigits;

Assert.bigNumber({ validateSignificantDigits }).validate(value);

const number = new BigNumber(value);
Expand All @@ -71,6 +73,8 @@ module.exports = function bigNumberGreaterThanOrEqualToAssert(threshold, { valid
}

throw new Violation(this, value, context);
} finally {
BigNumber.DEBUG = bigNumberDebug;
}

return true;
Expand Down
8 changes: 6 additions & 2 deletions src/asserts/big-number-less-than-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ module.exports = function bigNumberLessThan(threshold, { validateSignificantDigi
throw new Error('BigNumber is not installed');
}

BigNumber.DEBUG = !!validateSignificantDigits;

/**
* Extend `Assert` with `BigNumberAssert`.
*/
Expand All @@ -55,7 +53,11 @@ module.exports = function bigNumberLessThan(threshold, { validateSignificantDigi
*/

this.validate = value => {
const bigNumberDebug = BigNumber.DEBUG;

try {
BigNumber.DEBUG = !!validateSignificantDigits;

Assert.bigNumber({ validateSignificantDigits }).validate(value);

const number = new BigNumber(value);
Expand All @@ -72,6 +74,8 @@ module.exports = function bigNumberLessThan(threshold, { validateSignificantDigi
}

throw new Violation(this, value, context);
} finally {
BigNumber.DEBUG = bigNumberDebug;
}

return true;
Expand Down
8 changes: 6 additions & 2 deletions src/asserts/big-number-less-than-or-equal-to-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ module.exports = function bigNumberLessThanOrEqualToAssert(threshold, { validate
throw new Error('BigNumber is not installed');
}

BigNumber.DEBUG = !!validateSignificantDigits;

/**
* Extend `Assert` with `BigNumberAssert`.
*/
Expand All @@ -55,7 +53,11 @@ module.exports = function bigNumberLessThanOrEqualToAssert(threshold, { validate
*/

this.validate = value => {
const bigNumberDebug = BigNumber.DEBUG;

try {
BigNumber.DEBUG = !!validateSignificantDigits;

Assert.bigNumber({ validateSignificantDigits }).validate(value);

const number = new BigNumber(value);
Expand All @@ -72,6 +74,8 @@ module.exports = function bigNumberLessThanOrEqualToAssert(threshold, { validate
}

throw new Violation(this, value, context);
} finally {
BigNumber.DEBUG = bigNumberDebug;
}

return true;
Expand Down

0 comments on commit 3d37495

Please sign in to comment.