Skip to content

Commit

Permalink
If only the month and year are provided record start date as 1st day …
Browse files Browse the repository at this point in the history
…of the month, and end date has 1st day of next month
  • Loading branch information
kremio committed Oct 26, 2018
1 parent bd14a17 commit bce91bb
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 11 deletions.
51 changes: 40 additions & 11 deletions lib/report.js
Expand Up @@ -11,6 +11,18 @@ const factums = require('./factums')
const contexts = require('./contexts')


const isMonthAndYearOnly = (date) => {
const tokens = date.match(/^\s*([^0-9]+)\s+([0-9]{2,})/)

if( !tokens ){
return false
}

return {
month: germanMonths.nameToNumber( tokens[1].trim() ),
year: tokens[2].trim().length < 4 ? 2000 + Number( tokens[2].trim() ) : Number( tokens[2].trim() )
}
}

const parseDate = (date) => {

Expand Down Expand Up @@ -39,21 +51,38 @@ const parseDate = (date) => {
const parseH1 = (text) => {
const [date, ...title] = text.split("–")
let days
if( date.trim().length < 4 ){ //the days were separated with "–"
days = [date, title.shift()]
let dates

const yearAndMonth = isMonthAndYearOnly(date)
if( yearAndMonth ){
//Record the start of the month as the start date
dates = [ new Date( `${yearAndMonth.year}-${yearAndMonth.month}-01` ) ]

//Record the first day of the next month as the last date
yearAndMonth.month = (yearAndMonth.month + 1) % 12
if( yearAndMonth.month == 1 ){
yearAndMonth.year += 1
}

dates.push( new Date( `${yearAndMonth.year}-${yearAndMonth.month}-01` ) )
}else{
//(d./)?d. Monat YYYY
days = date.trim().split('/')
}
const {day, month, year} = parseDate( days.pop() )

//Create dates for all the days
const dates = days
.map( (d) => Number( d.replace('.','') ) )
.map( (d) => new Date( `${year}-${month}-${d}` ) )
if( date.trim().length < 4 ){ //the days were separated with "–"
days = [date, title.shift()]
}else{
//(d./)?d. Monat YYYY
days = date.trim().split('/')
}
const {day, month, year} = parseDate( days.pop() )

dates.push( new Date( `${year}-${month}-${day}` ) )
//Create dates for all the days
dates = days
.map( (d) => Number( d.replace('.','') ) )
.map( (d) => new Date( `${year}-${month}-${d}` ) )

dates.push( new Date( `${year}-${month}-${day}` ) )
}

return {
dates,
title: title.join("–").trim()
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/report.test.js
Expand Up @@ -26,6 +26,7 @@ const reportWithShortDateInSource = fs.readFileSync('./tests/samples/short_date_
const daysSeparatedWithEm = fs.readFileSync('./tests/samples/daysSeparatedWithEm.html', 'utf8')
const sourceWithMultipleDates = fs.readFileSync('./tests/samples/sourceWithMultipleDates.html', 'utf8')
const anonymousSource =fs.readFileSync('./tests/samples/anonymousSource.html', 'utf8')
const monthOnly = fs.readFileSync('./tests/samples/monthOnly.html', 'utf8')

request.mockImplementation((...args) => {
const cb = args.pop()
Expand Down Expand Up @@ -108,3 +109,15 @@ test( 'Add anonymous source if none available', async() => {
expect( validateSchema(result) ).toBeTruthy()
expect( result.sources[0].name ).toEqual('anonymous')
})

test( 'If only the month and year are provided record start date as 1st day of the month, and end date has 1st day of next month', async() => {
request.mockImplementationOnce((...args) => {
const cb = args.pop()
cb( null, {statusCode: 200}, monthOnly )
})
const result = await scrapeReport( 'https://domain.tld/path/to/page.html' )
expect( validateSchema(result) ).toBeTruthy()
expect( result.startDate ).toEqual( '2016-01-31T23:00:00.000Z' )
expect( result.endDate ).toEqual( '2016-02-29T23:00:00.000Z' )

})

0 comments on commit bce91bb

Please sign in to comment.