PluginProbe
The Innovative Form Builder – IvyForms / 0.6.1-backup
The Innovative Form Builder – IvyForms v0.6.1-backup
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / frontend / src / composables / useTimeField.ts

useTimeField.ts in The Innovative Form Builder – IvyForms 0.6.1-backup, at frontend/src/composables/useTimeField.ts

83 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Composable function for time field logic
3 */
4 export function useTimeField() {
5 function parseTimeString(str: string) {
6 if (!str) return { hours: '', minutes: '', amPm: '' }
7 const fullMatch = str.match(/^(\d{2}):?(\d{2})?(?:\s*(AM|PM))?$/)
8 const minutesAmPmMatch = str.match(/^:(\d{2})\s*(AM|PM)$/)
9 const hoursMatch = str.match(/^\d{2}$/)
10 const minutesMatch = str.match(/^:(\d{2})$/)
11 const ampmMatch = str.match(/^(AM|PM)$/)
12 if (fullMatch) {
13 return {
14 hours: fullMatch[1] || '',
15 minutes: fullMatch[2] || '',
16 amPm: fullMatch[3] || '',
17 }
18 }
19 if (minutesAmPmMatch) {
20 return {
21 hours: '',
22 minutes: minutesAmPmMatch[1],
23 amPm: minutesAmPmMatch[2],
24 }
25 }
26 if (hoursMatch) {
27 return {
28 hours: str,
29 minutes: '',
30 amPm: '',
31 }
32 }
33 if (minutesMatch) {
34 return {
35 hours: '',
36 minutes: minutesMatch[1],
37 amPm: '',
38 }
39 }
40 if (ampmMatch) {
41 return {
42 hours: '',
43 minutes: '',
44 amPm: ampmMatch[1],
45 }
46 }
47 return { hours: '', minutes: '', amPm: '' }
48 }
49
50 function formatTimeString(hours: string, minutes: string, amPm: string) {
51 if (!hours && !minutes && amPm) return amPm
52 if (!hours && minutes && amPm) return `:${minutes} ${amPm}`
53 if (hours && minutes) return amPm ? `${hours}:${minutes} ${amPm}` : `${hours}:${minutes}`
54 if (hours) return hours
55 if (minutes) return `:${minutes}`
56 return ''
57 }
58
59 function getAmPmFromHours(hours: string): 'AM' | 'PM' {
60 const h = parseInt(hours, 10)
61 return h >= 12 ? 'PM' : 'AM'
62 }
63
64 function to24hFormat(time: string): string {
65 if (!time) return ''
66 const match = time.match(/^(\d{1,2}):(\d{2})\s*(AM|PM)?$/i)
67 if (!match) return time
68 let hours = parseInt(match[1], 10)
69 const minutes = match[2]
70 const ampm = match[3]?.toUpperCase()
71 if (ampm === 'PM' && hours < 12) hours += 12
72 if (ampm === 'AM' && hours === 12) hours = 0
73 return `${hours.toString().padStart(2, '0')}:${minutes}`
74 }
75
76 return {
77 parseTimeString,
78 formatTimeString,
79 getAmPmFromHours,
80 to24hFormat,
81 }
82 }
83