Skip to content

Commit 4fb53d6

Browse files
ready for review
1 parent c88c52e commit 4fb53d6

File tree

10 files changed

+683
-6
lines changed

10 files changed

+683
-6
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ 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.Double = mongoose.Double;
4950
module.exports.Error = mongoose.Error;
5051
module.exports.MongooseError = mongoose.MongooseError;
5152
module.exports.now = mongoose.now;

lib/mongoose.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,19 @@ 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+
11701183
/**
11711184
* The Mongoose Number [SchemaType](https://mongoosejs.com/docs/schematypes.html). Used for
11721185
* declaring paths in your schema that Mongoose should cast to numbers.

lib/schema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,6 +2848,7 @@ module.exports = exports = Schema;
28482848
* - [Mixed](https://mongoosejs.com/docs/schematypes.html#mixed)
28492849
* - [UUID](https://mongoosejs.com/docs/schematypes.html#uuid)
28502850
* - [BigInt](https://mongoosejs.com/docs/schematypes.html#bigint)
2851+
* - [Double] (https://mongoosejs.com/docs/schematypes.html#double)
28512852
*
28522853
* Using this exposed access to the `Mixed` SchemaType, we can use them in our schema.
28532854
*

lib/schema/double.js

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
'use strict';
2+
3+
/*!
4+
* Module dependencies.
5+
*/
6+
7+
const CastError = require('../error/cast');
8+
const SchemaType = require('../schemaType');
9+
const castDouble = require('../cast/double');
10+
11+
/**
12+
* Double SchemaType constructor.
13+
*
14+
* @param {String} path
15+
* @param {Object} options
16+
* @inherits SchemaType
17+
* @api public
18+
*/
19+
20+
function SchemaDouble(path, options) {
21+
SchemaType.call(this, path, options, 'Double');
22+
}
23+
24+
/**
25+
* This schema type's name, to defend against minifiers that mangle
26+
* function names.
27+
*
28+
* @api public
29+
*/
30+
SchemaDouble.schemaName = 'Double';
31+
32+
SchemaDouble.defaultOptions = {};
33+
34+
/*!
35+
* Inherits from SchemaType.
36+
*/
37+
SchemaDouble.prototype = Object.create(SchemaType.prototype);
38+
SchemaDouble.prototype.constructor = SchemaDouble;
39+
40+
/*!
41+
* ignore
42+
*/
43+
44+
SchemaDouble._cast = castDouble;
45+
46+
/**
47+
* Sets a default option for all Double instances.
48+
*
49+
* #### Example:
50+
*
51+
* // Make all Double fields required by default
52+
* mongoose.Schema.Double.set('required', true);
53+
*
54+
* @param {String} option The option you'd like to set the value for
55+
* @param {Any} value value for option
56+
* @return {undefined}
57+
* @function set
58+
* @static
59+
* @api public
60+
*/
61+
62+
SchemaDouble.set = SchemaType.set;
63+
64+
SchemaDouble.setters = [];
65+
66+
/**
67+
* Attaches a getter for all Double instances
68+
*
69+
* #### Example:
70+
*
71+
* // Converts Double to be a represent milliseconds upon access
72+
* mongoose.Schema.Double.get(v => v == null ? '0.000 ms' : v.toString() + ' ms');
73+
*
74+
* @param {Function} getter
75+
* @return {this}
76+
* @function get
77+
* @static
78+
* @api public
79+
*/
80+
81+
SchemaDouble.get = SchemaType.get;
82+
83+
/*!
84+
* ignore
85+
*/
86+
87+
SchemaDouble._defaultCaster = v => {
88+
if (v != null) {
89+
if (typeof v !== 'number') {
90+
throw new Error();
91+
}
92+
}
93+
94+
return v;
95+
};
96+
97+
/**
98+
* Get/set the function used to cast arbitrary values to IEEE 754-2008 floating points
99+
*
100+
* #### Example:
101+
*
102+
* // Make Mongoose cast any NaNs to 0
103+
* const defaultCast = mongoose.Schema.Types.Double.cast();
104+
* mongoose.Schema.Types.Double.cast(v => {
105+
* if (isNaN(v)) {
106+
* return 0;
107+
* }
108+
* return defaultCast(v);
109+
* });
110+
*
111+
* // Or disable casting for Doubles entirely (only JS numbers are permitted)
112+
* mongoose.Schema.Double.cast(false);
113+
*
114+
*
115+
* @param {Function} caster
116+
* @return {Function}
117+
* @function get
118+
* @static
119+
* @api public
120+
*/
121+
122+
SchemaDouble.cast = function cast(caster) {
123+
if (arguments.length === 0) {
124+
return this._cast;
125+
}
126+
if (caster === false) {
127+
caster = this._defaultCaster;
128+
}
129+
130+
this._cast = caster;
131+
132+
return this._cast;
133+
};
134+
135+
136+
/*!
137+
* ignore
138+
*/
139+
140+
SchemaDouble._checkRequired = v => v != null;
141+
/**
142+
* Override the function the required validator uses to check whether a value
143+
* passes the `required` check.
144+
*
145+
* @param {Function} fn
146+
* @return {Function}
147+
* @function checkRequired
148+
* @static
149+
* @api public
150+
*/
151+
152+
SchemaDouble.checkRequired = SchemaType.checkRequired;
153+
154+
/**
155+
* Check if the given value satisfies a required validator.
156+
*
157+
* @param {Any} value
158+
* @return {Boolean}
159+
* @api public
160+
*/
161+
162+
SchemaDouble.prototype.checkRequired = function(value) {
163+
return this.constructor._checkRequired(value);
164+
};
165+
166+
/**
167+
* Casts to Double
168+
*
169+
* @param {Object} value
170+
* @param {Object} model this value is optional
171+
* @api private
172+
*/
173+
174+
SchemaDouble.prototype.cast = function(value) {
175+
let castDouble;
176+
if (typeof this._castFunction === 'function') {
177+
castDouble = this._castFunction;
178+
} else if (typeof this.constructor.cast === 'function') {
179+
castDouble = this.constructor.cast();
180+
} else {
181+
castDouble = SchemaDouble.cast();
182+
}
183+
184+
try {
185+
return castDouble(value);
186+
} catch (error) {
187+
throw new CastError('Double', value, this.path, error, this);
188+
}
189+
};
190+
191+
/*!
192+
* ignore
193+
*/
194+
195+
function handleSingle(val) {
196+
return this.cast(val);
197+
}
198+
199+
SchemaDouble.prototype.$conditionalHandlers = {
200+
...SchemaType.prototype.$conditionalHandlers,
201+
$gt: handleSingle,
202+
$gte: handleSingle,
203+
$lt: handleSingle,
204+
$lte: handleSingle
205+
};
206+
207+
208+
/*!
209+
* Module exports.
210+
*/
211+
212+
module.exports = SchemaDouble;

lib/schema/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ exports.ObjectId = require('./objectId');
1919
exports.String = require('./string');
2020
exports.Subdocument = require('./subdocument');
2121
exports.UUID = require('./uuid');
22+
exports.Double = require('./double');
2223

2324
// alias
2425

0 commit comments

Comments
 (0)