index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. module.exports = (function() {
  2. var __MODS__ = {};
  3. var __DEFINE__ = function(modId, func, req) { var m = { exports: {}, _tempexports: {} }; __MODS__[modId] = { status: 0, func: func, req: req, m: m }; };
  4. var __REQUIRE__ = function(modId, source) { if(!__MODS__[modId]) return require(source); if(!__MODS__[modId].status) { var m = __MODS__[modId].m; m._exports = m._tempexports; var desp = Object.getOwnPropertyDescriptor(m, "exports"); if (desp && desp.configurable) Object.defineProperty(m, "exports", { set: function (val) { if(typeof val === "object" && val !== m._exports) { m._exports.__proto__ = val.__proto__; Object.keys(val).forEach(function (k) { m._exports[k] = val[k]; }); } m._tempexports = val }, get: function () { return m._tempexports; } }); __MODS__[modId].status = 1; __MODS__[modId].func(__MODS__[modId].req, m, m.exports); } return __MODS__[modId].m.exports; };
  5. var __REQUIRE_WILDCARD__ = function(obj) { if(obj && obj.__esModule) { return obj; } else { var newObj = {}; if(obj != null) { for(var k in obj) { if (Object.prototype.hasOwnProperty.call(obj, k)) newObj[k] = obj[k]; } } newObj.default = obj; return newObj; } };
  6. var __REQUIRE_DEFAULT__ = function(obj) { return obj && obj.__esModule ? obj.default : obj; };
  7. __DEFINE__(1658993502782, function(require, module, exports) {
  8. (function (global, factory) {
  9. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  10. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  11. (factory((global.fecha = {})));
  12. }(this, (function (exports) {
  13. var token = /d{1,4}|M{1,4}|YY(?:YY)?|S{1,3}|Do|ZZ|Z|([HhMsDm])\1?|[aA]|"[^"]*"|'[^']*'/g;
  14. var twoDigitsOptional = "[1-9]\\d?";
  15. var twoDigits = "\\d\\d";
  16. var threeDigits = "\\d{3}";
  17. var fourDigits = "\\d{4}";
  18. var word = "[^\\s]+";
  19. var literal = /\[([^]*?)\]/gm;
  20. function shorten(arr, sLen) {
  21. var newArr = [];
  22. for (var i = 0, len = arr.length; i < len; i++) {
  23. newArr.push(arr[i].substr(0, sLen));
  24. }
  25. return newArr;
  26. }
  27. var monthUpdate = function (arrName) { return function (v, i18n) {
  28. var lowerCaseArr = i18n[arrName].map(function (v) { return v.toLowerCase(); });
  29. var index = lowerCaseArr.indexOf(v.toLowerCase());
  30. if (index > -1) {
  31. return index;
  32. }
  33. return null;
  34. }; };
  35. function assign(origObj) {
  36. var args = [];
  37. for (var _i = 1; _i < arguments.length; _i++) {
  38. args[_i - 1] = arguments[_i];
  39. }
  40. for (var _a = 0, args_1 = args; _a < args_1.length; _a++) {
  41. var obj = args_1[_a];
  42. for (var key in obj) {
  43. // @ts-ignore ex
  44. origObj[key] = obj[key];
  45. }
  46. }
  47. return origObj;
  48. }
  49. var dayNames = [
  50. "Sunday",
  51. "Monday",
  52. "Tuesday",
  53. "Wednesday",
  54. "Thursday",
  55. "Friday",
  56. "Saturday"
  57. ];
  58. var monthNames = [
  59. "January",
  60. "February",
  61. "March",
  62. "April",
  63. "May",
  64. "June",
  65. "July",
  66. "August",
  67. "September",
  68. "October",
  69. "November",
  70. "December"
  71. ];
  72. var monthNamesShort = shorten(monthNames, 3);
  73. var dayNamesShort = shorten(dayNames, 3);
  74. var defaultI18n = {
  75. dayNamesShort: dayNamesShort,
  76. dayNames: dayNames,
  77. monthNamesShort: monthNamesShort,
  78. monthNames: monthNames,
  79. amPm: ["am", "pm"],
  80. DoFn: function (dayOfMonth) {
  81. return (dayOfMonth +
  82. ["th", "st", "nd", "rd"][dayOfMonth % 10 > 3
  83. ? 0
  84. : ((dayOfMonth - (dayOfMonth % 10) !== 10 ? 1 : 0) * dayOfMonth) % 10]);
  85. }
  86. };
  87. var globalI18n = assign({}, defaultI18n);
  88. var setGlobalDateI18n = function (i18n) {
  89. return (globalI18n = assign(globalI18n, i18n));
  90. };
  91. var regexEscape = function (str) {
  92. return str.replace(/[|\\{()[^$+*?.-]/g, "\\$&");
  93. };
  94. var pad = function (val, len) {
  95. if (len === void 0) { len = 2; }
  96. val = String(val);
  97. while (val.length < len) {
  98. val = "0" + val;
  99. }
  100. return val;
  101. };
  102. var formatFlags = {
  103. D: function (dateObj) { return String(dateObj.getDate()); },
  104. DD: function (dateObj) { return pad(dateObj.getDate()); },
  105. Do: function (dateObj, i18n) {
  106. return i18n.DoFn(dateObj.getDate());
  107. },
  108. d: function (dateObj) { return String(dateObj.getDay()); },
  109. dd: function (dateObj) { return pad(dateObj.getDay()); },
  110. ddd: function (dateObj, i18n) {
  111. return i18n.dayNamesShort[dateObj.getDay()];
  112. },
  113. dddd: function (dateObj, i18n) {
  114. return i18n.dayNames[dateObj.getDay()];
  115. },
  116. M: function (dateObj) { return String(dateObj.getMonth() + 1); },
  117. MM: function (dateObj) { return pad(dateObj.getMonth() + 1); },
  118. MMM: function (dateObj, i18n) {
  119. return i18n.monthNamesShort[dateObj.getMonth()];
  120. },
  121. MMMM: function (dateObj, i18n) {
  122. return i18n.monthNames[dateObj.getMonth()];
  123. },
  124. YY: function (dateObj) {
  125. return pad(String(dateObj.getFullYear()), 4).substr(2);
  126. },
  127. YYYY: function (dateObj) { return pad(dateObj.getFullYear(), 4); },
  128. h: function (dateObj) { return String(dateObj.getHours() % 12 || 12); },
  129. hh: function (dateObj) { return pad(dateObj.getHours() % 12 || 12); },
  130. H: function (dateObj) { return String(dateObj.getHours()); },
  131. HH: function (dateObj) { return pad(dateObj.getHours()); },
  132. m: function (dateObj) { return String(dateObj.getMinutes()); },
  133. mm: function (dateObj) { return pad(dateObj.getMinutes()); },
  134. s: function (dateObj) { return String(dateObj.getSeconds()); },
  135. ss: function (dateObj) { return pad(dateObj.getSeconds()); },
  136. S: function (dateObj) {
  137. return String(Math.round(dateObj.getMilliseconds() / 100));
  138. },
  139. SS: function (dateObj) {
  140. return pad(Math.round(dateObj.getMilliseconds() / 10), 2);
  141. },
  142. SSS: function (dateObj) { return pad(dateObj.getMilliseconds(), 3); },
  143. a: function (dateObj, i18n) {
  144. return dateObj.getHours() < 12 ? i18n.amPm[0] : i18n.amPm[1];
  145. },
  146. A: function (dateObj, i18n) {
  147. return dateObj.getHours() < 12
  148. ? i18n.amPm[0].toUpperCase()
  149. : i18n.amPm[1].toUpperCase();
  150. },
  151. ZZ: function (dateObj) {
  152. var offset = dateObj.getTimezoneOffset();
  153. return ((offset > 0 ? "-" : "+") +
  154. pad(Math.floor(Math.abs(offset) / 60) * 100 + (Math.abs(offset) % 60), 4));
  155. },
  156. Z: function (dateObj) {
  157. var offset = dateObj.getTimezoneOffset();
  158. return ((offset > 0 ? "-" : "+") +
  159. pad(Math.floor(Math.abs(offset) / 60), 2) +
  160. ":" +
  161. pad(Math.abs(offset) % 60, 2));
  162. }
  163. };
  164. var monthParse = function (v) { return +v - 1; };
  165. var emptyDigits = [null, twoDigitsOptional];
  166. var emptyWord = [null, word];
  167. var amPm = [
  168. "isPm",
  169. word,
  170. function (v, i18n) {
  171. var val = v.toLowerCase();
  172. if (val === i18n.amPm[0]) {
  173. return 0;
  174. }
  175. else if (val === i18n.amPm[1]) {
  176. return 1;
  177. }
  178. return null;
  179. }
  180. ];
  181. var timezoneOffset = [
  182. "timezoneOffset",
  183. "[^\\s]*?[\\+\\-]\\d\\d:?\\d\\d|[^\\s]*?Z?",
  184. function (v) {
  185. var parts = (v + "").match(/([+-]|\d\d)/gi);
  186. if (parts) {
  187. var minutes = +parts[1] * 60 + parseInt(parts[2], 10);
  188. return parts[0] === "+" ? minutes : -minutes;
  189. }
  190. return 0;
  191. }
  192. ];
  193. var parseFlags = {
  194. D: ["day", twoDigitsOptional],
  195. DD: ["day", twoDigits],
  196. Do: ["day", twoDigitsOptional + word, function (v) { return parseInt(v, 10); }],
  197. M: ["month", twoDigitsOptional, monthParse],
  198. MM: ["month", twoDigits, monthParse],
  199. YY: [
  200. "year",
  201. twoDigits,
  202. function (v) {
  203. var now = new Date();
  204. var cent = +("" + now.getFullYear()).substr(0, 2);
  205. return +("" + (+v > 68 ? cent - 1 : cent) + v);
  206. }
  207. ],
  208. h: ["hour", twoDigitsOptional, undefined, "isPm"],
  209. hh: ["hour", twoDigits, undefined, "isPm"],
  210. H: ["hour", twoDigitsOptional],
  211. HH: ["hour", twoDigits],
  212. m: ["minute", twoDigitsOptional],
  213. mm: ["minute", twoDigits],
  214. s: ["second", twoDigitsOptional],
  215. ss: ["second", twoDigits],
  216. YYYY: ["year", fourDigits],
  217. S: ["millisecond", "\\d", function (v) { return +v * 100; }],
  218. SS: ["millisecond", twoDigits, function (v) { return +v * 10; }],
  219. SSS: ["millisecond", threeDigits],
  220. d: emptyDigits,
  221. dd: emptyDigits,
  222. ddd: emptyWord,
  223. dddd: emptyWord,
  224. MMM: ["month", word, monthUpdate("monthNamesShort")],
  225. MMMM: ["month", word, monthUpdate("monthNames")],
  226. a: amPm,
  227. A: amPm,
  228. ZZ: timezoneOffset,
  229. Z: timezoneOffset
  230. };
  231. // Some common format strings
  232. var globalMasks = {
  233. default: "ddd MMM DD YYYY HH:mm:ss",
  234. shortDate: "M/D/YY",
  235. mediumDate: "MMM D, YYYY",
  236. longDate: "MMMM D, YYYY",
  237. fullDate: "dddd, MMMM D, YYYY",
  238. isoDate: "YYYY-MM-DD",
  239. isoDateTime: "YYYY-MM-DDTHH:mm:ssZ",
  240. shortTime: "HH:mm",
  241. mediumTime: "HH:mm:ss",
  242. longTime: "HH:mm:ss.SSS"
  243. };
  244. var setGlobalDateMasks = function (masks) { return assign(globalMasks, masks); };
  245. /***
  246. * Format a date
  247. * @method format
  248. * @param {Date|number} dateObj
  249. * @param {string} mask Format of the date, i.e. 'mm-dd-yy' or 'shortDate'
  250. * @returns {string} Formatted date string
  251. */
  252. var format = function (dateObj, mask, i18n) {
  253. if (mask === void 0) { mask = globalMasks["default"]; }
  254. if (i18n === void 0) { i18n = {}; }
  255. if (typeof dateObj === "number") {
  256. dateObj = new Date(dateObj);
  257. }
  258. if (Object.prototype.toString.call(dateObj) !== "[object Date]" ||
  259. isNaN(dateObj.getTime())) {
  260. throw new Error("Invalid Date pass to format");
  261. }
  262. mask = globalMasks[mask] || mask;
  263. var literals = [];
  264. // Make literals inactive by replacing them with @@@
  265. mask = mask.replace(literal, function ($0, $1) {
  266. literals.push($1);
  267. return "@@@";
  268. });
  269. var combinedI18nSettings = assign(assign({}, globalI18n), i18n);
  270. // Apply formatting rules
  271. mask = mask.replace(token, function ($0) {
  272. return formatFlags[$0](dateObj, combinedI18nSettings);
  273. });
  274. // Inline literal values back into the formatted value
  275. return mask.replace(/@@@/g, function () { return literals.shift(); });
  276. };
  277. /**
  278. * Parse a date string into a Javascript Date object /
  279. * @method parse
  280. * @param {string} dateStr Date string
  281. * @param {string} format Date parse format
  282. * @param {i18n} I18nSettingsOptional Full or subset of I18N settings
  283. * @returns {Date|null} Returns Date object. Returns null what date string is invalid or doesn't match format
  284. */
  285. function parse(dateStr, format, i18n) {
  286. if (i18n === void 0) { i18n = {}; }
  287. if (typeof format !== "string") {
  288. throw new Error("Invalid format in fecha parse");
  289. }
  290. // Check to see if the format is actually a mask
  291. format = globalMasks[format] || format;
  292. // Avoid regular expression denial of service, fail early for really long strings
  293. // https://www.owasp.org/index.php/Regular_expression_Denial_of_Service_-_ReDoS
  294. if (dateStr.length > 1000) {
  295. return null;
  296. }
  297. // Default to the beginning of the year.
  298. var today = new Date();
  299. var dateInfo = {
  300. year: today.getFullYear(),
  301. month: 0,
  302. day: 1,
  303. hour: 0,
  304. minute: 0,
  305. second: 0,
  306. millisecond: 0,
  307. isPm: null,
  308. timezoneOffset: null
  309. };
  310. var parseInfo = [];
  311. var literals = [];
  312. // Replace all the literals with @@@. Hopefully a string that won't exist in the format
  313. var newFormat = format.replace(literal, function ($0, $1) {
  314. literals.push(regexEscape($1));
  315. return "@@@";
  316. });
  317. var specifiedFields = {};
  318. var requiredFields = {};
  319. // Change every token that we find into the correct regex
  320. newFormat = regexEscape(newFormat).replace(token, function ($0) {
  321. var info = parseFlags[$0];
  322. var field = info[0], regex = info[1], requiredField = info[3];
  323. // Check if the person has specified the same field twice. This will lead to confusing results.
  324. if (specifiedFields[field]) {
  325. throw new Error("Invalid format. " + field + " specified twice in format");
  326. }
  327. specifiedFields[field] = true;
  328. // Check if there are any required fields. For instance, 12 hour time requires AM/PM specified
  329. if (requiredField) {
  330. requiredFields[requiredField] = true;
  331. }
  332. parseInfo.push(info);
  333. return "(" + regex + ")";
  334. });
  335. // Check all the required fields are present
  336. Object.keys(requiredFields).forEach(function (field) {
  337. if (!specifiedFields[field]) {
  338. throw new Error("Invalid format. " + field + " is required in specified format");
  339. }
  340. });
  341. // Add back all the literals after
  342. newFormat = newFormat.replace(/@@@/g, function () { return literals.shift(); });
  343. // Check if the date string matches the format. If it doesn't return null
  344. var matches = dateStr.match(new RegExp(newFormat, "i"));
  345. if (!matches) {
  346. return null;
  347. }
  348. var combinedI18nSettings = assign(assign({}, globalI18n), i18n);
  349. // For each match, call the parser function for that date part
  350. for (var i = 1; i < matches.length; i++) {
  351. var _a = parseInfo[i - 1], field = _a[0], parser = _a[2];
  352. var value = parser
  353. ? parser(matches[i], combinedI18nSettings)
  354. : +matches[i];
  355. // If the parser can't make sense of the value, return null
  356. if (value == null) {
  357. return null;
  358. }
  359. dateInfo[field] = value;
  360. }
  361. if (dateInfo.isPm === 1 && dateInfo.hour != null && +dateInfo.hour !== 12) {
  362. dateInfo.hour = +dateInfo.hour + 12;
  363. }
  364. else if (dateInfo.isPm === 0 && +dateInfo.hour === 12) {
  365. dateInfo.hour = 0;
  366. }
  367. var dateWithoutTZ = new Date(dateInfo.year, dateInfo.month, dateInfo.day, dateInfo.hour, dateInfo.minute, dateInfo.second, dateInfo.millisecond);
  368. var validateFields = [
  369. ["month", "getMonth"],
  370. ["day", "getDate"],
  371. ["hour", "getHours"],
  372. ["minute", "getMinutes"],
  373. ["second", "getSeconds"]
  374. ];
  375. for (var i = 0, len = validateFields.length; i < len; i++) {
  376. // Check to make sure the date field is within the allowed range. Javascript dates allows values
  377. // outside the allowed range. If the values don't match the value was invalid
  378. if (specifiedFields[validateFields[i][0]] &&
  379. dateInfo[validateFields[i][0]] !== dateWithoutTZ[validateFields[i][1]]()) {
  380. return null;
  381. }
  382. }
  383. if (dateInfo.timezoneOffset == null) {
  384. return dateWithoutTZ;
  385. }
  386. return new Date(Date.UTC(dateInfo.year, dateInfo.month, dateInfo.day, dateInfo.hour, dateInfo.minute - dateInfo.timezoneOffset, dateInfo.second, dateInfo.millisecond));
  387. }
  388. var fecha = {
  389. format: format,
  390. parse: parse,
  391. defaultI18n: defaultI18n,
  392. setGlobalDateI18n: setGlobalDateI18n,
  393. setGlobalDateMasks: setGlobalDateMasks
  394. };
  395. exports.assign = assign;
  396. exports.default = fecha;
  397. exports.format = format;
  398. exports.parse = parse;
  399. exports.defaultI18n = defaultI18n;
  400. exports.setGlobalDateI18n = setGlobalDateI18n;
  401. exports.setGlobalDateMasks = setGlobalDateMasks;
  402. Object.defineProperty(exports, '__esModule', { value: true });
  403. })));
  404. //# sourceMappingURL=fecha.umd.js.map
  405. }, function(modId) {var map = {}; return __REQUIRE__(map[modId], modId); })
  406. return __REQUIRE__(1658993502782);
  407. })()
  408. //miniprogram-npm-outsideDeps=[]
  409. //# sourceMappingURL=index.js.map