Skip to content

Commit

Permalink
helpers: fixed naming issues
Browse files Browse the repository at this point in the history
fixed #15
  • Loading branch information
maxlath committed Mar 29, 2017
1 parent 92dd0dc commit 00e3c81
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
10 changes: 5 additions & 5 deletions lib/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ const wikidataTimeToDateObject = require('./wikidata_time_to_date_object')

const helpers = {}
helpers.isNumericId = (id) => /^[0-9]+$/.test(id)
helpers.isWikidataId = (id) => /^(Q|P)[0-9]+$/.test(id)
helpers.isWikidataEntityId = (id) => /^Q[0-9]+$/.test(id)
helpers.isWikidataPropertyId = (id) => /^P[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.isWikidataId(id)) {
} else if (helpers.isEntityId(id)) {
return numericId ? id.slice(1) : id
} else {
throw new Error('invalid id')
}
}

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

Expand Down
2 changes: 1 addition & 1 deletion lib/queries/get_reverse_claims.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function (property, value, options = {}) {
}

function getValueString (value) {
if (helpers.isWikidataEntityId(value)) {
if (helpers.isItemId(value)) {
value = `wd:${value}`
} else if (typeof value === 'string') {
value = `'${value}'`
Expand Down
12 changes: 6 additions & 6 deletions test/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ describe('general', function () {

helpers.should.be.an.Object()
helpers.isNumericId.should.be.a.Function()
helpers.isWikidataId.should.be.a.Function()
helpers.isWikidataEntityId.should.be.a.Function()
helpers.isWikidataPropertyId.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()
Expand All @@ -38,9 +38,9 @@ describe('general', function () {
helpers.normalizeWikidataTime.should.be.a.Function()

wdk.isNumericId.should.be.a.Function()
wdk.isWikidataId.should.be.a.Function()
wdk.isWikidataEntityId.should.be.a.Function()
wdk.isWikidataPropertyId.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()
Expand Down
36 changes: 36 additions & 0 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,40 @@ describe('helpers', function () {
done()
})
})
describe('isEntityId', function () {
it('should accept both item and property ids', function (done) {
helpers.isEntityId('Q571').should.be.true()
helpers.isEntityId('P31').should.be.true()
helpers.isEntityId('31').should.be.false()
helpers.isEntityId(31).should.be.false()
helpers.isEntityId('Z31').should.be.false()
helpers.isEntityId('q31').should.be.false()
helpers.isEntityId('p31').should.be.false()
done()
})
})
describe('isItemId', function () {
it('should accept both item and property ids', function (done) {
helpers.isItemId('Q571').should.be.true()
helpers.isItemId('P31').should.be.false()
helpers.isItemId('31').should.be.false()
helpers.isItemId(31).should.be.false()
helpers.isItemId('Z31').should.be.false()
helpers.isItemId('q31').should.be.false()
helpers.isItemId('p31').should.be.false()
done()
})
})
describe('isPropertyId', function () {
it('should accept both item and property ids', function (done) {
helpers.isPropertyId('P31').should.be.true()
helpers.isPropertyId('Q571').should.be.false()
helpers.isPropertyId('31').should.be.false()
helpers.isPropertyId(31).should.be.false()
helpers.isPropertyId('Z31').should.be.false()
helpers.isPropertyId('q31').should.be.false()
helpers.isPropertyId('p31').should.be.false()
done()
})
})
})
2 changes: 1 addition & 1 deletion test/simplify_sparql_results.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('wikidata simplify SPARQL results', function () {
it('should return an array of results values, filtering out blank nodes', function (done) {
const output = simplify(singleVarData)
output[0].should.equal('Q112983')
output.forEach((result) => helpers.isWikidataId(result).should.be.true())
output.forEach((result) => helpers.isEntityId(result).should.be.true())
done()
})
})
Expand Down

0 comments on commit 00e3c81

Please sign in to comment.