is-moment-input.js (1988B)
1import isObjectEmpty from './is-object-empty'; 2import hasOwnProp from './has-own-prop'; 3import isObject from './is-object'; 4import isDate from './is-date'; 5import isNumber from './is-number'; 6import isString from './is-string'; 7import { isMoment } from '../moment/constructor'; 8import isArray from './is-array'; 9 10// type MomentInput = Moment | Date | string | number | (number | string)[] | MomentInputObject | void; // null | undefined 11export function isMomentInput(input) { 12 return ( 13 isMoment(input) || 14 isDate(input) || 15 isString(input) || 16 isNumber(input) || 17 isNumberOrStringArray(input) || 18 isMomentInputObject(input) || 19 input === null || 20 input === undefined 21 ); 22} 23 24export function isMomentInputObject(input) { 25 var objectTest = isObject(input) && !isObjectEmpty(input), 26 propertyTest = false, 27 properties = [ 28 'years', 29 'year', 30 'y', 31 'months', 32 'month', 33 'M', 34 'days', 35 'day', 36 'd', 37 'dates', 38 'date', 39 'D', 40 'hours', 41 'hour', 42 'h', 43 'minutes', 44 'minute', 45 'm', 46 'seconds', 47 'second', 48 's', 49 'milliseconds', 50 'millisecond', 51 'ms', 52 ], 53 i, 54 property, 55 propertyLen = properties.length; 56 57 for (i = 0; i < propertyLen; i += 1) { 58 property = properties[i]; 59 propertyTest = propertyTest || hasOwnProp(input, property); 60 } 61 62 return objectTest && propertyTest; 63} 64 65function isNumberOrStringArray(input) { 66 var arrayTest = isArray(input), 67 dataTypeTest = false; 68 if (arrayTest) { 69 dataTypeTest = 70 input.filter(function (item) { 71 return !isNumber(item) && isString(input); 72 }).length === 0; 73 } 74 return arrayTest && dataTypeTest; 75}