-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_input.js
executable file
·158 lines (135 loc) · 4.27 KB
/
parse_input.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env osascript -l JavaScript
function run(argv) {
ObjC.import('stdlib');
const defaultDuration = $.getenv('default_duration');
const presetDuration1 = $.getenv('preset_duration_1');
const presetDuration2 = $.getenv('preset_duration_2');
const MAX_DELAY_IN_SECONDS = 60 * 60 * 2; // two hours
const ACCEPTED_UNITS_SECONDS = ['s', 'sec', 'secs', 'second', 'seconds'];
const ACCEPTED_UNITS_MINUTES = ['', 'm', 'min', 'mins', 'minute', 'minutes'];
const ACCEPTED_UNITS_HOURS = ['h', 'hr', 'hrs', 'hour', 'hours'];
const inputToTimeMap = (input) => {
const times = [...(input || '').trim().matchAll(/(\d*\.?\d+)\s*(\w*)/ig)];
return times.reduce((res, [_, digits, units]) => {
const number = Number(digits);
if (ACCEPTED_UNITS_SECONDS.includes(units)) {
res.seconds = number > 0 && number;
}
if (ACCEPTED_UNITS_MINUTES.includes(units)) {
res.minutes = number > 0 && number;
}
if (ACCEPTED_UNITS_HOURS.includes(units)) {
res.hours = number > 0 && number;
}
return res;
}, {});
};
const isValidTimeMap = (timeMap) => !!timeMap.seconds || !!timeMap.minutes || !!timeMap.hours;
const timeMapToReadableTime = (timeMap) => {
const readableTime = [];
const pr = new Intl.PluralRules('en-US');
const endings = new Map([
['one', ''],
['two', 's'],
['few', 's'],
['other', 's'],
]);
const pluralizeUnits = (n, unit) => {
const rule = pr.select(n);
const ending = endings.get(rule);
return `${unit}${ending}`;
};
if (timeMap.hours) {
readableTime.push(`${timeMap.hours} ${pluralizeUnits(timeMap.hours, 'hour')}`);
}
if (timeMap.minutes) {
readableTime.push(`${timeMap.minutes} ${pluralizeUnits(timeMap.minutes, 'minute')}`);
}
if (timeMap.seconds) {
readableTime.push(`${timeMap.seconds} ${pluralizeUnits(timeMap.seconds, 'second')}`);
}
return new Intl.ListFormat('en', { style: 'long', type: 'conjunction' }).format(readableTime);
};
const timeMapToSeconds = (timeMap) => {
return Object.entries(timeMap).reduce((seconds, [unit, amount]) => {
switch (unit) {
case 'hours':
seconds += amount * 60 * 60;
break;
case 'minutes':
seconds += amount * 60;
break;
case 'seconds':
seconds += amount;
break;
default:
break;
}
return seconds;
}, 0);
};
const calculateFireTime = (seconds) => {
const options = {
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: false,
};
const showTime = Date.now() + seconds * 1000;
return new Intl.DateTimeFormat('en-US', options).format(showTime);
};
const createArgumentItem = () => {
const timeMap = inputToTimeMap(argv[0] || defaultDuration);
const seconds = timeMapToSeconds(timeMap);
const readableTime = timeMapToReadableTime(timeMap);
let title = '';
let subtitle = '';
if (!argv[0]) {
title = 'Set timer...';
subtitle = `Hit ↵ to set to ${readableTime} or provide duration`;
} else if (isValidTimeMap(timeMap)) {
if (seconds <= MAX_DELAY_IN_SECONDS) {
title = `Set timer for ${readableTime}`;
subtitle = `Will fire at ${calculateFireTime(seconds)}`;
} else {
title = 'Too long delay!';
}
} else {
title = 'Can\'t understand that!';
}
return {
uid: 'timer',
title,
subtitle,
arg: seconds,
variables: {
'timer_seconds': seconds,
},
};
}
const createPresetItem = (input, uid) => {
const timeMap = inputToTimeMap(input || defaultDuration);
const seconds = timeMapToSeconds(timeMap);
const readableTime = timeMapToReadableTime(timeMap);
return {
uid,
title: `Set timer for ${readableTime}`,
subtitle: `Will fire at ${calculateFireTime(seconds)}`,
arg: seconds,
variables: {
'timer_seconds': seconds,
},
};
}
const items = [
createArgumentItem(),
];
if (!argv[0]) {
items.push(createPresetItem(presetDuration1, 'timer-preset-1'));
items.push(createPresetItem(presetDuration2, 'timer-preset-2'));
}
return JSON.stringify({
rerun: 1,
items,
});
}