48cecb6af81c4442d8584eaf0a963a5be5014514ac7558beb65add9a10ecee6fe1e887788e20ae2eedbc089e86095fc3b474f1f8122184117d463e48ff5343 11 KB

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