isDate.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isDate;
  6. var _merge = _interopRequireDefault(require("./util/merge"));
  7. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  9. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  10. function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  11. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  12. function _createForOfIteratorHelper(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = o[Symbol.iterator](); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
  13. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  14. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  15. var default_date_options = {
  16. format: 'YYYY/MM/DD',
  17. delimiters: ['/', '-'],
  18. strictMode: false
  19. };
  20. function isValidFormat(format) {
  21. return /(^(y{4}|y{2})[.\/-](m{1,2})[.\/-](d{1,2})$)|(^(m{1,2})[.\/-](d{1,2})[.\/-]((y{4}|y{2})$))|(^(d{1,2})[.\/-](m{1,2})[.\/-]((y{4}|y{2})$))/gi.test(format);
  22. }
  23. function zip(date, format) {
  24. var zippedArr = [],
  25. len = Math.min(date.length, format.length);
  26. for (var i = 0; i < len; i++) {
  27. zippedArr.push([date[i], format[i]]);
  28. }
  29. return zippedArr;
  30. }
  31. function isDate(input, options) {
  32. if (typeof options === 'string') {
  33. // Allow backward compatbility for old format isDate(input [, format])
  34. options = (0, _merge.default)({
  35. format: options
  36. }, default_date_options);
  37. } else {
  38. options = (0, _merge.default)(options, default_date_options);
  39. }
  40. if (typeof input === 'string' && isValidFormat(options.format)) {
  41. var formatDelimiter = options.delimiters.find(function (delimiter) {
  42. return options.format.indexOf(delimiter) !== -1;
  43. });
  44. var dateDelimiter = options.strictMode ? formatDelimiter : options.delimiters.find(function (delimiter) {
  45. return input.indexOf(delimiter) !== -1;
  46. });
  47. var dateAndFormat = zip(input.split(dateDelimiter), options.format.toLowerCase().split(formatDelimiter));
  48. var dateObj = {};
  49. var _iterator = _createForOfIteratorHelper(dateAndFormat),
  50. _step;
  51. try {
  52. for (_iterator.s(); !(_step = _iterator.n()).done;) {
  53. var _step$value = _slicedToArray(_step.value, 2),
  54. dateWord = _step$value[0],
  55. formatWord = _step$value[1];
  56. if (dateWord.length !== formatWord.length) {
  57. return false;
  58. }
  59. dateObj[formatWord.charAt(0)] = dateWord;
  60. }
  61. } catch (err) {
  62. _iterator.e(err);
  63. } finally {
  64. _iterator.f();
  65. }
  66. return new Date("".concat(dateObj.m, "/").concat(dateObj.d, "/").concat(dateObj.y)).getDate() === +dateObj.d;
  67. }
  68. if (!options.strictMode) {
  69. return Object.prototype.toString.call(input) === '[object Date]' && isFinite(input);
  70. }
  71. return false;
  72. }
  73. module.exports = exports.default;
  74. module.exports.default = exports.default;