|
| 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; |
0 commit comments