startOfDay: if between midnight and custom day start, jump back to previous day

This commit is contained in:
Cole Gleason 2014-01-08 22:26:43 -06:00
parent d9e8c53d9a
commit 3bd81abda3
2 changed files with 6 additions and 2 deletions

View file

@ -31,7 +31,10 @@ api.startOfWeek = api.startOfWeek = (options={}) ->
api.startOfDay = (options={}) ->
o = sanitizeOptions(options)
moment(o.now).startOf('day').add('h', o.dayStart)
dayStart = moment(o.now).startOf('day').add('h', o.dayStart)
# if between midnight and Custom Day Start, jump back to previous day
dayStart.subtract('day', 1) if moment(o.now).isBefore(dayStart)
return dayStart
dayMapping = {0:'su',1:'m',2:'t',3:'w',4:'th',5:'f',6:'s'}
@ -1239,4 +1242,3 @@ api.wrap = (user, main=true) ->
get: ->
tasks = user.habits.concat(user.dailys).concat(user.todos).concat(user.rewards)
_.object(_.pluck(tasks, "id"), tasks)

View file

@ -487,6 +487,8 @@ describe 'Helper', ->
expect(shared.startOfDay({now: new Date(2013, 0, 1, 0)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00'
expect(shared.startOfDay({now: new Date(2013, 0, 1, 5)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00'
expect(shared.startOfDay({now: new Date(2013, 0, 1, 23, 59, 59)}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 00:00'
# between midnight and custom day start
expect(shared.startOfDay({now: new Date(2013, 0, 2, 2), dayStart: 4}).format('YYYY-MM-DD HH:mm')).to.eql '2013-01-01 04:00'
it 'counts pets', ->
pets = {}