# all files / keystone/fields/types/number/ NumberType.js

94.44% Statements 68/72
79.75% Branches 63/79
85.71% Functions 6/7
94.44% Lines 68/72

    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
    1311×
    1×
    1×
    1×

    1×
    3×
    3×
    3×
    3×
    3×
    1×

    2×

    1×

    1×
    12×
    12×
    12×
    3×
    1×

    2×
    2×

    12×

    1×
    6×
    6×
    6×
    1×

    6×

    1×
    12×
    12×
    2×
    2×

    10×
    4×
    4×
    4×
    3×

    4×

    6×
    6×
    5×
    2×

    3×
    2×

    1×

    6×

    1×

    1×
    37×
    37×
    2×

    35×
    26×
    26×

    9×

    1×
    10×
    10×
    1×

    9×
    9×
    7×
    7×

    2×
    2×

    9×

    1×
     var FieldType = require('../Type');
    var numeral = require('numeral');
    var util = require('util');
    var utils = require('keystone-utils');

    /**
     * Number FieldType Constructor
     * @extends Field
     * @api public
     */
    function number (list, path, options) {
    	this._nativeType = Number;
    	this._fixedSize = 'small';
    	this._underscoreMethods = ['format'];
    	this.formatString = (options.format === false) ? false : (options.format || '0,0[.][000000000000]');
    	if (this.formatString && typeof this.formatString !== 'string') {
    		throw new Error('FieldType.Number: options.format must be a string.');
    	}
    	number.super_.call(this, list, path, options);
    }
    util.inherits(number, FieldType);

    number.prototype.validateInput = function (data, callback) {
    	var value = this.getValueFromData(data);
    	var result = value === undefined || typeof value === 'number' || value === null;
    	if (typeof value === 'string') {
    		if (value === '') {
    			result = true;
    		} else {
    			value = utils.number(value);
    			result = !isNaN(value);
    		}
    	}
    	utils.defer(callback, result);
    };

    number.prototype.validateRequiredInput = function (item, data, callback) {
    	var value = this.getValueFromData(data);
    	var result = !!(value || typeof value === 'number');
    	if (value === undefined && item.get(this.path)) {
    		result = true;
    	}
    	utils.defer(callback, result);
    };

    /**
     * Add filters to a query
     */
    number.prototype.addFilterToQuery = function (filter, query) {
    	query = query || {};
    	if (filter.mode === 'equals' && !filter.value) {
    		query[this.path] = filter.inverted ? { $nin: ['', 0, null] } : { $in: ['', 0, null] };
    		return query;
    	}
    	if (filter.mode === 'between') {
    		var min = utils.number(filter.value.min);
    		var max = utils.number(filter.value.max);
    		if (!isNaN(min) && !isNaN(max)) {
    			query[this.path] = filter.inverted ? { $gte: max, $lte: min } : { $gte: min, $lte: max };
    		}
    		return query;
    	}
    	var value = utils.number(filter.value);
    	if (!isNaN(value)) {
    		if (filter.mode === 'gt') {
    			query[this.path] = filter.inverted ? { $lt: value } : { $gt: value };
    		}
    		else if (filter.mode === 'lt') {
    			query[this.path] = filter.inverted ? { $gt: value } : { $lt: value };
    		}
    		else {
    			query[this.path] = value;
    		}
    	}
    	return query;
    };

    /**
     * Formats the field value
     */
    number.prototype.format = function (item, format) {
    	var value = item.get(this.path);
    	if (format || this.formatString) {
    		return (typeof value === 'number') ? numeral(value).format(format || this.formatString) : '';
    	} else {
    		return value || value === 0 ? String(value) : '';
    	}
    };

    /**
     * Checks that a valid number has been provided in a data object
     * An empty value clears the stored value and is considered valid
     *
     * Deprecated
     */
    number.prototype.inputIsValid = function (data, required, item) {
    	var value = this.getValueFromData(data);
    	if (value === undefined && item && (item.get(this.path) || item.get(this.path) === 0)) {
    		return true;
    	}
    	if (value !== undefined && value !== '') {
    		var newValue = utils.number(value);
    		return (!isNaN(newValue));
    	} else {
    		return (required) ? false : true;
    	}
    };

    /**
     * Updates the value for this field in the item from a data object
     */
    number.prototype.updateItem = function (item, data, callback) {
    	var value = this.getValueFromData(data);
    	if (value === undefined) {
    		return process.nextTick(callback);
    	}
    	var newValue = utils.number(value);
    	if (!isNaN(newValue)) {
    		Eif (newValue !== item.get(this.path)) {
    			item.set(this.path, newValue);
    		}
    	} else Eif (typeof item.get(this.path) === 'number') {
    		item.set(this.path, null);
    	}
    	process.nextTick(callback);
    };

    /* Export Field Type */
    module.exports = number;