18f829241a5015be45623b8e35c8e5ec164ef7d1c564686f6220511bfe45450990d6654133b1940a8d4c775cd3248a9d228388731b52aa2ecda28f0c5e44ac 12 KB

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