# all files / keystone/fields/types/datearray/ DateArrayType.js

92.78% Statements 90/97
82.8% Branches 77/93
80% Functions 8/10
92.71% Lines 89/96

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

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

    2×
    2×

    1×

    1×

    1×
    16×
    16×
    16×
    12×
    5×

    12×
    16×

    16×
    2×

    14×

    16×
    5×
    5×

    16×

    1×
    9×
    9×

    9×
    2×
    1×

    9×
    2×
    1×

    7×
    4×
    4×
    7×

    7×

    7×

    7×
    2×
    2×

    4×
    2×

    9×

    1×

    10×
    10×

    10×
    2×
    1×

    1×

    9×
    3×
    1×

    2×
    1×

    1×
    1×

    6×

    5×
    7×

    5×

    6×
    2×

    4×

    1×

    6×

    6×
    6×

    4×
    12×

    6×
    1×

    6×
    1×
    1×

    6×
    6×

    6×

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

    /**
     * Date FieldType Constructor
     * @extends Field
     * @api public
     */
    function datearray (list, path, options) {
    	this._nativeType = [Date];
    	this._defaultSize = 'medium';
    	this._underscoreMethods = ['format'];
    	this._properties = ['formatString'];
    	this.parseFormatString = options.parseFormat || 'YYYY-MM-DD';
    	this.formatString = (options.format === false) ? false : (options.format || 'Do MMM YYYY');
    	if (this.formatString && typeof this.formatString !== 'string') {
    		throw new Error('FieldType.DateArray: options.format must be a string.');
    	}
    	this.separator = options.separator || ' | ';
    	datearray.super_.call(this, list, path, options);
    }
    util.inherits(datearray, FieldType);

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

    /**
     * Asynchronously confirms that the provided value is valid
     */
    datearray.prototype.validateInput = function (data, callback) {
    	var value = this.getValueFromData(data);
    	var result = true;
    	if (value !== undefined && value !== null && value !== '') {
    		if (!Array.isArray(value)) {
    			value = [value];
    		}
    		for (var i = 0; i < value.length; i++) {
    			var currentValue;
    			// If we pass it an epoch, parse it without the format string
    			if (typeof value[i] === 'number') {
    				currentValue = moment(value[i]);
    			} else {
    				currentValue = moment(value[i], this.parseFormatString);
    			}
    			// If moment does not think it's a valid date, invalidate
    			if (!currentValue.isValid()) {
    				result = false;
    				break;
    			}
    		}
    	}
    	utils.defer(callback, result);
    };

    /**
     * Asynchronously confirms that the a value is present
     */
    datearray.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 (typeof value === 'string' || typeof value === 'number') {
    		if (moment(value).isValid()) {
    			result = true;
    		}
    	// If it's an array of only dates and/or dateify-able data, validate
    	} else if (Array.isArray(value)) {
    		var invalidContent = false;
    		for (var i = 0; i < value.length; i++) {
    			var currentValue;
    			// If we pass it an epoch, parse it without the format string
    			Iif (typeof value[i] === 'number') {
    				currentValue = moment(value[i]);
    			} else {
    				currentValue = moment(value[i], this.parseFormatString);
    			}
    			// If even a single item is not a valid date, invalidate
    			if (!currentValue.isValid()) {
    				invalidContent = true;
    				break;
    			}
    		}
    		if (invalidContent === false) {
    			result = true;
    		}
    	}
    	utils.defer(callback, result);
    };

    /**
     * Checks that a valid array of dates has been provided in a data object
     * An empty value clears the stored value and is considered valid
     *
     * Deprecated
     */
    datearray.prototype.inputIsValid = function (data, required, item) {

    	var value = this.getValueFromData(data);
    	var parseFormatString = this.parseFormatString;

    	if (typeof value === 'string') {
    		if (!moment(value, parseFormatString).isValid()) {
    			return false;
    		}
    		value = [value];
    	}

    	if (required) {
    		if (value === undefined && item && item.get(this.path) && item.get(this.path).length) {
    			return true;
    		}
    		if (value === undefined || !Array.isArray(value)) {
    			return false;
    		}
    		Eif (Array.isArray(value) && !value.length) {
    			return false;
    		}
    	}

    	if (Array.isArray(value)) {
    		// filter out empty fields
    		value = value.filter(function (date) {
    			return date.trim() !== '';
    		});
    		// if there are no values left, and requried is true, return false
    		Iif (required && !value.length) {
    			return false;
    		}
    		// if any date in the array is invalid, return false
    		if (value.some(function (dateValue) { return !moment(dateValue, parseFormatString).isValid(); })) {
    			return false;
    		}
    	}

    	return (value === undefined || Array.isArray(value));

    };

    /**
     * Updates the value for this field in the item from a data object
     */
    datearray.prototype.updateItem = function (item, data, callback) {

    	var value = this.getValueFromData(data);

    	Eif (value !== undefined) {
    		if (Array.isArray(value)) {
    			// Only save valid dates
    			value = value.filter(function (date) {
    				return moment(date).isValid();
    			});
    		}
    		if (value === null) {
    			value = [];
    		}
    		if (typeof value === 'string') {
    			Eif (moment(value).isValid()) {
    				value = [value];
    			}
    		}
    		Eif (Array.isArray(value)) {
    			item.set(this.path, value);
    		}
    	}

    	process.nextTick(callback);
    };

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