-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
50 lines (36 loc) · 1.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
'use strict';
var mongoose = require('mongoose');
var util = require('util');
var CastError = mongoose.SchemaType.CastError;
function FloatType(digits) {
function Float(path, options) {
this.path = path;
mongoose.SchemaTypes.Number.call(this, path, options);
}
util.inherits(Float, mongoose.SchemaTypes.Number);
Float.prototype.cast = function(value) {
if (value == null || value === '') {
return value;
}
if (typeof value === 'string' || typeof value === 'boolean') {
value = Number(value);
}
if (isNaN(value)) return new CastError('Number', value, this.path);
return Number(value.toFixed(digits || 2));
};
Float._checkRequired = value => typeof value === 'number' || value instanceof Number;
return Float;
}
module.exports.loadType = function(mongoose, digits) {
var floatType = new FloatType(digits);
if (mongoose.Schema && typeof mongoose.Schema.Types === 'object') {
mongoose.Schema.Types.Float = floatType;
}
if (typeof mongoose.SchemaTypes === 'object') {
mongoose.SchemaTypes.Float = floatType;
}
if (typeof mongoose.Types === 'object') {
mongoose.Types.Float = floatType;
}
return floatType;
};