3cbda9df8fad4a2796149cc5895522b1bc2b5fb9605c35ace1abe9ede6dc5e663686376115f63a8dc63bca512b1f46f6a4f3e7cf90672ea2b2dac1e0f74fd2 12 KB

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