corrade-nucleus-nucleons – Blame information for rev 14

Subversion Repositories:
Rev:
Rev Author Line No. Line
10 office 1 import { addFormatToken } from '../format/format';
2 import { addUnitAlias } from './aliases';
3 import { addUnitPriority } from './priorities';
4 import { addRegexToken, match1to2, matchWord, regexEscape } from '../parse/regex';
5 import { addWeekParseToken } from '../parse/token';
6 import toInt from '../utils/to-int';
7 import isArray from '../utils/is-array';
8 import indexOf from '../utils/index-of';
9 import hasOwnProp from '../utils/has-own-prop';
10 import { createUTC } from '../create/utc';
11 import getParsingFlags from '../create/parsing-flags';
12  
13 // FORMATTING
14  
15 addFormatToken('d', 0, 'do', 'day');
16  
17 addFormatToken('dd', 0, 0, function (format) {
18 return this.localeData().weekdaysMin(this, format);
19 });
20  
21 addFormatToken('ddd', 0, 0, function (format) {
22 return this.localeData().weekdaysShort(this, format);
23 });
24  
25 addFormatToken('dddd', 0, 0, function (format) {
26 return this.localeData().weekdays(this, format);
27 });
28  
29 addFormatToken('e', 0, 0, 'weekday');
30 addFormatToken('E', 0, 0, 'isoWeekday');
31  
32 // ALIASES
33  
34 addUnitAlias('day', 'd');
35 addUnitAlias('weekday', 'e');
36 addUnitAlias('isoWeekday', 'E');
37  
38 // PRIORITY
39 addUnitPriority('day', 11);
40 addUnitPriority('weekday', 11);
41 addUnitPriority('isoWeekday', 11);
42  
43 // PARSING
44  
45 addRegexToken('d', match1to2);
46 addRegexToken('e', match1to2);
47 addRegexToken('E', match1to2);
48 addRegexToken('dd', function (isStrict, locale) {
49 return locale.weekdaysMinRegex(isStrict);
50 });
51 addRegexToken('ddd', function (isStrict, locale) {
52 return locale.weekdaysShortRegex(isStrict);
53 });
54 addRegexToken('dddd', function (isStrict, locale) {
55 return locale.weekdaysRegex(isStrict);
56 });
57  
58 addWeekParseToken(['dd', 'ddd', 'dddd'], function (input, week, config, token) {
59 var weekday = config._locale.weekdaysParse(input, token, config._strict);
60 // if we didn't get a weekday name, mark the date as invalid
61 if (weekday != null) {
62 week.d = weekday;
63 } else {
64 getParsingFlags(config).invalidWeekday = input;
65 }
66 });
67  
68 addWeekParseToken(['d', 'e', 'E'], function (input, week, config, token) {
69 week[token] = toInt(input);
70 });
71  
72 // HELPERS
73  
74 function parseWeekday(input, locale) {
75 if (typeof input !== 'string') {
76 return input;
77 }
78  
79 if (!isNaN(input)) {
80 return parseInt(input, 10);
81 }
82  
83 input = locale.weekdaysParse(input);
84 if (typeof input === 'number') {
85 return input;
86 }
87  
88 return null;
89 }
90  
91 function parseIsoWeekday(input, locale) {
92 if (typeof input === 'string') {
93 return locale.weekdaysParse(input) % 7 || 7;
94 }
95 return isNaN(input) ? null : input;
96 }
97  
98 // LOCALES
99  
100 export var defaultLocaleWeekdays = 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_');
101 export function localeWeekdays (m, format) {
102 if (!m) {
103 return isArray(this._weekdays) ? this._weekdays :
104 this._weekdays['standalone'];
105 }
106 return isArray(this._weekdays) ? this._weekdays[m.day()] :
107 this._weekdays[this._weekdays.isFormat.test(format) ? 'format' : 'standalone'][m.day()];
108 }
109  
110 export var defaultLocaleWeekdaysShort = 'Sun_Mon_Tue_Wed_Thu_Fri_Sat'.split('_');
111 export function localeWeekdaysShort (m) {
112 return (m) ? this._weekdaysShort[m.day()] : this._weekdaysShort;
113 }
114  
115 export var defaultLocaleWeekdaysMin = 'Su_Mo_Tu_We_Th_Fr_Sa'.split('_');
116 export function localeWeekdaysMin (m) {
117 return (m) ? this._weekdaysMin[m.day()] : this._weekdaysMin;
118 }
119  
120 function handleStrictParse(weekdayName, format, strict) {
121 var i, ii, mom, llc = weekdayName.toLocaleLowerCase();
122 if (!this._weekdaysParse) {
123 this._weekdaysParse = [];
124 this._shortWeekdaysParse = [];
125 this._minWeekdaysParse = [];
126  
127 for (i = 0; i < 7; ++i) {
128 mom = createUTC([2000, 1]).day(i);
129 this._minWeekdaysParse[i] = this.weekdaysMin(mom, '').toLocaleLowerCase();
130 this._shortWeekdaysParse[i] = this.weekdaysShort(mom, '').toLocaleLowerCase();
131 this._weekdaysParse[i] = this.weekdays(mom, '').toLocaleLowerCase();
132 }
133 }
134  
135 if (strict) {
136 if (format === 'dddd') {
137 ii = indexOf.call(this._weekdaysParse, llc);
138 return ii !== -1 ? ii : null;
139 } else if (format === 'ddd') {
140 ii = indexOf.call(this._shortWeekdaysParse, llc);
141 return ii !== -1 ? ii : null;
142 } else {
143 ii = indexOf.call(this._minWeekdaysParse, llc);
144 return ii !== -1 ? ii : null;
145 }
146 } else {
147 if (format === 'dddd') {
148 ii = indexOf.call(this._weekdaysParse, llc);
149 if (ii !== -1) {
150 return ii;
151 }
152 ii = indexOf.call(this._shortWeekdaysParse, llc);
153 if (ii !== -1) {
154 return ii;
155 }
156 ii = indexOf.call(this._minWeekdaysParse, llc);
157 return ii !== -1 ? ii : null;
158 } else if (format === 'ddd') {
159 ii = indexOf.call(this._shortWeekdaysParse, llc);
160 if (ii !== -1) {
161 return ii;
162 }
163 ii = indexOf.call(this._weekdaysParse, llc);
164 if (ii !== -1) {
165 return ii;
166 }
167 ii = indexOf.call(this._minWeekdaysParse, llc);
168 return ii !== -1 ? ii : null;
169 } else {
170 ii = indexOf.call(this._minWeekdaysParse, llc);
171 if (ii !== -1) {
172 return ii;
173 }
174 ii = indexOf.call(this._weekdaysParse, llc);
175 if (ii !== -1) {
176 return ii;
177 }
178 ii = indexOf.call(this._shortWeekdaysParse, llc);
179 return ii !== -1 ? ii : null;
180 }
181 }
182 }
183  
184 export function localeWeekdaysParse (weekdayName, format, strict) {
185 var i, mom, regex;
186  
187 if (this._weekdaysParseExact) {
188 return handleStrictParse.call(this, weekdayName, format, strict);
189 }
190  
191 if (!this._weekdaysParse) {
192 this._weekdaysParse = [];
193 this._minWeekdaysParse = [];
194 this._shortWeekdaysParse = [];
195 this._fullWeekdaysParse = [];
196 }
197  
198 for (i = 0; i < 7; i++) {
199 // make the regex if we don't have it already
200  
201 mom = createUTC([2000, 1]).day(i);
202 if (strict && !this._fullWeekdaysParse[i]) {
203 this._fullWeekdaysParse[i] = new RegExp('^' + this.weekdays(mom, '').replace('.', '\.?') + '$', 'i');
204 this._shortWeekdaysParse[i] = new RegExp('^' + this.weekdaysShort(mom, '').replace('.', '\.?') + '$', 'i');
205 this._minWeekdaysParse[i] = new RegExp('^' + this.weekdaysMin(mom, '').replace('.', '\.?') + '$', 'i');
206 }
207 if (!this._weekdaysParse[i]) {
208 regex = '^' + this.weekdays(mom, '') + '|^' + this.weekdaysShort(mom, '') + '|^' + this.weekdaysMin(mom, '');
209 this._weekdaysParse[i] = new RegExp(regex.replace('.', ''), 'i');
210 }
211 // test the regex
212 if (strict && format === 'dddd' && this._fullWeekdaysParse[i].test(weekdayName)) {
213 return i;
214 } else if (strict && format === 'ddd' && this._shortWeekdaysParse[i].test(weekdayName)) {
215 return i;
216 } else if (strict && format === 'dd' && this._minWeekdaysParse[i].test(weekdayName)) {
217 return i;
218 } else if (!strict && this._weekdaysParse[i].test(weekdayName)) {
219 return i;
220 }
221 }
222 }
223  
224 // MOMENTS
225  
226 export function getSetDayOfWeek (input) {
227 if (!this.isValid()) {
228 return input != null ? this : NaN;
229 }
230 var day = this._isUTC ? this._d.getUTCDay() : this._d.getDay();
231 if (input != null) {
232 input = parseWeekday(input, this.localeData());
233 return this.add(input - day, 'd');
234 } else {
235 return day;
236 }
237 }
238  
239 export function getSetLocaleDayOfWeek (input) {
240 if (!this.isValid()) {
241 return input != null ? this : NaN;
242 }
243 var weekday = (this.day() + 7 - this.localeData()._week.dow) % 7;
244 return input == null ? weekday : this.add(input - weekday, 'd');
245 }
246  
247 export function getSetISODayOfWeek (input) {
248 if (!this.isValid()) {
249 return input != null ? this : NaN;
250 }
251  
252 // behaves the same as moment#day except
253 // as a getter, returns 7 instead of 0 (1-7 range instead of 0-6)
254 // as a setter, sunday should belong to the previous week.
255  
256 if (input != null) {
257 var weekday = parseIsoWeekday(input, this.localeData());
258 return this.day(this.day() % 7 ? weekday : weekday - 7);
259 } else {
260 return this.day() || 7;
261 }
262 }
263  
264 var defaultWeekdaysRegex = matchWord;
265 export function weekdaysRegex (isStrict) {
266 if (this._weekdaysParseExact) {
267 if (!hasOwnProp(this, '_weekdaysRegex')) {
268 computeWeekdaysParse.call(this);
269 }
270 if (isStrict) {
271 return this._weekdaysStrictRegex;
272 } else {
273 return this._weekdaysRegex;
274 }
275 } else {
276 if (!hasOwnProp(this, '_weekdaysRegex')) {
277 this._weekdaysRegex = defaultWeekdaysRegex;
278 }
279 return this._weekdaysStrictRegex && isStrict ?
280 this._weekdaysStrictRegex : this._weekdaysRegex;
281 }
282 }
283  
284 var defaultWeekdaysShortRegex = matchWord;
285 export function weekdaysShortRegex (isStrict) {
286 if (this._weekdaysParseExact) {
287 if (!hasOwnProp(this, '_weekdaysRegex')) {
288 computeWeekdaysParse.call(this);
289 }
290 if (isStrict) {
291 return this._weekdaysShortStrictRegex;
292 } else {
293 return this._weekdaysShortRegex;
294 }
295 } else {
296 if (!hasOwnProp(this, '_weekdaysShortRegex')) {
297 this._weekdaysShortRegex = defaultWeekdaysShortRegex;
298 }
299 return this._weekdaysShortStrictRegex && isStrict ?
300 this._weekdaysShortStrictRegex : this._weekdaysShortRegex;
301 }
302 }
303  
304 var defaultWeekdaysMinRegex = matchWord;
305 export function weekdaysMinRegex (isStrict) {
306 if (this._weekdaysParseExact) {
307 if (!hasOwnProp(this, '_weekdaysRegex')) {
308 computeWeekdaysParse.call(this);
309 }
310 if (isStrict) {
311 return this._weekdaysMinStrictRegex;
312 } else {
313 return this._weekdaysMinRegex;
314 }
315 } else {
316 if (!hasOwnProp(this, '_weekdaysMinRegex')) {
317 this._weekdaysMinRegex = defaultWeekdaysMinRegex;
318 }
319 return this._weekdaysMinStrictRegex && isStrict ?
320 this._weekdaysMinStrictRegex : this._weekdaysMinRegex;
321 }
322 }
323  
324  
325 function computeWeekdaysParse () {
326 function cmpLenRev(a, b) {
327 return b.length - a.length;
328 }
329  
330 var minPieces = [], shortPieces = [], longPieces = [], mixedPieces = [],
331 i, mom, minp, shortp, longp;
332 for (i = 0; i < 7; i++) {
333 // make the regex if we don't have it already
334 mom = createUTC([2000, 1]).day(i);
335 minp = this.weekdaysMin(mom, '');
336 shortp = this.weekdaysShort(mom, '');
337 longp = this.weekdays(mom, '');
338 minPieces.push(minp);
339 shortPieces.push(shortp);
340 longPieces.push(longp);
341 mixedPieces.push(minp);
342 mixedPieces.push(shortp);
343 mixedPieces.push(longp);
344 }
345 // Sorting makes sure if one weekday (or abbr) is a prefix of another it
346 // will match the longer piece.
347 minPieces.sort(cmpLenRev);
348 shortPieces.sort(cmpLenRev);
349 longPieces.sort(cmpLenRev);
350 mixedPieces.sort(cmpLenRev);
351 for (i = 0; i < 7; i++) {
352 shortPieces[i] = regexEscape(shortPieces[i]);
353 longPieces[i] = regexEscape(longPieces[i]);
354 mixedPieces[i] = regexEscape(mixedPieces[i]);
355 }
356  
357 this._weekdaysRegex = new RegExp('^(' + mixedPieces.join('|') + ')', 'i');
358 this._weekdaysShortRegex = this._weekdaysRegex;
359 this._weekdaysMinRegex = this._weekdaysRegex;
360  
361 this._weekdaysStrictRegex = new RegExp('^(' + longPieces.join('|') + ')', 'i');
362 this._weekdaysShortStrictRegex = new RegExp('^(' + shortPieces.join('|') + ')', 'i');
363 this._weekdaysMinStrictRegex = new RegExp('^(' + minPieces.join('|') + ')', 'i');
364 }