Skip to content

Commit b3c8955

Browse files
requested changes
1 parent dbb68dc commit b3c8955

File tree

5 files changed

+48
-53
lines changed

5 files changed

+48
-53
lines changed

lib/cast/double.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,17 @@ const BSON = require('bson');
1414
*/
1515

1616
module.exports = function castDouble(val) {
17-
if (val == null) {
18-
return val;
19-
}
20-
if (val === '') {
17+
if (val == null || val === '') {
2118
return null;
2219
}
2320

2421
let coercedVal;
25-
if (val instanceof BSON.Int32 || val instanceof BSON.Double) {
26-
coercedVal = val.value;
27-
} else if (val instanceof BSON.Long) {
22+
if (val instanceof BSON.Long) {
2823
coercedVal = val.toNumber();
2924
} else if (typeof val === 'string') {
3025
try {
3126
coercedVal = BSON.Double.fromString(val);
27+
return coercedVal;
3228
} catch {
3329
assert.ok(false);
3430
}
@@ -38,15 +34,16 @@ module.exports = function castDouble(val) {
3834
if (typeof tempVal === 'string') {
3935
try {
4036
coercedVal = BSON.Double.fromString(val);
37+
return coercedVal;
4138
} catch {
4239
assert.ok(false);
4340
}
4441
} else {
4542
coercedVal = Number(tempVal);
4643
}
47-
} {
44+
} else {
4845
coercedVal = Number(val);
4946
}
5047

51-
return coercedVal;
48+
return new BSON.Double(coercedVal);
5249
};

lib/helpers/clone.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const isObject = require('./isObject');
1111
const isPOJO = require('./isPOJO');
1212
const symbols = require('./symbols');
1313
const trustedSymbol = require('./query/trusted').trustedSymbol;
14+
const BSON = require('bson');
1415

1516
/**
1617
* Object clone with Mongoose natives support.
@@ -30,6 +31,10 @@ function clone(obj, options, isArrayChild) {
3031
if (obj == null) {
3132
return obj;
3233
}
34+
35+
if (obj._bsontype === 'Double') {
36+
return new BSON.Double(obj.value);
37+
}
3338
if (typeof obj === 'number' || typeof obj === 'string' || typeof obj === 'boolean' || typeof obj === 'bigint') {
3439
return obj;
3540
}

lib/mongoose.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,19 +1167,6 @@ Mongoose.prototype.Mixed = SchemaTypes.Mixed;
11671167

11681168
Mongoose.prototype.Date = SchemaTypes.Date;
11691169

1170-
/**
1171-
* The Mongoose Double [SchemaType](https://mongoosejs.com/docs/schematypes.html). Used for
1172-
* declaring paths in your schema that should be 64-bit IEEE 754-2008 floating points.
1173-
*
1174-
* #### Example:
1175-
*
1176-
* const vehicleSchema = new Car({ gasLevel: mongoose.Double });
1177-
*
1178-
* @property Double
1179-
* @api public
1180-
*/
1181-
Mongoose.prototype.Double = SchemaTypes.Double;
1182-
11831170
/**
11841171
* The Mongoose Number [SchemaType](https://mongoosejs.com/docs/schematypes.html). Used for
11851172
* declaring paths in your schema that Mongoose should cast to numbers.

lib/schema/double.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ SchemaDouble.get = SchemaType.get;
8686

8787
SchemaDouble._defaultCaster = v => {
8888
if (v != null) {
89-
if (typeof v !== 'number') {
89+
if (v._bsontype !== 'Double') {
9090
throw new Error();
9191
}
9292
}

test/double.test.js

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ describe('Double', function() {
2020
const doc = new Test({
2121
myDouble: 13
2222
});
23-
assert.strictEqual(doc.myDouble, 13);
24-
assert.equal(typeof doc.myDouble, 'number');
23+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(13));
24+
assert.equal(typeof doc.myDouble, 'object');
2525
});
2626

2727
describe('supports the required property', function() {
28-
it('when vaglue is null', async function() {
28+
it('when value is null', async function() {
2929
const schema = new Schema({
3030
Double: {
3131
type: Schema.Types.Double,
@@ -83,7 +83,7 @@ describe('Double', function() {
8383
const doc = new Test({
8484
myDouble: undefined
8585
});
86-
assert.strictEqual(doc.myDouble, undefined);
86+
assert.deepStrictEqual(doc.myDouble, undefined);
8787
});
8888

8989
it('supports null as input', function() {
@@ -97,7 +97,7 @@ describe('Double', function() {
9797
const doc = new Test({
9898
myDouble: null
9999
});
100-
assert.strictEqual(doc.myDouble, null);
100+
assert.deepStrictEqual(doc.myDouble, null);
101101
});
102102
});
103103

@@ -113,7 +113,7 @@ describe('Double', function() {
113113
const doc = new Test({
114114
myDouble: '-42.008'
115115
});
116-
assert.strictEqual(doc.myDouble, -42.008);
116+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(-42.008));
117117
});
118118

119119
it('casts from exponential string', function() {
@@ -127,7 +127,7 @@ describe('Double', function() {
127127
const doc = new Test({
128128
myDouble: '1.22008e45'
129129
});
130-
assert.strictEqual(doc.myDouble, 1.22008e45);
130+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(1.22008e45));
131131
});
132132

133133
it('casts from infinite string', function() {
@@ -145,8 +145,8 @@ describe('Double', function() {
145145
myDouble1: 'Infinity',
146146
myDouble2: '-Infinity'
147147
});
148-
assert.strictEqual(doc.myDouble1, Infinity);
149-
assert.strictEqual(doc.myDouble2, -Infinity);
148+
assert.deepStrictEqual(doc.myDouble1, new BSON.Double(Infinity));
149+
assert.deepStrictEqual(doc.myDouble2, new BSON.Double(-Infinity));
150150
});
151151

152152
it('casts from NaN string', function() {
@@ -160,7 +160,7 @@ describe('Double', function() {
160160
const doc = new Test({
161161
myDouble: 'NaN'
162162
});
163-
assert.strictEqual(doc.myDouble, NaN);
163+
assert.deepStrictEqual(doc.myDouble, new BSON.Double('NaN'));
164164
});
165165

166166
it('casts from number', function() {
@@ -172,7 +172,7 @@ describe('Double', function() {
172172
const doc = new Test({
173173
myDouble: 988
174174
});
175-
assert.strictEqual(doc.myDouble, 988);
175+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(988));
176176
});
177177

178178
it('casts from bigint', function() {
@@ -184,7 +184,7 @@ describe('Double', function() {
184184
const doc = new Test({
185185
myDouble: -997n
186186
});
187-
assert.strictEqual(doc.myDouble, -997);
187+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(-997));
188188
});
189189

190190
it('casts from BSON.Long', function() {
@@ -196,7 +196,7 @@ describe('Double', function() {
196196
const doc = new Test({
197197
myDouble: BSON.Long.fromNumber(-997987)
198198
});
199-
assert.strictEqual(doc.myDouble, -997987);
199+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(-997987));
200200
});
201201

202202
it('casts from BSON.Double', function() {
@@ -208,7 +208,7 @@ describe('Double', function() {
208208
const doc = new Test({
209209
myDouble: new BSON.Double(-997983.33)
210210
});
211-
assert.strictEqual(doc.myDouble, -997983.33);
211+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(-997983.33));
212212
});
213213

214214
it('casts boolean true to 1', function() {
@@ -220,7 +220,7 @@ describe('Double', function() {
220220
const doc = new Test({
221221
myDouble: true
222222
});
223-
assert.strictEqual(doc.myDouble, 1);
223+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(1));
224224
});
225225

226226
it('casts boolean false to 0', function() {
@@ -232,7 +232,7 @@ describe('Double', function() {
232232
const doc = new Test({
233233
myDouble: false
234234
});
235-
assert.strictEqual(doc.myDouble, 0);
235+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(0));
236236
});
237237

238238
it('casts empty string to null', function() {
@@ -244,7 +244,7 @@ describe('Double', function() {
244244
const doc = new Test({
245245
myDouble: ''
246246
});
247-
assert.strictEqual(doc.myDouble, null);
247+
assert.deepStrictEqual(doc.myDouble, null);
248248
});
249249

250250
it('supports valueOf() function ', function() {
@@ -256,7 +256,7 @@ describe('Double', function() {
256256
const doc = new Test({
257257
myDouble: { a: 'random', b: { c: 'whatever' }, valueOf: () => 83.008 }
258258
});
259-
assert.strictEqual(doc.myDouble, 83.008);
259+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(83.008));
260260
});
261261
});
262262

@@ -276,7 +276,7 @@ describe('Double', function() {
276276
myDouble: 'helloworld'
277277
});
278278

279-
assert.strictEqual(doc.myDouble, undefined);
279+
assert.deepStrictEqual(doc.myDouble, undefined);
280280
const err = await doc.validate().catch(e => e);
281281
assert.ok(err);
282282
assert.ok(err.errors['myDouble']);
@@ -309,10 +309,10 @@ describe('Double', function() {
309309
const Test = mongoose.model('Test', schema);
310310
const doc = new Test({
311311
myDouble1: '52',
312-
myDouble2: 52
312+
myDouble2: new BSON.Double(52)
313313
});
314-
assert.strictEqual(doc.myDouble1, undefined);
315-
assert.strictEqual(doc.myDouble2, 52);
314+
assert.deepStrictEqual(doc.myDouble1, undefined);
315+
assert.deepStrictEqual(doc.myDouble2, new BSON.Double(52));
316316

317317
const err = await doc.validate().catch(e => e);
318318
assert.ok(err);
@@ -322,7 +322,7 @@ describe('Double', function() {
322322
it('supports custom cast', () => {
323323
mongoose.Schema.Types.Double.cast(v => {
324324
if (isNaN(v)) {
325-
return 0;
325+
return new BSON.Double(2);
326326
}
327327
return defaultCast(v);
328328
});
@@ -336,7 +336,7 @@ describe('Double', function() {
336336
const doc = new Test({
337337
myDouble: NaN
338338
});
339-
assert.strictEqual(doc.myDouble, 0);
339+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(2));
340340
});
341341
});
342342

@@ -367,19 +367,25 @@ describe('Double', function() {
367367
await Test.create({ myDouble: '42.04' });
368368
const doc = await Test.findOne({ myDouble: { $type: 'number' } });
369369
assert.ok(doc);
370-
assert.strictEqual(doc.myDouble, 42.04);
370+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(42.04));
371371
});
372372

373373
it('is NOT queryable as a BSON Integer in MongoDB if the value is NOT integer', async function() {
374374
await Test.create({ myDouble: '42.04' });
375375
const doc = await Test.findOne({ myDouble: { $type: 'int' } });
376-
assert.strictEqual(doc, null);
376+
assert.deepStrictEqual(doc, null);
377377
});
378378

379-
it('is queryable as a BSON Double in MongoDB', async function() {
379+
it('is queryable as a BSON Double in MongoDB when a non-integer is provided', async function() {
380380
await Test.create({ myDouble: '42.04' });
381381
const doc = await Test.findOne({ myDouble: { $type: 'double' } });
382-
assert.equal(doc.myDouble, 42.04);
382+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(42.04));
383+
});
384+
385+
it('is queryable as a BSON Double in MongoDB when an integer is provided', async function() {
386+
await Test.create({ myDouble: '42' });
387+
const doc = await Test.findOne({ myDouble: { $type: 'double' } });
388+
assert.deepStrictEqual(doc.myDouble, new BSON.Double(42));
383389
});
384390
});
385391

@@ -393,11 +399,11 @@ describe('Double', function() {
393399

394400
let docs = await Test.find({ myDouble: { $gte: 1.710 } }).sort({ myDouble: 1 });
395401
assert.equal(docs.length, 2);
396-
assert.deepStrictEqual(docs.map(doc => doc.myDouble), [1.710, 1.8]);
402+
assert.deepStrictEqual(docs.map(doc => doc.myDouble), [new BSON.Double(1.710), new BSON.Double(1.8)]);
397403

398404
docs = await Test.find({ myDouble: { $lt: 1.710 } }).sort({ myDouble: -1 });
399405
assert.equal(docs.length, 2);
400-
assert.deepStrictEqual(docs.map(doc => doc.myDouble), [1.709, 1.2]);
406+
assert.deepStrictEqual(docs.map(doc => doc.myDouble), [new BSON.Double(1.709), new BSON.Double(1.2)]);
401407
});
402408

403409
it('supports populate()', async function() {

0 commit comments

Comments
 (0)