import {
localeMonthsParse,
defaultLocaleMonths, localeMonths,
- defaultLocaleMonthsShort, localeMonthsShort
+ defaultLocaleMonthsShort, localeMonthsShort,
+ defaultMonthsRegex, monthsRegex,
+ defaultMonthsShortRegex, monthsShortRegex,
+ computeMonthsParse
} from '../units/month';
-proto.months = localeMonths;
-proto._months = defaultLocaleMonths;
-proto.monthsShort = localeMonthsShort;
-proto._monthsShort = defaultLocaleMonthsShort;
-proto.monthsParse = localeMonthsParse;
+proto.months = localeMonths;
+proto._months = defaultLocaleMonths;
+proto.monthsShort = localeMonthsShort;
+proto._monthsShort = defaultLocaleMonthsShort;
+proto.monthsParse = localeMonthsParse;
+proto._monthsRegex = defaultMonthsRegex;
+proto.monthsRegex = monthsRegex;
+proto._monthsShortRegex = defaultMonthsShortRegex;
+proto.monthsShortRegex = monthsShortRegex;
+proto._computeMonthsParse = computeMonthsParse;
// Week
import { localeWeek, defaultLocaleWeek, localeFirstDayOfYear, localeFirstDayOfWeek } from '../units/week';
// any word (or two) characters or numbers including two/three word month in arabic.
// includes scottish gaelic two word and hyphenated months
-export var matchWord = /[0-9]*(a[mn]\s?)?['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\-]+|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}/i;
+export var matchWord = /[0-9]*['a-z\u00A0-\u05FF\u0700-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+|[\u0600-\u06FF\/]+(\s*?[\u0600-\u06FF]+){1,2}/i;
import hasOwnProp from '../utils/has-own-prop';
var regexes = {};
export function addRegexToken (token, regex, strictRegex) {
- regexes[token] = isFunction(regex) ? regex : function (isStrict) {
+ regexes[token] = isFunction(regex) ? regex : function (isStrict, localeData) {
return (isStrict && strictRegex) ? strictRegex : regex;
};
}
// Code from http://stackoverflow.com/questions/3561493/is-there-a-regexp-escape-function-in-javascript
function unescapeFormat(s) {
- return s.replace('\\', '').replace(/\\(\[)|\\(\])|\[([^\]\[]*)\]|\\(.)/g, function (matched, p1, p2, p3, p4) {
+ return regexEscape(s.replace('\\', '').replace(/\\(\[)|\\(\])|\[([^\]\[]*)\]|\\(.)/g, function (matched, p1, p2, p3, p4) {
return p1 || p2 || p3 || p4;
- }).replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
+ }));
+}
+
+export function regexEscape(s) {
+ return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
}
import { get } from '../moment/get-set';
+import hasOwnProp from '../utils/has-own-prop';
import { addFormatToken } from '../format/format';
import { addUnitAlias } from './aliases';
-import { addRegexToken, match1to2, match2, matchWord } from '../parse/regex';
+import { addRegexToken, match1to2, match2, matchWord, regexEscape } from '../parse/regex';
import { addParseToken } from '../parse/token';
import { hooks } from '../utils/hooks';
import { MONTH } from './constants';
addRegexToken('M', match1to2);
addRegexToken('MM', match1to2, match2);
-addRegexToken('MMM', matchWord);
-addRegexToken('MMMM', matchWord);
+addRegexToken('MMM', function (isStrict, locale) {
+ return locale.monthsShortRegex(isStrict);
+});
+addRegexToken('MMMM', function (isStrict, locale) {
+ return locale.monthsRegex(isStrict);
+});
addParseToken(['M', 'MM'], function (input, array) {
array[MONTH] = toInt(input) - 1;
export function getDaysInMonth () {
return daysInMonth(this.year(), this.month());
}
+
+export var defaultMonthsShortRegex = matchWord;
+export function monthsShortRegex (isStrict) {
+ if (this._monthsParseExact) {
+ this._computeMonthsParse();
+ if (isStrict) {
+ return this._monthsShortStrictRegex;
+ } else {
+ return this._monthsShortRegex;
+ }
+ } else {
+ return this._monthsShortStrictRegex && isStrict ?
+ this._monthsShortStrictRegex : this._monthsShortRegex;
+ }
+}
+
+export var defaultMonthsRegex = matchWord;
+export function monthsRegex (isStrict) {
+ if (this._monthsParseExact) {
+ this._computeMonthsParse();
+ if (isStrict) {
+ return this._monthsStrictRegex;
+ } else {
+ return this._monthsRegex;
+ }
+ } else {
+ return this._monthsStrictRegex && isStrict ?
+ this._monthsStrictRegex : this._monthsRegex;
+ }
+}
+
+export function computeMonthsParse () {
+ if (!hasOwnProp(this, '_monthsRegex')) {
+ var shortOnly = '', longOnly = '', mixed = '', i, mom;
+ for (i = 0; i < 12; i++) {
+ // make the regex if we don't have it already
+ mom = createUTC([2000, i]);
+ shortOnly += '|' + regexEscape(this.monthsShort(mom, ''));
+ longOnly += '|' + regexEscape(this.months(mom, ''));
+ mixed += '|' + regexEscape(this.months(mom, '')) + '|' + regexEscape(this.monthsShort(mom, ''));
+ }
+ shortOnly = shortOnly.substr(1);
+ longOnly = longOnly.substr(1);
+ mixed = mixed.substr(1);
+
+ this._monthsRegex = new RegExp('^(' + mixed + ')', 'i');
+ this._monthsShortRegex = this._monthsRegex;
+ this._monthsStrictRegex = new RegExp('^(' + longOnly + ')$', 'i');
+ this._monthsShortStrictRegex = new RegExp('^(' + shortOnly + ')$', 'i');
+ }
+}
import moment from '../moment';
var months = [
- 'Am Faoilleach',
- 'An Gearran',
- 'Am Màrt',
- 'An Giblean',
- 'An Cèitean',
- 'An t-Ògmhios',
- 'An t-Iuchar',
- 'An Lùnastal',
- 'An t-Sultain',
- 'An Dàmhair',
- 'An t-Samhain',
- 'An Dùbhlachd'
+ 'Am Faoilleach', 'An Gearran', 'Am Màrt', 'An Giblean', 'An Cèitean', 'An t-Ògmhios', 'An t-Iuchar', 'An Lùnastal', 'An t-Sultain', 'An Dàmhair', 'An t-Samhain', 'An Dùbhlachd'
];
var monthsShort = ['Faoi', 'Gear', 'Màrt', 'Gibl', 'Cèit', 'Ògmh', 'Iuch', 'Lùn', 'Sult', 'Dàmh', 'Samh', 'Dùbh'];
export default moment.defineLocale('gd', {
months : months,
monthsShort : monthsShort,
+ monthsParseExact : true,
weekdays : weekdays,
weekdaysShort : weekdaysShort,
weekdaysMin : weekdaysMin,
test('parse', function (assert) {
function equalTest(monthName, monthFormat, monthNum) {
- assert.equal(moment(monthName, monthFormat).month(), monthNum, monthName + ' should be month ' + monthNum + 1);
+ assert.equal(moment(monthName, monthFormat).month(), monthNum, monthName + ' should be month ' + (monthNum + 1));
}
for (var i = 0; i < 12; i++) {