diff --git a/lib/helpers/helpers.js b/lib/helpers/helpers.js index fc40f654..3884337e 100644 --- a/lib/helpers/helpers.js +++ b/lib/helpers/helpers.js @@ -1,10 +1,16 @@ const toDateObject = require('./wikidata_time_to_date_object') const helpers = {} +helpers.isNumericId = (id) => /^[0-9]+$/.test(id) 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.getNumericId = function (id) { + if (!(helpers.isEntityId(id))) throw new Error(`invalid wikidata id: ${id}`) + return id.replace(/Q|P/, '') +} + helpers.wikidataTimeToDateObject = toDateObject // Try to parse the date or return the input diff --git a/test/general.js b/test/general.js index 043d25a9..698f7f69 100644 --- a/test/general.js +++ b/test/general.js @@ -38,6 +38,8 @@ describe('general', function () { wdk.isEntityId.should.be.a.Function() wdk.isItemId.should.be.a.Function() wdk.isPropertyId.should.be.a.Function() + wdk.isNumericId.should.be.a.Function() + wdk.getNumericId.should.be.a.Function() wdk.wikidataTimeToDateObject.should.be.a.Function() wdk.wikidataTimeToEpochTime.should.be.a.Function() wdk.wikidataTimeToISOString.should.be.a.Function()