Skip to content

Commit

Permalink
simplify_claims: use helpers.wikidataTimeToISOString as date normalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
maxlath committed Mar 29, 2017
1 parent a5ae25f commit e2158f9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions lib/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
const wikidataTimeToDateObject = require('./wikidata_time_to_date_object')
const toDateObject = require('./wikidata_time_to_date_object')

const helpers = {}
helpers.isEntityId = (id) => /^(Q|P)[0-9]+$/.test(id)
helpers.isItemId = (id) => /^Q[0-9]+$/.test(id)
helpers.isPropertyId = (id) => /^P[0-9]+$/.test(id)

helpers.wikidataTimeToDateObject = wikidataTimeToDateObject
helpers.wikidataTimeToDateObject = toDateObject

helpers.wikidataTimeToEpochTime = function (wikidataTime) {
return wikidataTimeToDateObject(wikidataTime).getTime()
return toDateObject(wikidataTime).getTime()
}

helpers.wikidataTimeToISOString = function (wikidataTime) {
return wikidataTimeToDateObject(wikidataTime).toISOString()
return toDateObject(wikidataTime).toISOString()
}

module.exports = helpers
2 changes: 1 addition & 1 deletion lib/helpers/simplify_claims.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const simplifyClaim = function (claim, entityPrefix, propertyPrefix, keepQualifi
value = prefixedId(datavalue, propertyPrefix)
break
case 'time':
value = helpers.normalizeWikidataTime(datavalue.value.time)
value = helpers.wikidataTimeToISOString(datavalue.value.time)
break
case 'quantity':
value = parseFloat(datavalue.value.amount)
Expand Down
10 changes: 5 additions & 5 deletions test/simplify_claims.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ describe('simplifyClaim', function () {
it('should return a valid time for possible invalid dates', function (done) {
// exemple: Q4132785>P577 is 1953-00-00T00:00:00Z
const simplified = simplifyClaim(Q4132785.claims.P577[0])
isNaN(simplified).should.equal(false)
simplified.should.equal(-536457600000)
simplified.should.equal('1953-01-01T00:00:00.000Z')
done()
})

it('should return a valid time even for possible invalid negative date', function (done) {
// exemple: Q4132785>P577 is -1953-00-00T00:00:00Z
const simplified = simplifyClaim(Q4132785NegativeDate.claims.P577[0])
isNaN(simplified).should.equal(false)
simplified.should.equal(-123797894400000)
// cf https://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15.1
simplified.should.equal('-001953-01-01T00:00:00.000Z')
done()
})

Expand Down Expand Up @@ -210,7 +210,7 @@ describe('simplifyClaim', function () {
simplifiedWithQualifiers.qualifiers.P973.should.be.an.Array()
simplifiedWithQualifiers.qualifiers.P973[0].should.equal('http://mappings.dbpedia.org/index.php/OntologyClass:Book')
simplifiedWithQualifiers.qualifiers.P813.should.be.an.Array()
simplifiedWithQualifiers.qualifiers.P813[0].should.equal(1433980800000)
simplifiedWithQualifiers.qualifiers.P813[0].should.equal('2015-06-11T00:00:00.000Z')
done()
})

Expand Down

0 comments on commit e2158f9

Please sign in to comment.