From 2b5020ef24e405f20a40edb25d45525e5738b19d Mon Sep 17 00:00:00 2001 From: maxlath Date: Fri, 31 Mar 2017 12:12:01 +0200 Subject: [PATCH] Partially revert "helpers: removing helpers that were only needed to work with the now-offline WDQ service" to bring back wdk.isNumericId, and wdk.getNumericId as they are needed, at least by the wikidata-edit module This partially reverts commit df4301a09f6fa930711d7e956ad929ed10a443a2. --- lib/helpers/helpers.js | 6 ++++++ test/general.js | 2 ++ 2 files changed, 8 insertions(+) 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()