-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.coffee
53 lines (46 loc) · 1.77 KB
/
helpers.coffee
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
moment = require('moment')
METERS_IN_A_MILE = 1609.344
HOUR = 3600
MINUTE = 60
conversions =
metersToMi: (meters) ->
(meters / METERS_IN_A_MILE).toFixed(2)
secondsToHms: (seconds) ->
hours = Math.floor(seconds / HOUR)
seconds = seconds % HOUR
minutes = Math.floor(seconds / MINUTE)
seconds = Math.floor(seconds % MINUTE)
minutes = '0' + minutes if minutes < 10
seconds = '0' + seconds if seconds < 10
strParts = [ minutes, seconds ]
strParts.unshift(hours) if hours > 0
strParts.join(':')
conversions['pace'] = (meters, seconds) ->
miles = meters / METERS_IN_A_MILE
this.secondsToHms(seconds / miles)
viewHelpers =
formatDateTime: (rkTimeString) ->
# RunKeeper hands us times like "Tue, 1 Mar 2011 07:00:00"
# We want to retur ones like "Mar 1, 2011 (Tues) at 1:50PM"
moment(rkTimeString).format('MMM D, YYYY (ddd) @ h:mmA')
formatTimeDuration: (rkTimeString, seconds) ->
start = moment(rkTimeString)
end = start.clone().add('seconds', seconds)
start.format('hh:mmA') + ' - ' + end.format('hh:mmA')
cellToolTip: (act) ->
dist = act['total_distance']
dur = act['duration']
tips = []
tips.push("#{ conversions.metersToMi(dist) } miles (#{ conversions.pace(dist, dur) } min/mi)")
tips.push(viewHelpers.formatTimeDuration(act['start_time'], act['duration']))
tips.join('<br/>')
monthToClassName: (monthName) ->
monthName.replace(/\s/, '').toLowerCase()
exports.helpers =
metersToMi : conversions.metersToMi
secondsToHms : conversions.secondsToHms
formatDateTime : viewHelpers.formatDateTime
formatTime : viewHelpers.formatTime
cellToolTip : viewHelpers.cellToolTip
pace : conversions.pace
monthToClassName : viewHelpers.monthToClassName