Skip to content

Commit

Permalink
build: try to resolve date in different languages
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Jan 18, 2020
1 parent 037458e commit f8b9ece
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/metascraper-helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const {
includes,
invoke,
isArray,
isDate,
isEmpty,
isNumber,
isString,
Expand All @@ -24,7 +23,6 @@ const {
const langs = require('iso-639-3').map(({ iso6391 }) => iso6391)
const condenseWhitespace = require('condense-whitespace')
const urlRegex = require('url-regex')({ exact: true })

const isRelativeUrl = require('is-relative-url')
const fileExtension = require('file-extension')
const _normalizeUrl = require('normalize-url')
Expand Down Expand Up @@ -189,8 +187,10 @@ const url = (value, { url = '' } = {}) => {
return isUri(value) ? value : null
}

const LANGUAGES_DETECTION_SUPPORTED = ['fr', 'en', 'es', 'pt', 'ja', 'de']

const date = value => {
if (!(isString(value) || isNumber(value))) return false
if (!(isString(value) || isNumber(value))) return undefined

// remove whitespace for easier parsing
if (isString(value)) trim(value)
Expand All @@ -202,8 +202,19 @@ const date = value => {
return new Date(toString(value)).toISOString()
}

const parsed = chrono.parseDate(value)
if (isDate(parsed)) return parsed.toISOString()
let parsedValue

for (let index = 0; index < LANGUAGES_DETECTION_SUPPORTED.length; index++) {
const lang = LANGUAGES_DETECTION_SUPPORTED[index]
const parsed = chrono[lang].parseDate(value)

if (parsed && !isNaN(parsed.getTime())) {
parsedValue = parsed.toISOString()
break
}
}

return parsedValue
}

const lang = value => {
Expand Down
6 changes: 6 additions & 0 deletions packages/metascraper-helpers/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,16 @@ describe('metascraper-helpers', () => {
})

it('.date', () => {
should(date('')).be.equal(undefined)
should(date()).be.equal(undefined)
should(date(undefined)).be.equal(undefined)
should(date(null)).be.equal(undefined)
should(date('null')).be.equal(undefined)
should(date('Jun 20')).be.equal('2020-06-20T12:00:00.000Z')
should(date('Jun 20 2018')).be.equal('2018-06-20T12:00:00.000Z')
should(date('Jun 2018')).be.equal('2018-06-01T12:00:00.000Z')
should(date(2010)).be.equal('2010-01-01T00:00:00.000Z')
should(date('11 juil. 2019')).be.equal(undefined)
})
})

Expand Down

0 comments on commit f8b9ece

Please sign in to comment.