# all files / keystone/fields/types/numberarray/ NumberArrayType.js

88.64% Statements 78/88
75.82% Branches 69/91
80% Functions 8/10
88.64% Lines 78/88

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

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

    2×
    2×

    1×

    1×

    1×
    5×

    1×
    17×
    17×

    17×

    13×
    5×

    13×
    25×

    25×
    9×

    25×
    5×
    5×

    17×

    1×
    8×
    8×

    8×
    2×
    1×

    8×

    8×
    4×
    4×
    8×

    8×
    2×

    8×
    2×
    2×

    4×
    2×

    8×

    1×
    9×
    9×
    4×
    1×

    3×
    3×

    5×

    5×
    4×
    5×
    1×

    4×

    1×
    9×
    9×
    9×
    1×

    9×
    2×

    9×
    20×
    5×

    20×

    20×

    9×

    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 numberarray (list, path, options) {
    	this._nativeType = [Number];
    	this._underscoreMethods = ['format'];
    	this._formatString = (options.format === false) ? false : (options.format || '0,0[.][000000000000]');
    	this._defaultSize = 'small';
    	if (this._formatString && typeof this._formatString !== 'string') {
    		throw new Error('FieldType.NumberArray: options.format must be a string.');
    	}
    	this.separator = options.separator || ' | ';
    	numberarray.super_.call(this, list, path, options);
    }
    util.inherits(numberarray, FieldType);

    /**
     * Formats the field value
     */
    numberarray.prototype.format = function (item, format, separator) {
    	var value = item.get(this.path);
    	if (format || this._formatString) {
    		value = value.map(function (n) {
    			return numeral(n).format(format || this._formatString);
    		});
    	}
    	return value.join(separator || this.separator);
    };

    /**
     * Checks if a value is a valid number
     */
    function isValidNumber (value) {
    	return !Number.isNaN(utils.number(value));
    }

    /**
     * Asynchronously confirms that the provided value is valid
     */
    numberarray.prototype.validateInput = function (data, callback) {
    	var value = this.getValueFromData(data);
    	var result = true;
    	// Let undefined, empty string and null pass
    	if (value !== undefined && value !== '' && value !== null) {
    		// Coerce a single value to an array
    		if (!Array.isArray(value)) {
    			value = [value];
    		}
    		for (var i = 0; i < value.length; i++) {
    			var thisValue = value[i];
    			// If it's a string, check if there's a number in the string
    			if (typeof thisValue === 'string') {
    				thisValue = utils.number(thisValue);
    			}
    			// If it's not a number or NaN invalidate
    			if (typeof thisValue !== 'number' || Number.isNaN(thisValue)) {
    				result = false;
    				break;
    			}
    		}
    	}
    	utils.defer(callback, result);
    };

    /**
     * Asynchronously confirms that the a value is present
     */
    numberarray.prototype.validateRequiredInput = function (item, data, callback) {
    	var value = this.getValueFromData(data);
    	var result = false;
    	// If the field is undefined but has a value saved already, validate
    	if (value === undefined) {
    		if (item.get(this.path) && item.get(this.path).length) {
    			result = true;
    		}
    	}
    	// If it's a string that's not empty, validate
    	Iif (typeof value === 'string' && value !== '') {
    		result = true;
    	// If it's an array of only numbers and/or numberify-able data, validate
    	} else if (Array.isArray(value)) {
    		var invalidContent = false;
    		for (var i = 0; i < value.length; i++) {
    			var thisValue = value[i];
    			// If it's a string, check if there's a number in the string
    			if (typeof thisValue === 'string') {
    				thisValue = utils.number(thisValue);
    			}
    			// If even a single item is not a number or NaN, invalidate
    			if (typeof thisValue !== 'number' || Number.isNaN(thisValue)) {
    				invalidContent = true;
    				break;
    			}
    		}
    		if (invalidContent === false) {
    			result = true;
    		}
    	}
    	utils.defer(callback, result);
    };

    /**
     * Checks that a valid array of number has been provided in a data object
     * An empty value clears the stored value and is considered valid
     *
     * Deprecated
     */
    numberarray.prototype.inputIsValid = function (data, required, item) {
    	var value = this.getValueFromData(data);
    	if (required) {
    		if (value === undefined && item && item.get(this.path) && item.get(this.path).length) {
    			return true;
    		}
    		Eif (value === undefined || !Array.isArray(value) || (typeof value !== 'string') || (typeof value !== 'number')) {
    			return false;
    		}
    		if (Array.isArray(value) && !value.length) {
    			return false;
    		}
    	}
    	Iif (typeof value === 'string') {
    		if (!isValidNumber(value)) {
    			return false;
    		}
    	}
    	if (Array.isArray(value)) {
    		for (var index = 0; index < value.length; index++) {
    			if (!isValidNumber(value[index])) {
    				return false;
    			}
    		}
    	}
    	return (value === undefined || Array.isArray(value) || (typeof value === 'string') || (typeof value === 'number'));
    };

    /**
     * Updates the value for this field in the item from a data object
     */
    numberarray.prototype.updateItem = function (item, data, callback) {
    	var value = this.getValueFromData(data);
    	Eif (typeof value !== 'undefined') {
    		if (value === null || value === '') {
    			value = [];
    		}
    		if (!Array.isArray(value)) {
    			value = [value];
    		}
    		value = value.map(function (num) {
    			if (typeof num !== 'number') {
    				num = utils.number(num);
    			}
    			return num;
    		}).filter(function (num) {
    			return !Number.isNaN(num);
    		});
    		item.set(this.path, value);
    	}
    	process.nextTick(callback);
    };

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