diff --git a/lib/helpers/simplify_claims.js b/lib/helpers/simplify_claims.js index ebabee6b..45d7b904 100644 --- a/lib/helpers/simplify_claims.js +++ b/lib/helpers/simplify_claims.js @@ -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 @@ -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 diff --git a/test/data/Q19180293.json b/test/data/Q19180293.json new file mode 100644 index 00000000..532d2e16 --- /dev/null +++ b/test/data/Q19180293.json @@ -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": {} +} diff --git a/test/simplify_qualifiers.js b/test/simplify_qualifiers.js index 8f931f7f..eafa53b7 100644 --- a/test/simplify_qualifiers.js +++ b/test/simplify_qualifiers.js @@ -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 => { @@ -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 () { @@ -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 () { @@ -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() + }) + }) })