Skip to content

Commit

Permalink
fix: support Number object in Int32 and Double constructors
Browse files Browse the repository at this point in the history
Numbers objects deserve the same love and support as their primitive
counterparts. So handle them appropriately without introducing
regressions into behaviors of other types that may be passed in as
values

Fixes NODE-2384
  • Loading branch information
stieg authored and mbroadst committed Jan 9, 2020
1 parent ff30b58 commit fe3f0dc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/double.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ class Double {
/**
* Create a Double type
*
* @param {number} value the number we want to represent as a double.
* @param {number|Number} value the number we want to represent as a double.
* @return {Double}
*/
constructor(value) {
if (value instanceof Number) {
value = value.valueOf();
}

this.value = value;
}

Expand Down
6 changes: 5 additions & 1 deletion lib/int_32.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ class Int32 {
/**
* Create an Int32 type
*
* @param {number} value the number we want to represent as an int32.
* @param {number|Number} value the number we want to represent as an int32.
* @return {Int32}
*/
constructor(value) {
if (value instanceof Number) {
value = value.valueOf();
}

this.value = value;
}

Expand Down
21 changes: 21 additions & 0 deletions test/node/double_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const BSON = require('../../lib/bson');
const Double = BSON.Double;
const expect = require('chai').expect;

describe('Double', function() {
describe('Constructor', function() {
var value = 42.3456;

it('Primitive number', function(done) {
expect(new Double(value).valueOf()).to.equal(value);
done();
});

it('Number object', function(done) {
expect(new Double(new Number(value)).valueOf()).to.equal(value);
done();
});
});
});
21 changes: 21 additions & 0 deletions test/node/int_32_tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const BSON = require('../../lib/bson');
const Int32 = BSON.Int32;
const expect = require('chai').expect;

describe('Int32', function() {
describe('Constructor', function() {
var value = 42;

it('Primitive number', function(done) {
expect(new Int32(value).valueOf()).to.equal(value);
done();
});

it('Number object', function(done) {
expect(new Int32(new Number(value)).valueOf()).to.equal(value);
done();
});
});
});

0 comments on commit fe3f0dc

Please sign in to comment.