Debian Codegraph @

vendor/static/moment/src/lib/units/day-of-week.js

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