Skip to content

Commit 18d18a7

Browse files
suggested changes
1 parent c6bbafb commit 18d18a7

File tree

4 files changed

+36
-34
lines changed

4 files changed

+36
-34
lines changed

index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ module.exports.Decimal128 = mongoose.Decimal128;
4646
module.exports.Mixed = mongoose.Mixed;
4747
module.exports.Date = mongoose.Date;
4848
module.exports.Number = mongoose.Number;
49-
module.exports.Int32 = mongoose.Int32;
5049
module.exports.Error = mongoose.Error;
5150
module.exports.MongooseError = mongoose.MongooseError;
5251
module.exports.now = mongoose.now;

lib/cast/int32.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,7 @@ module.exports = function castInt32(val) {
2121
return null;
2222
}
2323

24-
let coercedVal;
25-
if (val instanceof BSON.Int32 || val instanceof BSON.Double) {
26-
coercedVal = val.value;
27-
} else if (val instanceof BSON.Long) {
28-
coercedVal = val.toNumber();
29-
} else {
30-
coercedVal = Number(val);
31-
}
24+
const coercedVal = val instanceof BSON.Long ? val.toNumber() : Number(val);
3225

3326
const INT32_MAX = 0x7FFFFFFF;
3427
const INT32_MIN = -0x80000000;

lib/mongoose.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,21 +1140,6 @@ Mongoose.prototype.syncIndexes = function(options) {
11401140
Mongoose.prototype.Decimal128 = SchemaTypes.Decimal128;
11411141

11421142

1143-
/**
1144-
* The Mongoose Int32 [SchemaType](https://mongoosejs.com/docs/schematypes.html). Used for
1145-
* declaring paths in your schema that should be 32-bit integers.
1146-
* Do not use this to create a new Int32 instance, use `mongoose.Types.Int32`
1147-
* instead.
1148-
*
1149-
* #### Example:
1150-
*
1151-
* const vehicleSchema = new Schema({ numTires: mongoose.Int32 });
1152-
*
1153-
* @property Int32
1154-
* @api public
1155-
*/
1156-
Mongoose.prototype.Int32 = SchemaTypes.Int32;
1157-
11581143
/**
11591144
* The Mongoose Mixed [SchemaType](https://mongoosejs.com/docs/schematypes.html). Used for
11601145
* declaring paths in your schema that Mongoose's change tracking, casting,

test/int32.test.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
const assert = require('assert');
44
const start = require('./common');
55
const BSON = require('bson');
6+
const sinon = require('sinon');
67

78
const mongoose = start.mongoose;
89
const Schema = mongoose.Schema;
910

1011
const INT32_MAX = 0x7FFFFFFF;
1112
const INT32_MIN = -0x80000000;
1213

13-
describe('Int32', function() {
14+
describe.only('Int32', function() {
1415
beforeEach(() => mongoose.deleteModel(/Test/));
1516

1617
it('is a valid schema type', function() {
@@ -27,7 +28,7 @@ describe('Int32', function() {
2728
});
2829

2930
describe('supports the required property', function() {
30-
it('when vaglue is null', async function() {
31+
it('when value is null', async function() {
3132
const schema = new Schema({
3233
int32: {
3334
type: Schema.Types.Int32,
@@ -141,7 +142,7 @@ describe('Int32', function() {
141142
const Test = mongoose.model('Test', schema);
142143

143144
const doc = new Test({
144-
myInt: -42
145+
myInt: '-42'
145146
});
146147
assert.strictEqual(doc.myInt, -42);
147148
});
@@ -182,16 +183,40 @@ describe('Int32', function() {
182183
assert.strictEqual(doc.myInt, -997);
183184
});
184185

185-
it('casts from BSON.Long provided its value is within bounds of Int32', function() {
186-
const schema = new Schema({
187-
myInt: Schema.Types.Int32
186+
describe('long', function() {
187+
after(function() {
188+
sinon.restore();
188189
});
189-
const Test = mongoose.model('Test', schema);
190190

191-
const doc = new Test({
192-
myInt: BSON.Long.fromNumber(-997)
191+
it('casts from BSON.Long provided its value is within bounds of Int32', function() {
192+
const schema = new Schema({
193+
myInt: Schema.Types.Int32
194+
});
195+
const Test = mongoose.model('Test', schema);
196+
197+
const doc = new Test({
198+
myInt: BSON.Long.fromNumber(-997)
199+
});
200+
assert.strictEqual(doc.myInt, -997);
201+
});
202+
203+
it('calls Long.toNumber when casting long', function() {
204+
// this is a perf optimization, since long.toNumber() is faster than Number(long)
205+
const schema = new Schema({
206+
myInt: Schema.Types.Int32
207+
});
208+
const Test = mongoose.model('Test', schema);
209+
210+
sinon.stub(BSON.Long.prototype, 'toNumber').callsFake(function() {
211+
return 2;
212+
});
213+
214+
const doc = new Test({
215+
myInt: BSON.Long.fromNumber(-997)
216+
});
217+
218+
assert.strictEqual(doc.myInt, 2);
193219
});
194-
assert.strictEqual(doc.myInt, -997);
195220
});
196221

197222
it('casts from BSON.Double provided its value is an integer', function() {

0 commit comments

Comments
 (0)