Skip to content

Commit

Permalink
support today param in today inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
spencermountain committed Aug 28, 2020
1 parent bf23ffd commit a611956
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 31 deletions.
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ This project follows semVer, where:
* don't reverse dates for .every() method
-->

### v6.6.3
- support for 'today' param with null inputs
- support for 'today' param with 'today/tonight' inputs
- interpret empty-string input like null input (as now)

### v6.6.2

- [fix] for formatting when the output is 0 😓
Expand Down
18 changes: 5 additions & 13 deletions scratch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,9 @@ const spacetime = require('./src/index')
// let s = spacetime([2020, 1, 29])
// console.log(s.format())

let s = spacetime('jan 3 2019').startOf('year') //.minus(1, 'second')
let arr = s.every('day', s.endOf('year'))
console.log(arr.length)
arr = arr.slice(0, 5)
arr.forEach((d) => {
console.log(d.format('nice'))
})
let s = spacetime('now', null, { today: { year: 2012 } })
console.log(s.format())

// try to keep time of day in every?
let s = spacetime.today('Canada/Eastern').time('2:01pm')
let hours = s.every('week', s.add(1, 'year'))
hours.forEach((d) => {
console.log(d.format('time') + ' - ' + d.sunPosition().altitude)
})
// let a = spacetime(null, 'Canada/Eastern')
// let b = spacetime(Date.now(), 'Canada/Eastern')
// console.log(a.format(), b.format())
10 changes: 9 additions & 1 deletion src/input/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,15 @@ const parseInput = (s, input, givenTz) => {
}
//set tmp time
s.epoch = Date.now()
if (input === null || input === undefined) {
// overwrite tmp time with 'today' value, if exists
if (s._today && fns.isObject(s._today) && Object.keys(s._today).length > 0) {
let res = handleObject(s, today, defaults)
if (res.isValid()) {
s.epoch = res.epoch
}
}
// null input means 'now'
if (input === null || input === undefined || input === '') {
return s //k, we're good.
}
//support input of Date() object
Expand Down
43 changes: 26 additions & 17 deletions src/input/named-dates.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
// pull in 'today' data for the baseline moment
const getNow = function (s) {
s.epoch = Date.now()
Object.keys(s._today || {}).forEach((k) => {
if (typeof s[k] === 'function') {
s = s[k](s._today[k])
}
})
return s
}

const dates = {
now: s => {
s.epoch = Date.now()
return s
now: (s) => {
return getNow(s)
},
tonight: s => {
s.epoch = Date.now()
s = s.hour(18)
return s
today: (s) => {
return getNow(s)
},
today: s => {
s.epoch = Date.now()
tonight: (s) => {
s = getNow(s)
s = s.hour(18) //6pm
return s
},
tomorrow: s => {
s.epoch = Date.now()
tomorrow: (s) => {
s = getNow(s)
s = s.add(1, 'day')
s = s.startOf('day')
return s
},
yesterday: s => {
s.epoch = Date.now()
yesterday: (s) => {
s = getNow(s)
s = s.subtract(1, 'day')
s = s.startOf('day')
return s
},
christmas: s => {
let year = new Date().getFullYear()
christmas: (s) => {
let year = getNow(s).year()
s = s.set([year, 11, 25, 18, 0, 0]) // Dec 25
return s
},
'new years': s => {
let year = new Date().getFullYear()
'new years': (s) => {
let year = getNow(s).year()
s = s.set([year, 11, 31, 18, 0, 0]) // Dec 31
return s
}
Expand Down
10 changes: 10 additions & 0 deletions test/today.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ test('change assumed date', function (t) {
t.end()
})

test('null input w/ today', function (t) {
let s = spacetime(null, null, { today: { year: 2012, month: 2 } })
t.equal(s.format('nice-year'), 'Mar 1st, 2012', 'got date')

s = spacetime('', 'Canada/Eastern', { today: { year: 1999, month: 0, date: 28 } })
t.equal(s.format('nice-year'), 'Jan 28th, 1999', 'got date')

t.end()
})

test('today methods works', function (t) {
let today = {
date: 2,
Expand Down

0 comments on commit a611956

Please sign in to comment.