vuestic-ui
Version:
Vue 3 UI Framework
55 lines (54 loc) • 1.33 kB
JavaScript
import { ref } from "vue";
const parse = (text) => {
const m = text.match(/[0-9]{1,2}/g);
if (!m) {
return [];
}
return m.map((s) => Number(s));
};
const parsePeriod = (text) => {
const m = text.match(/pm|am/i);
if (!m) {
return null;
}
return Number(m[0].toLowerCase() === "pm");
};
const defaultParseDateFunction = (text) => {
const d = /* @__PURE__ */ new Date();
const [h, m, s] = parse(text);
const period = parsePeriod(text);
if (!h) {
return null;
}
const is12format = period !== null && h <= 12;
const isPM = is12format && !!period;
const fh = is12format ? h === 12 ? 0 : h : h;
d.setHours(Math.min(fh || 0, is12format ? 12 : 24) + (isPM ? 12 : 0));
d.setMinutes(Math.min(m || 0, 60));
d.setSeconds(Math.min(s || 0, 60));
return d;
};
const useTimeParser = (props) => {
const getParseDateFn = () => props.parse || defaultParseDateFunction;
const isValid = ref(true);
const parseDate = (text) => {
const parse3 = getParseDateFn();
const result = parse3(text);
if (!result) {
isValid.value = false;
}
return result;
};
const parse2 = (text) => {
isValid.value = true;
return parseDate(text);
};
return {
parse: parse2,
isValid
};
};
export {
useTimeParser as u
};
//# sourceMappingURL=time-text-parser.js.map