Seeing a few people download the widget was really satisfying, and I was soon wondering what else I could publish. As a pragmatic engineer, it seemed clear to me what the next widget should do: If my first widget calculated the time since an event, the next should calculate the time until an event. I set out to create the companion to “Time Since” and improve upon the underlying code.
My first code design choice was to cut out the use of an Apple Script and calculated everything in one CoffeeScript file. It would be more efficient and easier to read.
My code began to look increasingly like spaghetti as I tried to achieve 6 key features:
The remaining features were added by using a combination of if statements and arrays. The if statements simply ask if an amount is equal to zero. If not, then it’s appended to an array of terms to include in the output. At the end of this code chunk it’s possible to ask how many non-zero terms to include in the output. An array with length one less than the number of output terms is created and filled with commas with an ‘and’ in the final position.
The two arrays are combined in the correct order by looping through the index of the longer array and appending each term to a final array. The final array is the output.
‘Time Until’ can be downloaded from the Übersicht widgets gallery. I think another useful feature would be an option to specify the output only in terms of a chosen amount, such as weeks or days. Maybe I’ll do that in the future.