PageRenderTime 31ms CodeModel.GetById 13ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 1ms

/node_modules/moment/src/lib/units/month.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 290 lines | 238 code | 34 blank | 18 comment | 60 complexity | 02f2c0c16099fe74f8152e2735689956 MD5 | raw file
  1import { get } from '../moment/get-set';
  2import hasOwnProp from '../utils/has-own-prop';
  3import { addFormatToken } from '../format/format';
  4import { addUnitAlias } from './aliases';
  5import { addUnitPriority } from './priorities';
  6import { addRegexToken, match1to2, match2, matchWord, regexEscape } from '../parse/regex';
  7import { addParseToken } from '../parse/token';
  8import { hooks } from '../utils/hooks';
  9import { MONTH } from './constants';
 10import toInt from '../utils/to-int';
 11import isArray from '../utils/is-array';
 12import isNumber from '../utils/is-number';
 13import mod from '../utils/mod';
 14import indexOf from '../utils/index-of';
 15import { createUTC } from '../create/utc';
 16import getParsingFlags from '../create/parsing-flags';
 17import { isLeapYear } from '../units/year';
 18
 19export function daysInMonth(year, month) {
 20    if (isNaN(year) || isNaN(month)) {
 21        return NaN;
 22    }
 23    var modMonth = mod(month, 12);
 24    year += (month - modMonth) / 12;
 25    return modMonth === 1 ? (isLeapYear(year) ? 29 : 28) : (31 - modMonth % 7 % 2);
 26}
 27
 28// FORMATTING
 29
 30addFormatToken('M', ['MM', 2], 'Mo', function () {
 31    return this.month() + 1;
 32});
 33
 34addFormatToken('MMM', 0, 0, function (format) {
 35    return this.localeData().monthsShort(this, format);
 36});
 37
 38addFormatToken('MMMM', 0, 0, function (format) {
 39    return this.localeData().months(this, format);
 40});
 41
 42// ALIASES
 43
 44addUnitAlias('month', 'M');
 45
 46// PRIORITY
 47
 48addUnitPriority('month', 8);
 49
 50// PARSING
 51
 52addRegexToken('M',    match1to2);
 53addRegexToken('MM',   match1to2, match2);
 54addRegexToken('MMM',  function (isStrict, locale) {
 55    return locale.monthsShortRegex(isStrict);
 56});
 57addRegexToken('MMMM', function (isStrict, locale) {
 58    return locale.monthsRegex(isStrict);
 59});
 60
 61addParseToken(['M', 'MM'], function (input, array) {
 62    array[MONTH] = toInt(input) - 1;
 63});
 64
 65addParseToken(['MMM', 'MMMM'], function (input, array, config, token) {
 66    var month = config._locale.monthsParse(input, token, config._strict);
 67    // if we didn't find a month name, mark the date as invalid.
 68    if (month != null) {
 69        array[MONTH] = month;
 70    } else {
 71        getParsingFlags(config).invalidMonth = input;
 72    }
 73});
 74
 75// LOCALES
 76
 77var MONTHS_IN_FORMAT = /D[oD]?(\[[^\[\]]*\]|\s)+MMMM?/;
 78export var defaultLocaleMonths = 'January_February_March_April_May_June_July_August_September_October_November_December'.split('_');
 79export function localeMonths (m, format) {
 80    if (!m) {
 81        return isArray(this._months) ? this._months :
 82            this._months['standalone'];
 83    }
 84    return isArray(this._months) ? this._months[m.month()] :
 85        this._months[(this._months.isFormat || MONTHS_IN_FORMAT).test(format) ? 'format' : 'standalone'][m.month()];
 86}
 87
 88export var defaultLocaleMonthsShort = 'Jan_Feb_Mar_Apr_May_Jun_Jul_Aug_Sep_Oct_Nov_Dec'.split('_');
 89export function localeMonthsShort (m, format) {
 90    if (!m) {
 91        return isArray(this._monthsShort) ? this._monthsShort :
 92            this._monthsShort['standalone'];
 93    }
 94    return isArray(this._monthsShort) ? this._monthsShort[m.month()] :
 95        this._monthsShort[MONTHS_IN_FORMAT.test(format) ? 'format' : 'standalone'][m.month()];
 96}
 97
 98function handleStrictParse(monthName, format, strict) {
 99    var i, ii, mom, llc = monthName.toLocaleLowerCase();
100    if (!this._monthsParse) {
101        // this is not used
102        this._monthsParse = [];
103        this._longMonthsParse = [];
104        this._shortMonthsParse = [];
105        for (i = 0; i < 12; ++i) {
106            mom = createUTC([2000, i]);
107            this._shortMonthsParse[i] = this.monthsShort(mom, '').toLocaleLowerCase();
108            this._longMonthsParse[i] = this.months(mom, '').toLocaleLowerCase();
109        }
110    }
111
112    if (strict) {
113        if (format === 'MMM') {
114            ii = indexOf.call(this._shortMonthsParse, llc);
115            return ii !== -1 ? ii : null;
116        } else {
117            ii = indexOf.call(this._longMonthsParse, llc);
118            return ii !== -1 ? ii : null;
119        }
120    } else {
121        if (format === 'MMM') {
122            ii = indexOf.call(this._shortMonthsParse, llc);
123            if (ii !== -1) {
124                return ii;
125            }
126            ii = indexOf.call(this._longMonthsParse, llc);
127            return ii !== -1 ? ii : null;
128        } else {
129            ii = indexOf.call(this._longMonthsParse, llc);
130            if (ii !== -1) {
131                return ii;
132            }
133            ii = indexOf.call(this._shortMonthsParse, llc);
134            return ii !== -1 ? ii : null;
135        }
136    }
137}
138
139export function localeMonthsParse (monthName, format, strict) {
140    var i, mom, regex;
141
142    if (this._monthsParseExact) {
143        return handleStrictParse.call(this, monthName, format, strict);
144    }
145
146    if (!this._monthsParse) {
147        this._monthsParse = [];
148        this._longMonthsParse = [];
149        this._shortMonthsParse = [];
150    }
151
152    // TODO: add sorting
153    // Sorting makes sure if one month (or abbr) is a prefix of another
154    // see sorting in computeMonthsParse
155    for (i = 0; i < 12; i++) {
156        // make the regex if we don't have it already
157        mom = createUTC([2000, i]);
158        if (strict && !this._longMonthsParse[i]) {
159            this._longMonthsParse[i] = new RegExp('^' + this.months(mom, '').replace('.', '') + '$', 'i');
160            this._shortMonthsParse[i] = new RegExp('^' + this.monthsShort(mom, '').replace('.', '') + '$', 'i');
161        }
162        if (!strict && !this._monthsParse[i]) {
163            regex = '^' + this.months(mom, '') + '|^' + this.monthsShort(mom, '');
164            this._monthsParse[i] = new RegExp(regex.replace('.', ''), 'i');
165        }
166        // test the regex
167        if (strict && format === 'MMMM' && this._longMonthsParse[i].test(monthName)) {
168            return i;
169        } else if (strict && format === 'MMM' && this._shortMonthsParse[i].test(monthName)) {
170            return i;
171        } else if (!strict && this._monthsParse[i].test(monthName)) {
172            return i;
173        }
174    }
175}
176
177// MOMENTS
178
179export function setMonth (mom, value) {
180    var dayOfMonth;
181
182    if (!mom.isValid()) {
183        // No op
184        return mom;
185    }
186
187    if (typeof value === 'string') {
188        if (/^\d+$/.test(value)) {
189            value = toInt(value);
190        } else {
191            value = mom.localeData().monthsParse(value);
192            // TODO: Another silent failure?
193            if (!isNumber(value)) {
194                return mom;
195            }
196        }
197    }
198
199    dayOfMonth = Math.min(mom.date(), daysInMonth(mom.year(), value));
200    mom._d['set' + (mom._isUTC ? 'UTC' : '') + 'Month'](value, dayOfMonth);
201    return mom;
202}
203
204export function getSetMonth (value) {
205    if (value != null) {
206        setMonth(this, value);
207        hooks.updateOffset(this, true);
208        return this;
209    } else {
210        return get(this, 'Month');
211    }
212}
213
214export function getDaysInMonth () {
215    return daysInMonth(this.year(), this.month());
216}
217
218var defaultMonthsShortRegex = matchWord;
219export function monthsShortRegex (isStrict) {
220    if (this._monthsParseExact) {
221        if (!hasOwnProp(this, '_monthsRegex')) {
222            computeMonthsParse.call(this);
223        }
224        if (isStrict) {
225            return this._monthsShortStrictRegex;
226        } else {
227            return this._monthsShortRegex;
228        }
229    } else {
230        if (!hasOwnProp(this, '_monthsShortRegex')) {
231            this._monthsShortRegex = defaultMonthsShortRegex;
232        }
233        return this._monthsShortStrictRegex && isStrict ?
234            this._monthsShortStrictRegex : this._monthsShortRegex;
235    }
236}
237
238var defaultMonthsRegex = matchWord;
239export function monthsRegex (isStrict) {
240    if (this._monthsParseExact) {
241        if (!hasOwnProp(this, '_monthsRegex')) {
242            computeMonthsParse.call(this);
243        }
244        if (isStrict) {
245            return this._monthsStrictRegex;
246        } else {
247            return this._monthsRegex;
248        }
249    } else {
250        if (!hasOwnProp(this, '_monthsRegex')) {
251            this._monthsRegex = defaultMonthsRegex;
252        }
253        return this._monthsStrictRegex && isStrict ?
254            this._monthsStrictRegex : this._monthsRegex;
255    }
256}
257
258function computeMonthsParse () {
259    function cmpLenRev(a, b) {
260        return b.length - a.length;
261    }
262
263    var shortPieces = [], longPieces = [], mixedPieces = [],
264        i, mom;
265    for (i = 0; i < 12; i++) {
266        // make the regex if we don't have it already
267        mom = createUTC([2000, i]);
268        shortPieces.push(this.monthsShort(mom, ''));
269        longPieces.push(this.months(mom, ''));
270        mixedPieces.push(this.months(mom, ''));
271        mixedPieces.push(this.monthsShort(mom, ''));
272    }
273    // Sorting makes sure if one month (or abbr) is a prefix of another it
274    // will match the longer piece.
275    shortPieces.sort(cmpLenRev);
276    longPieces.sort(cmpLenRev);
277    mixedPieces.sort(cmpLenRev);
278    for (i = 0; i < 12; i++) {
279        shortPieces[i] = regexEscape(shortPieces[i]);
280        longPieces[i] = regexEscape(longPieces[i]);
281    }
282    for (i = 0; i < 24; i++) {
283        mixedPieces[i] = regexEscape(mixedPieces[i]);
284    }
285
286    this._monthsRegex = new RegExp('^(' + mixedPieces.join('|') + ')', 'i');
287    this._monthsShortRegex = this._monthsRegex;
288    this._monthsStrictRegex = new RegExp('^(' + longPieces.join('|') + ')', 'i');
289    this._monthsShortStrictRegex = new RegExp('^(' + shortPieces.join('|') + ')', 'i');
290}