Skip to content

Commit

Permalink
If only the year is provided record start date as 1st january of the …
Browse files Browse the repository at this point in the history
…year, and end date has 31st december of that year
  • Loading branch information
kremio committed Oct 26, 2018
1 parent bce91bb commit 0c12480
Show file tree
Hide file tree
Showing 3 changed files with 278 additions and 5 deletions.
30 changes: 25 additions & 5 deletions lib/report.js
Expand Up @@ -10,6 +10,17 @@ const motives = require('./motives')
const factums = require('./factums')
const contexts = require('./contexts')

const fourDigitYear = ( year ) => year.trim().length < 4 ? 2000 + Number( year.trim() ) : Number( year.trim() )

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

if( !tokens ){
return false
}

return fourDigitYear( tokens[1] )
}

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

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

Expand All @@ -32,7 +43,7 @@ const parseDate = (date) => {
return {
day: Number( day.replace('.','') ),
month: Number( month.replace('.','') ),
year: year.length < 4 ? 2000 + Number(year) : Number(year)
year: fourDigitYear(year)
}
}

Expand All @@ -41,7 +52,7 @@ const parseDate = (date) => {
return {
day: Number( day.replace('.','') ),
month: germanMonths.nameToNumber(month),
year: Number(year)
year: fourDigitYear(year)
}
}

Expand All @@ -53,8 +64,17 @@ const parseH1 = (text) => {
let days
let dates

const yearAndMonth = isMonthAndYearOnly(date)
if( yearAndMonth ){
const yearOnly = isYearOnly(date)
const yearAndMonth = !yearOnly ? isMonthAndYearOnly(date) : false

if( yearOnly ){
//Record start on 1st january of the year
//Record end on 31st decemeber of year
dates = [
new Date( `${yearOnly}-01-01` ),
new Date( `${yearOnly}-12-31` ),
]
}else if( yearAndMonth ){
//Record the start of the month as the start date
dates = [ new Date( `${yearAndMonth.year}-${yearAndMonth.month}-01` ) ]

Expand Down
13 changes: 13 additions & 0 deletions tests/lib/report.test.js
Expand Up @@ -27,6 +27,8 @@ const daysSeparatedWithEm = fs.readFileSync('./tests/samples/daysSeparatedWithEm
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')
const yearOnly = fs.readFileSync('./tests/samples/yearOnly.html', 'utf8')


request.mockImplementation((...args) => {
const cb = args.pop()
Expand Down Expand Up @@ -119,5 +121,16 @@ test( 'If only the month and year are provided record start date as 1st day of t
expect( validateSchema(result) ).toBeTruthy()
expect( result.startDate ).toEqual( '2016-01-31T23:00:00.000Z' )
expect( result.endDate ).toEqual( '2016-02-29T23:00:00.000Z' )
})

test( 'If only the year is provided record start date as 1st january of the year, and end date has 31st december of that year', async() => {
request.mockImplementationOnce((...args) => {
const cb = args.pop()
cb( null, {statusCode: 200}, yearOnly )
})
const result = await scrapeReport( 'https://domain.tld/path/to/page.html' )
expect( validateSchema(result) ).toBeTruthy()
expect( result.startDate ).toEqual( '2015-01-01T00:00:00.000Z' )
expect( result.endDate ).toEqual( '2015-12-31T00:00:00.000Z' )

})

0 comments on commit 0c12480

Please sign in to comment.