forked from lykmapipo/mongoose-bigdecimal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
242 lines (192 loc) · 5.63 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
'use strict';
//dependencies
var BigDecimal = require('big.js');
var mongoose = require('mongoose');
var SchemaType = mongoose.SchemaType;
var CastError = SchemaType.CastError;
var errorMessages = mongoose.Error.messages;
//override valueOf to return javascript Number than string
var valueOf = BigDecimal.prototype.valueOf;
BigDecimal.prototype.valueOf = function() {
return Number(valueOf.call(this));
};
/**
* @constructor
* @description mongoose bigdecimal SchemaType
* @param {String} path
* @param {Object} options
* @inherits SchemaType
* @api private
*/
function SchemaBigDecimal(path, options) {
SchemaType.call(this, path, options, 'BigDecimal');
}
/**
* @description specifies schema type's name, to defend against
* minifiers that mangle function names.
* @api private
*/
SchemaBigDecimal.schemaName = 'BigDecimal';
/**
* @description inherits from mongoose SchemaType
*/
SchemaBigDecimal.prototype = Object.create(SchemaType.prototype);
SchemaBigDecimal.prototype.constructor = BigDecimal;
/**
* @function
* @description implement checkRequired method
* Required validator for bigdecimal schema type
* @param {any} value
* @return {Boolean}
* @private
*/
SchemaBigDecimal.prototype.checkRequired = function checkRequired(value) {
return (value !== null) && (value instanceof BigDecimal);
};
/**
* @function
* @description sets a maximum bigdecimal validator
* @param {BigDecimal} value minimum bigdecimal allowed
* @param {String} [message] optional custom error message
* @return {SchemaBigDecimal} this
* @see Customized Error Messages #error_messages_MongooseError-messages
* @public
*/
SchemaBigDecimal.prototype.min = function(value, message) {
if (this.minValidator) {
this.validators = this.validators.filter(function(v) {
return v.validator !== this.minValidator;
}, this);
}
if (null !== value) {
var msg = message || errorMessages.Number.min;
msg = msg.replace(/{MIN}/, value);
this.minValidator = function(v) {
return v !== null && v.gt(value);
};
this.validators.push({
validator: this.minValidator,
message: msg,
type: 'min'
});
}
return this;
};
/**
* @function
* @description sets a maximum bigdecimal validator
* @param {BigDecimal} value maximum bigdecimal allowed
* @param {String} [message] optional custom error message
* @return {SchemaBigDecimal} this
* @see Customized Error Messages #error_messages_MongooseError-messages
* @public
*/
SchemaBigDecimal.prototype.max = function(value, message) {
if (this.maxValidator) {
this.validators = this.validators.filter(function(v) {
return v.validator !== this.maxValidator;
}, this);
}
if (null !== value) {
var msg = message || errorMessages.Number.max;
msg = msg.replace(/{MAX}/, value);
this.maxValidator = function(v) {
return v !== null && v.lt(value);
};
this.validators.push({
validator: this.maxValidator,
message: msg,
type: 'max'
});
}
return this;
};
/**
* @function
* @description casts to bigdecimal
* @param {Object} value the value to cast
* @param {Document} doc document that triggers the casting
* @param {Boolean} [init]
* @private
*/
SchemaBigDecimal.prototype.cast = function(value /*,doc , init*/ ) {
//is null
if (null === value) {
return value;
}
//is empty string
if ('' === value) {
return null;
}
//is bigdecimal
if (value instanceof BigDecimal) {
return value;
}
//is number or string
if ((typeof value === 'string') || (typeof value === 'number')) {
try {
return new BigDecimal(value);
} catch (e) {
throw new CastError('BigDecimal', value, this.path);
}
}
throw new CastError('BigDecimal', value, this.path);
};
/*!
* ignore
*/
function handleSingle(val) {
/*jshint validthis:true*/
return this.cast(val);
}
function handleArray(val) {
/*jshint validthis:true*/
if (!Array.isArray(val)) {
val = [val];
}
return val.map(function(m) {
var cast = this.cast(m);
return cast.valueOf();
}.bind(this));
}
//query conditions that are supported for bigdecimal schema types
SchemaBigDecimal.prototype.$conditionalHandlers = {
'$lt': handleSingle,
'$lte': handleSingle,
'$gt': handleSingle,
'$gte': handleSingle,
'$ne': handleSingle,
'$in': handleArray,
'$nin': handleArray,
'$mod': handleArray,
'$all': handleArray
};
/**
* Casts contents for queries.
*
* @param {String} $conditional
* @param {any} [value] value to be casted for query
* @private
*/
SchemaBigDecimal.prototype.castForQuery = function($conditional, val) {
if (arguments.length === 2) {
var handler = this.$conditionalHandlers[$conditional];
if (!handler) {
throw new Error('Can\'t use ' + $conditional + ' with BigDecimal.');
}
return handler.call(this, val).valueOf();
} else {
return this.cast($conditional).valueOf();
}
};
//------------------------------------------------------------------------------
// Attach Types
//------------------------------------------------------------------------------
//extend mongoose schema types with bigdecimal type
if (!mongoose.Schema.Types.BigDecimal) {
mongoose.Schema.Types.BigDecimal = SchemaBigDecimal;
}
//extend mongoose schema types with bigdecimal type
if (!mongoose.Types.BigDecimal) {
mongoose.Types.BigDecimal = SchemaBigDecimal;
}