Skip to content

Commit

Permalink
helpers: removing helpers that were only needed to work with the now-…
Browse files Browse the repository at this point in the history
…offline WDQ service

that is, all helpers dealing with entities ids in there numeric id form
  • Loading branch information
maxlath committed Mar 29, 2017
1 parent 00e3c81 commit df4301a
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 87 deletions.
20 changes: 0 additions & 20 deletions lib/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
const wikidataTimeToDateObject = 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.normalizeId = function (id, numericId, type = 'Q') {
if (helpers.isNumericId(id)) {
return numericId ? id : `${type}${id}`
} else if (helpers.isEntityId(id)) {
return numericId ? id.slice(1) : id
} else {
throw new Error('invalid id')
}
}

helpers.getNumericId = function (id) {
if (!(helpers.isEntityId(id))) throw new Error(`invalid wikidata id: ${id}`)
return id.replace(/Q|P/, '')
}

helpers.normalizeIds = function (ids, numericId, type = 'Q') {
return ids.map((id) => helpers.normalizeId(id, numericId, type))
}

helpers.wikidataTimeToDateObject = wikidataTimeToDateObject

helpers.wikidataTimeToEpochTime = function (wikidataTime) {
Expand Down
3 changes: 1 addition & 2 deletions lib/queries/get_entities.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const helpers = require('../helpers/helpers')
const buildUrl = require('../utils/build_url')
const { isPlainObject, forceArray, shortLang } = require('../utils/utils')

Expand Down Expand Up @@ -28,7 +27,7 @@ module.exports = function (ids, languages, props, format) {
// Properties can be either one property as a string
// or an array or properties;
// either case me just want to deal with arrays
ids = helpers.normalizeIds(forceArray(ids))
ids = forceArray(ids)
props = forceArray(props)

const query = {
Expand Down
8 changes: 0 additions & 8 deletions test/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,17 @@ describe('general', function () {
wdk.getWikidataIdsFromWikipediaTitles.should.be.a.Function()

helpers.should.be.an.Object()
helpers.isNumericId.should.be.a.Function()
helpers.isEntityId.should.be.a.Function()
helpers.isItemId.should.be.a.Function()
helpers.isPropertyId.should.be.a.Function()
helpers.normalizeId.should.be.a.Function()
helpers.getNumericId.should.be.a.Function()
helpers.normalizeIds.should.be.a.Function()
helpers.wikidataTimeToDateObject.should.be.a.Function()
helpers.wikidataTimeToEpochTime.should.be.a.Function()
helpers.wikidataTimeToISOString.should.be.a.Function()
helpers.normalizeWikidataTime.should.be.a.Function()

wdk.isNumericId.should.be.a.Function()
wdk.isEntityId.should.be.a.Function()
wdk.isItemId.should.be.a.Function()
wdk.isPropertyId.should.be.a.Function()
wdk.normalizeId.should.be.a.Function()
wdk.getNumericId.should.be.a.Function()
wdk.normalizeIds.should.be.a.Function()
wdk.wikidataTimeToDateObject.should.be.a.Function()
wdk.wikidataTimeToEpochTime.should.be.a.Function()
wdk.wikidataTimeToISOString.should.be.a.Function()
Expand Down
8 changes: 0 additions & 8 deletions test/get_entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ describe('wikidata getEntities', function () {
url2.split('&ids=Q535|Q7546&').length.should.equal(2)
done()
})

it('accepts numeric ids', function (done) {
const url = getEntities(['535', '7546'])
url.should.equal(getEntities({ids: ['535', '7546']}))
const url2 = qs.unescape(url)
url2.split('&ids=Q535|Q7546&').length.should.equal(2)
done()
})
})
describe('languages', function () {
it('default to no language parameter', function (done) {
Expand Down
49 changes: 0 additions & 49 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,6 @@ describe('helpers', function () {
done()
})

describe('normalizeId', function () {
it('should return a string', function (done) {
helpers.normalizeId('Q571').should.be.a.String()
helpers.normalizeId('P50').should.be.a.String()
helpers.normalizeId('571').should.be.a.String()
done()
})

it('should return a Q id by default', function (done) {
helpers.normalizeId('Q571').should.equal('Q571')
helpers.normalizeId('571').should.equal('Q571')
helpers.normalizeId('571').should.equal('Q571')
done()
})

it('should return a numeric id if requested', function (done) {
helpers.normalizeId('Q571', true).should.equal('571')
helpers.normalizeId('571', true).should.equal('571')
done()
})

it('should return a Q id in the other case', function (done) {
helpers.normalizeId('Q571', false).should.equal('Q571')
helpers.normalizeId('571', false).should.equal('Q571')
done()
})

it('should return a P id if requested', function (done) {
helpers.normalizeId('P50', false, 'P').should.equal('P50')
helpers.normalizeId('50', false, 'P').should.equal('P50')
done()
})

it('should return a Q in the other case', function (done) {
helpers.normalizeId('Q571', false, 'Q').should.equal('Q571')
helpers.normalizeId('571', false, 'Q').should.equal('Q571')
done()
})

it('should have no type with numeric id anyway', function (done) {
helpers.normalizeId('Q571', true, 'Q').should.equal('571')
helpers.normalizeId('571', true, 'Q').should.equal('571')
helpers.normalizeId('Q50', true, 'P').should.equal('50')
helpers.normalizeId('50', true, 'P').should.equal('50')
helpers.normalizeId('50', true, 'anything').should.equal('50')
done()
})
})

const ISOtime = '2014-05-14T00:00:00.000Z'
const wdTime = '+2014-05-14T00:00:00Z'
const epoch = 1400025600000
Expand Down

0 comments on commit df4301a

Please sign in to comment.