Skip to content

Commit

Permalink
Added effective dates to the parse object (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
marchchad authored Oct 14, 2020
1 parent 18bd85a commit 2174296
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
37 changes: 34 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,14 @@ const link = ($row, columnIndex) =>
.find('a')
.attr('href')

const extractRow = $row => {
/**
* Extract the relevant information from the dom node and return
* an object with the data mapped by the appropriate named key
* @param {HTMLNode} $row - The dom node that contains the tabular data
* @param {String} effectiveStartDate - The start date the terminal procedure is effective for
* @param {HTMLNode} effectiveEndDate - The end date the terminal procedure is effective for
*/
const extractRow = ($row, effectiveStartDate, effectiveEndDate) => {
const type = text($row, 7)

if (!type) {
Expand Down Expand Up @@ -181,7 +188,29 @@ const extractRow = $row => {
compare: {
name: text($row, 9),
url: link($row, 9)
}
},
effectiveStartDate,
effectiveEndDate
}
}

/**
* Scrape the Effective date range from the dom
* @param {Object} $ - The Cheerio object that contains the serialized dom
* @returns {Object} - An object containing the effective start and end date
*/
const extractEffectiveDates = $ => {
const baseEffectiveDateString = $('.resultsSummary .join').html()
.split(':')[1]
.split('<')[0]
.trim()

const [ startMonthDay, remainder ] = baseEffectiveDateString.split('-')
const [ endMonthDay, yearAndCycle ] = remainder.split(',')
const [ year, _ ] = yearAndCycle.split('[')
return {
effectiveStartDate: new Date(`${startMonthDay.trim()} ${year}`),
effectiveEndDate: new Date(`${endMonthDay.trim()} ${year}`)
}
}

Expand Down Expand Up @@ -213,10 +242,12 @@ const parse = html => {
return { results, pageCount }
}

const { effectiveStartDate, effectiveEndDate } = extractEffectiveDates($)

results = $resultsTable
.find('tr')
.toArray()
.map(row => extractRow($(row)))
.map(row => extractRow($(row), effectiveStartDate, effectiveEndDate))
.filter(x => !!x)

if (results.length > 0) {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"description": "Fetch terminal procedures from https://www.faa.gov/",
"version": "1.0.5",
"author": {
"name": "Forrest Desjardins",
"email": "[email protected]",
"url": "github.com/fdesjardins"
"name": "FAA Aviation Data Portal",
"email": "[email protected]",
"url": "https://github.com/FAA-Aviation-Data-Portal"
},
"dependencies": {
"cheerio": "^1.0.0-rc.3",
Expand Down Expand Up @@ -68,7 +68,7 @@
"text"
]
},
"repository": "fdesjardins/terminal-procedures",
"repository": "FAA-Aviation-Data-Portal/terminal-procedures",
"scripts": {
"debug": "node ./example.js",
"test": "eslint *.js && nyc mocha test.js"
Expand Down

0 comments on commit 2174296

Please sign in to comment.