Skip to content

Commit

Permalink
πŸ› Don't let falsy values passed to Decimal classes cause an error
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Feb 23, 2024
1 parent 757a643 commit 7a0cc81
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 0.9.2 (WIP)

* Clear the entire `Develry.Request.cache` as soon as a non-GET request is made
* Don't let falsy values passed to `Decimal` classes cause an error

## 0.9.1 (2024-02-19)

Expand Down
6 changes: 4 additions & 2 deletions lib/decimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function createMutableClass(ImmutableParent, MutableConstructor) {
*
* @author Jelle De Loecker <[email protected]>
* @since 0.8.14
* @version 0.8.14
* @version 0.9.2
*
* @param {string|number} value
*/
Expand All @@ -61,7 +61,9 @@ const Decimal = Fn.inherits('Develry.AbstractNumeric', function Decimal(value) {
return new Decimal(value);
}

if (typeof value != 'string') {
if (!value) {
value = '0';
} else if (typeof value != 'string') {
value = String(value);
}

Expand Down
9 changes: 9 additions & 0 deletions test/decimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ describe('Decimal', function() {
decimalEquals(Decimal('-345.43e+4'), '-3454300');
decimalEquals(Decimal('3.345E-9'), '0.000000003345');
});

it('should handle falsy values', () => {
decimalEquals(Decimal(''), '0');
decimalEquals(Decimal(0), '0');
decimalEquals(Decimal(false), '0');
decimalEquals(Decimal(), '0');
decimalEquals(Decimal(null), '0');
decimalEquals(Decimal(undefined), '0');
});
});

describe('#toDry()', () => {
Expand Down

0 comments on commit 7a0cc81

Please sign in to comment.