forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(NODE-6504): Add Double as a SchemaType #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0e5fd09
initial commit
aditi-khare-mongoDB 727ca14
temp
aditi-khare-mongoDB c88c52e
casted
aditi-khare-mongoDB 4fb53d6
ready for review
aditi-khare-mongoDB 6ec99a9
lint fix
aditi-khare-mongoDB dbb68dc
add link
aditi-khare-mongoDB b3c8955
requested changes
aditi-khare-mongoDB b467e69
requested change 2
aditi-khare-mongoDB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| 'use strict'; | ||
|
|
||
| const assert = require('assert'); | ||
| const BSON = require('bson'); | ||
| const isBsonType = require('../helpers/isBsonType'); | ||
|
|
||
| /** | ||
| * Given a value, cast it to a IEEE 754-2008 floating point, or throw an `Error` if the value | ||
| * cannot be casted. `null`, `undefined`, and `NaN` are considered valid inputs. | ||
| * | ||
| * @param {Any} value | ||
| * @return {Number} | ||
| * @throws {Error} if `value` does not represent a IEEE 754-2008 floating point. If casting from a string, see [BSON Double.fromString API documentation](https://mongodb.github.io/node-mongodb-native/Next/classes/BSON.Double.html#fromString) | ||
| * @api private | ||
| */ | ||
|
|
||
| module.exports = function castDouble(val) { | ||
| if (val == null || val === '') { | ||
| return null; | ||
| } | ||
|
|
||
| let coercedVal; | ||
| if (isBsonType(val, 'Long')) { | ||
| coercedVal = val.toNumber(); | ||
| } else if (typeof val === 'string') { | ||
| try { | ||
| coercedVal = BSON.Double.fromString(val); | ||
| return coercedVal; | ||
| } catch { | ||
| assert.ok(false); | ||
| } | ||
| } else if (typeof val === 'object') { | ||
| const tempVal = val.valueOf() ?? val.toString(); | ||
| // ex: { a: 'im an object, valueOf: () => 'helloworld' } // throw an error | ||
| if (typeof tempVal === 'string') { | ||
| try { | ||
| coercedVal = BSON.Double.fromString(val); | ||
| return coercedVal; | ||
| } catch { | ||
| assert.ok(false); | ||
| } | ||
| } else { | ||
| coercedVal = Number(tempVal); | ||
| } | ||
| } else { | ||
| coercedVal = Number(val); | ||
| } | ||
|
|
||
| return new BSON.Double(coercedVal); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| 'use strict'; | ||
|
|
||
| /*! | ||
| * Module dependencies. | ||
| */ | ||
|
|
||
| const CastError = require('../error/cast'); | ||
| const SchemaType = require('../schemaType'); | ||
| const castDouble = require('../cast/double'); | ||
|
|
||
| /** | ||
| * Double SchemaType constructor. | ||
| * | ||
| * @param {String} path | ||
| * @param {Object} options | ||
| * @inherits SchemaType | ||
| * @api public | ||
| */ | ||
|
|
||
| function SchemaDouble(path, options) { | ||
| SchemaType.call(this, path, options, 'Double'); | ||
| } | ||
|
|
||
| /** | ||
| * This schema type's name, to defend against minifiers that mangle | ||
| * function names. | ||
| * | ||
| * @api public | ||
| */ | ||
| SchemaDouble.schemaName = 'Double'; | ||
|
|
||
| SchemaDouble.defaultOptions = {}; | ||
|
|
||
| /*! | ||
| * Inherits from SchemaType. | ||
| */ | ||
| SchemaDouble.prototype = Object.create(SchemaType.prototype); | ||
| SchemaDouble.prototype.constructor = SchemaDouble; | ||
|
|
||
| /*! | ||
| * ignore | ||
| */ | ||
|
|
||
| SchemaDouble._cast = castDouble; | ||
|
|
||
| /** | ||
| * Sets a default option for all Double instances. | ||
| * | ||
| * #### Example: | ||
| * | ||
| * // Make all Double fields required by default | ||
| * mongoose.Schema.Double.set('required', true); | ||
| * | ||
| * @param {String} option The option you'd like to set the value for | ||
| * @param {Any} value value for option | ||
| * @return {undefined} | ||
| * @function set | ||
| * @static | ||
| * @api public | ||
| */ | ||
|
|
||
| SchemaDouble.set = SchemaType.set; | ||
|
|
||
| SchemaDouble.setters = []; | ||
|
|
||
| /** | ||
| * Attaches a getter for all Double instances | ||
| * | ||
| * #### Example: | ||
| * | ||
| * // Converts Double to be a represent milliseconds upon access | ||
| * mongoose.Schema.Double.get(v => v == null ? '0.000 ms' : v.toString() + ' ms'); | ||
| * | ||
| * @param {Function} getter | ||
| * @return {this} | ||
| * @function get | ||
| * @static | ||
| * @api public | ||
| */ | ||
|
|
||
| SchemaDouble.get = SchemaType.get; | ||
|
|
||
| /*! | ||
| * ignore | ||
| */ | ||
|
|
||
| SchemaDouble._defaultCaster = v => { | ||
| if (v != null) { | ||
| if (v._bsontype !== 'Double') { | ||
baileympearson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| throw new Error(); | ||
| } | ||
| } | ||
|
|
||
| return v; | ||
| }; | ||
|
|
||
| /** | ||
| * Get/set the function used to cast arbitrary values to IEEE 754-2008 floating points | ||
| * | ||
| * #### Example: | ||
| * | ||
| * // Make Mongoose cast any NaNs to 0 | ||
| * const defaultCast = mongoose.Schema.Types.Double.cast(); | ||
| * mongoose.Schema.Types.Double.cast(v => { | ||
| * if (isNaN(v)) { | ||
| * return 0; | ||
| * } | ||
| * return defaultCast(v); | ||
| * }); | ||
| * | ||
| * // Or disable casting for Doubles entirely (only JS numbers are permitted) | ||
| * mongoose.Schema.Double.cast(false); | ||
| * | ||
| * | ||
| * @param {Function} caster | ||
| * @return {Function} | ||
| * @function get | ||
| * @static | ||
| * @api public | ||
| */ | ||
|
|
||
| SchemaDouble.cast = function cast(caster) { | ||
| if (arguments.length === 0) { | ||
| return this._cast; | ||
| } | ||
| if (caster === false) { | ||
| caster = this._defaultCaster; | ||
| } | ||
|
|
||
| this._cast = caster; | ||
|
|
||
| return this._cast; | ||
| }; | ||
|
|
||
|
|
||
| /*! | ||
| * ignore | ||
| */ | ||
|
|
||
| SchemaDouble._checkRequired = v => v != null; | ||
| /** | ||
| * Override the function the required validator uses to check whether a value | ||
| * passes the `required` check. | ||
| * | ||
| * @param {Function} fn | ||
| * @return {Function} | ||
| * @function checkRequired | ||
| * @static | ||
| * @api public | ||
| */ | ||
|
|
||
| SchemaDouble.checkRequired = SchemaType.checkRequired; | ||
|
|
||
| /** | ||
| * Check if the given value satisfies a required validator. | ||
| * | ||
| * @param {Any} value | ||
| * @return {Boolean} | ||
| * @api public | ||
| */ | ||
|
|
||
| SchemaDouble.prototype.checkRequired = function(value) { | ||
| return this.constructor._checkRequired(value); | ||
| }; | ||
|
|
||
| /** | ||
| * Casts to Double | ||
| * | ||
| * @param {Object} value | ||
| * @param {Object} model this value is optional | ||
| * @api private | ||
| */ | ||
|
|
||
| SchemaDouble.prototype.cast = function(value) { | ||
| let castDouble; | ||
| if (typeof this._castFunction === 'function') { | ||
| castDouble = this._castFunction; | ||
| } else if (typeof this.constructor.cast === 'function') { | ||
| castDouble = this.constructor.cast(); | ||
| } else { | ||
| castDouble = SchemaDouble.cast(); | ||
| } | ||
|
|
||
| try { | ||
| return castDouble(value); | ||
| } catch (error) { | ||
| throw new CastError('Double', value, this.path, error, this); | ||
| } | ||
| }; | ||
|
|
||
| /*! | ||
| * ignore | ||
| */ | ||
|
|
||
| function handleSingle(val) { | ||
| return this.cast(val); | ||
| } | ||
|
|
||
| SchemaDouble.prototype.$conditionalHandlers = { | ||
| ...SchemaType.prototype.$conditionalHandlers, | ||
| $gt: handleSingle, | ||
| $gte: handleSingle, | ||
| $lt: handleSingle, | ||
| $lte: handleSingle | ||
| }; | ||
|
|
||
|
|
||
| /*! | ||
| * Module exports. | ||
| */ | ||
|
|
||
| module.exports = SchemaDouble; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.