isTime.js 785 B

12345678910111213141516171819202122
  1. import merge from './util/merge';
  2. var default_time_options = {
  3. hourFormat: 'hour24',
  4. mode: 'default'
  5. };
  6. var formats = {
  7. hour24: {
  8. "default": /^([01]?[0-9]|2[0-3]):([0-5][0-9])$/,
  9. withSeconds: /^([01]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/,
  10. withOptionalSeconds: /^([01]?[0-9]|2[0-3]):([0-5][0-9])(?::([0-5][0-9]))?$/
  11. },
  12. hour12: {
  13. "default": /^(0?[1-9]|1[0-2]):([0-5][0-9]) (A|P)M$/,
  14. withSeconds: /^(0?[1-9]|1[0-2]):([0-5][0-9]):([0-5][0-9]) (A|P)M$/,
  15. withOptionalSeconds: /^(0?[1-9]|1[0-2]):([0-5][0-9])(?::([0-5][0-9]))? (A|P)M$/
  16. }
  17. };
  18. export default function isTime(input, options) {
  19. options = merge(options, default_time_options);
  20. if (typeof input !== 'string') return false;
  21. return formats[options.hourFormat][options.mode].test(input);
  22. }