UNPKG

1.3 kBJavaScriptView Raw
1/**
2 * Module dependencies.
3 */
4
5var _ = require('lodash'),
6 SchemaType = require('../schematype');
7
8/**
9 * Creates a new SchemaNumber instance.
10 *
11 * @param {Object} [options]
12 * @api public
13 */
14
15var SchemaNumber = module.exports = function(options){
16 SchemaType.call(this, options);
17};
18
19SchemaNumber.__proto__ = SchemaType;
20SchemaNumber.type = SchemaNumber.prototype.type = Number;
21
22/**
23 * Inherits from SchemaType.
24 */
25
26SchemaNumber.prototype.__proto__ = SchemaType.prototype;
27
28/**
29 * Checks if the given `value` is a number.
30 *
31 * @param {any} value
32 * @return {Boolean}
33 * @api public
34 */
35
36SchemaNumber.prototype.checkRequired = function(value){
37 return value instanceof Number || typeof value === 'number';
38};
39
40/**
41 * Casts the given `value` to a number.
42 *
43 * @param {any} value
44 * @return {Number}
45 * @api public
46 */
47
48SchemaNumber.prototype.cast = function(value){
49 if (value == null) return value;
50 if (value === '') return null;
51 if (value instanceof Number || typeof value === 'number') return value;
52 if (!isNaN(+value)) return +value;
53
54 return null;
55};
56
57SchemaNumber.prototype.u$inc = function(data, value){
58 if (!data) return +value;
59
60 return data + +value;
61};
62
63SchemaNumber.prototype.u$dec = function(data, value){
64 if (!data) return -+value;
65
66 return data - +value;
67};
\No newline at end of file