Skip to content

Commit

Permalink
simplify qualifiers: add support for empty values
Browse files Browse the repository at this point in the history
fix #47
  • Loading branch information
maxlath committed Mar 16, 2019
1 parent ed0e7a4 commit e539d1f
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lib/helpers/simplify_claims.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ const simplifyClaim = function (claim, ...options) {
if (mainsnak) {
datatype = mainsnak.datatype
datavalue = mainsnak.datavalue
if (!datavalue) {
if (mainsnak.snaktype === 'somevalue') return options.somevalueValue
else return options.novalueValue
}
} else {
// Qualifiers have no mainsnak, and define datatype, datavalue on claim
datavalue = claim.datavalue
Expand All @@ -93,6 +89,13 @@ const simplifyClaim = function (claim, ...options) {
else isReferenceSnak = true
}

if (!datavalue) {
const snaktype = (isQualifierSnak || isReferenceSnak) ? claim.snaktype : mainsnak.snaktype
if (snaktype === 'somevalue') return options.somevalueValue
else if (snaktype === 'novalue') return options.novalueValue
else throw new Error('no datavalue or special snaktype found')
}

const value = parseClaim(datatype, datavalue, options, claim.id)

// Qualifiers should not attempt to keep sub-qualifiers or references
Expand Down
48 changes: 48 additions & 0 deletions test/data/Q19180293.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"type": "item",
"id": "Q19180293",
"labels": {},
"aliases": {},
"claims": {
"P1433": [
{
"mainsnak": {
"snaktype": "value",
"property": "P1433",
"hash": "b5a3566c7c166b94bae127474751a968d0597146",
"datavalue": {
"value": {
"entity-type": "item",
"numeric-id": 28823728,
"id": "Q28823728"
},
"type": "wikibase-entityid"
},
"datatype": "wikibase-item"
},
"type": "statement",
"qualifiers": {
"P156": [
{
"snaktype": "somevalue",
"property": "P156",
"hash": "06ebe2529907ec88b14c16a08a845f15eb6942ef",
"datatype": "wikibase-item"
}
],
"P1100": [
{
"snaktype": "novalue",
"property": "P1100",
"hash": "0f3aa5a76e4a90daa40720962d96200842d3ae58",
"datatype": "quantity"
}
]
},
"id": "Q19180293$4570bfdc-4e34-6e50-e93a-3248db40737b",
"rank": "normal"
}
]
},
"sitelinks": {}
}
71 changes: 70 additions & 1 deletion test/simplify_qualifiers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require('should')
const should = require('should')
const { simplifyQualifier, simplifyPropertyQualifiers, simplifyQualifiers } = require('../lib/helpers/simplify_claims')
const Q2112 = require('./data/Q2112.json')
const Q19180293 = require('./data/Q19180293.json')

describe('simplifyQualifier', function () {
it('should simplify a qualifier', done => {
Expand All @@ -9,6 +10,30 @@ describe('simplifyQualifier', function () {
simplified.should.equal('1953-01-01T00:00:00.000Z')
done()
})

describe('empty values', function () {
it('should return the desired novalueValue', function (done) {
const noValueQualifier = Q19180293.claims.P1433[0].qualifiers.P1100[0]
should(simplifyQualifier(noValueQualifier)).not.be.ok()
simplifyQualifier(noValueQualifier, { novalueValue: '-' }).should.equal('-')
simplifyQualifier(noValueQualifier, { novalueValue: '' }).should.equal('')
done()
})

it('should return the desired somevalueValue', function (done) {
const someValueQualifier = Q19180293.claims.P1433[0].qualifiers.P156[0]
should(simplifyQualifier(someValueQualifier)).not.be.ok()
simplifyQualifier(someValueQualifier, { somevalueValue: '?' }).should.equal('?')
simplifyQualifier(someValueQualifier, { somevalueValue: '' }).should.equal('')
done()
})

it('should accept null as a possible value', function (done) {
const noValueQualifier = Q19180293.claims.P1433[0].qualifiers.P1100[0]
should(simplifyQualifier(noValueQualifier, { novalueValue: null }) === null).be.true()
done()
})
})
})

describe('simplifyPropertyQualifiers', function () {
Expand All @@ -18,6 +43,28 @@ describe('simplifyPropertyQualifiers', function () {
simplified.should.deepEqual([ '1953-01-01T00:00:00.000Z' ])
done()
})

describe('empty values', function () {
it('should return the desired novalueValue', function (done) {
const propQualifiers = Q19180293.claims.P1433[0].qualifiers.P1100
simplifyPropertyQualifiers(propQualifiers, { novalueValue: '-' }).should.deepEqual([ '-' ])
simplifyPropertyQualifiers(propQualifiers, { novalueValue: '' }).should.deepEqual([ '' ])
done()
})

it('should return the desired somevalueValue', function (done) {
const propQualifiers = Q19180293.claims.P1433[0].qualifiers.P156
simplifyPropertyQualifiers(propQualifiers, { somevalueValue: '?' }).should.deepEqual([ '?' ])
simplifyPropertyQualifiers(propQualifiers, { somevalueValue: '' }).should.deepEqual([ '' ])
done()
})

it('should accept null as a possible value', function (done) {
const propQualifiers = Q19180293.claims.P1433[0].qualifiers.P1100
simplifyPropertyQualifiers(propQualifiers, { novalueValue: null }).should.deepEqual([ null ])
done()
})
})
})

describe('simplifyQualifiers', function () {
Expand All @@ -27,4 +74,26 @@ describe('simplifyQualifiers', function () {
simplified.P580.should.deepEqual([ '1953-01-01T00:00:00.000Z' ])
done()
})

describe('empty values', function () {
it('should return the desired novalueValue', function (done) {
const qualifiers = Q19180293.claims.P1433[0].qualifiers
simplifyQualifiers(qualifiers, { novalueValue: '-' }).P1100.should.deepEqual([ '-' ])
simplifyQualifiers(qualifiers, { novalueValue: '' }).P1100.should.deepEqual([ '' ])
done()
})

it('should return the desired somevalueValue', function (done) {
const qualifiers = Q19180293.claims.P1433[0].qualifiers
simplifyQualifiers(qualifiers, { somevalueValue: '?' }).P156.should.deepEqual([ '?' ])
simplifyQualifiers(qualifiers, { somevalueValue: '' }).P156.should.deepEqual([ '' ])
done()
})

it('should accept null as a possible value', function (done) {
const qualifiers = Q19180293.claims.P1433[0].qualifiers
simplifyQualifiers(qualifiers, { novalueValue: null }).P1100.should.deepEqual([ null ])
done()
})
})
})

0 comments on commit e539d1f

Please sign in to comment.