from-anything.js (3351B)
1import isArray from '../utils/is-array'; 2import isObject from '../utils/is-object'; 3import isObjectEmpty from '../utils/is-object-empty'; 4import isUndefined from '../utils/is-undefined'; 5import isNumber from '../utils/is-number'; 6import isDate from '../utils/is-date'; 7import map from '../utils/map'; 8import { createInvalid } from './valid'; 9import { Moment, isMoment } from '../moment/constructor'; 10import { getLocale } from '../locale/locales'; 11import { hooks } from '../utils/hooks'; 12import checkOverflow from './check-overflow'; 13import { isValid } from './valid'; 14 15import { configFromStringAndArray } from './from-string-and-array'; 16import { configFromStringAndFormat } from './from-string-and-format'; 17import { configFromString } from './from-string'; 18import { configFromArray } from './from-array'; 19import { configFromObject } from './from-object'; 20 21function createFromConfig(config) { 22 var res = new Moment(checkOverflow(prepareConfig(config))); 23 if (res._nextDay) { 24 // Adding is smart enough around DST 25 res.add(1, 'd'); 26 res._nextDay = undefined; 27 } 28 29 return res; 30} 31 32export function prepareConfig(config) { 33 var input = config._i, 34 format = config._f; 35 36 config._locale = config._locale || getLocale(config._l); 37 38 if (input === null || (format === undefined && input === '')) { 39 return createInvalid({ nullInput: true }); 40 } 41 42 if (typeof input === 'string') { 43 config._i = input = config._locale.preparse(input); 44 } 45 46 if (isMoment(input)) { 47 return new Moment(checkOverflow(input)); 48 } else if (isDate(input)) { 49 config._d = input; 50 } else if (isArray(format)) { 51 configFromStringAndArray(config); 52 } else if (format) { 53 configFromStringAndFormat(config); 54 } else { 55 configFromInput(config); 56 } 57 58 if (!isValid(config)) { 59 config._d = null; 60 } 61 62 return config; 63} 64 65function configFromInput(config) { 66 var input = config._i; 67 if (isUndefined(input)) { 68 config._d = new Date(hooks.now()); 69 } else if (isDate(input)) { 70 config._d = new Date(input.valueOf()); 71 } else if (typeof input === 'string') { 72 configFromString(config); 73 } else if (isArray(input)) { 74 config._a = map(input.slice(0), function (obj) { 75 return parseInt(obj, 10); 76 }); 77 configFromArray(config); 78 } else if (isObject(input)) { 79 configFromObject(config); 80 } else if (isNumber(input)) { 81 // from milliseconds 82 config._d = new Date(input); 83 } else { 84 hooks.createFromInputFallback(config); 85 } 86} 87 88export function createLocalOrUTC(input, format, locale, strict, isUTC) { 89 var c = {}; 90 91 if (format === true || format === false) { 92 strict = format; 93 format = undefined; 94 } 95 96 if (locale === true || locale === false) { 97 strict = locale; 98 locale = undefined; 99 } 100 101 if ( 102 (isObject(input) && isObjectEmpty(input)) || 103 (isArray(input) && input.length === 0) 104 ) { 105 input = undefined; 106 } 107 // object construction must be done this way. 108 // https://github.com/moment/moment/issues/1423 109 c._isAMomentObject = true; 110 c._useUTC = c._isUTC = isUTC; 111 c._l = locale; 112 c._i = input; 113 c._f = format; 114 c._strict = strict; 115 116 return createFromConfig(c); 117}