wikibase-sdk
Advanced tools
Comparing version 7.1.2 to 7.1.4
@@ -12,2 +12,4 @@ const { simplifyClaims } = require('./simplify_claims') | ||
if (entity.datatype) simplified.datatype = entity.datatype | ||
simplifyIfDefined(entity, simplified, 'labels') | ||
@@ -14,0 +16,0 @@ simplifyIfDefined(entity, simplified, 'descriptions') |
@@ -15,4 +15,4 @@ module.exports = (input, options = {}) => { | ||
const { richVars, standaloneVars } = identifyVars(vars) | ||
return results.map(getSimplifiedResult(richVars, standaloneVars)) | ||
const { richVars, associatedVars, standaloneVars } = identifyVars(vars) | ||
return results.map(getSimplifiedResult(richVars, associatedVars, standaloneVars)) | ||
} | ||
@@ -73,70 +73,46 @@ | ||
const identifyVars = vars => { | ||
const data = { richVars: [], standaloneVars: [] } | ||
return vars.reduce(spreadVars(vars), data) | ||
const richVars = vars.filter(varName => vars.some(isAssociatedVar(varName))) | ||
const associatedVarPattern = new RegExp(`^(${richVars.join('|')})[A-Z]`) | ||
const associatedVars = vars.filter(varName => associatedVarPattern.test(varName)) | ||
const standaloneVars = vars.filter(varName => { | ||
return !richVars.includes(varName) && !associatedVarPattern.test(varName) | ||
}) | ||
return { richVars, associatedVars, standaloneVars } | ||
} | ||
const spreadVars = vars => (data, varName) => { | ||
if (vars.some(isAssociatedVar(varName))) { | ||
data.richVars.push(varName) | ||
return data | ||
} | ||
if (!associatedVarPattern.test(varName)) { | ||
data.standaloneVars.push(varName) | ||
return data | ||
} | ||
let associatedVar = varName | ||
.replace(associatedVarPattern, '$1') | ||
// The pattern regex fails to capture AltLabel prefixes alone, | ||
// due to the comflict with Label | ||
.replace(/Alt$/, '') | ||
if (!vars.includes(associatedVar)) { | ||
data.standaloneVars.push(varName) | ||
} | ||
return data | ||
const isAssociatedVar = varNameA => { | ||
const pattern = new RegExp(`^${varNameA}[A-Z]\\w+`) | ||
return pattern.test.bind(pattern) | ||
} | ||
const associatedVarPattern = /^(\w+)(Label|Description|AltLabel)$/ | ||
const isAssociatedVar = varNameA => varNameB => { | ||
if (`${varNameA}Label` === varNameB) return true | ||
if (`${varNameA}Description` === varNameB) return true | ||
if (`${varNameA}AltLabel` === varNameB) return true | ||
return false | ||
} | ||
const getSimplifiedResult = (richVars, standaloneVars) => { | ||
return result => { | ||
const simplifiedResult = {} | ||
for (let varName of richVars) { | ||
let value = parseValue(result[varName]) | ||
if (value != null) { | ||
simplifiedResult[varName] = { value } | ||
addAssociatedValue(result, varName, 'label', simplifiedResult[varName]) | ||
addAssociatedValue(result, varName, 'description', simplifiedResult[varName]) | ||
addAssociatedValue(result, varName, 'aliases', simplifiedResult[varName]) | ||
} | ||
const getSimplifiedResult = (richVars, associatedVars, standaloneVars) => result => { | ||
const simplifiedResult = {} | ||
for (let varName of richVars) { | ||
let richVarData = {} | ||
let value = parseValue(result[varName]) | ||
if (value != null) richVarData.value = value | ||
for (let associatedVarName of associatedVars) { | ||
if (associatedVarName.startsWith(varName)) addAssociatedValue(result, varName, associatedVarName, richVarData) | ||
} | ||
for (let varName of standaloneVars) { | ||
simplifiedResult[varName] = parseValue(result[varName]) | ||
} | ||
return simplifiedResult | ||
if (Object.keys(richVarData).length > 0) simplifiedResult[varName] = richVarData | ||
} | ||
for (let varName of standaloneVars) { | ||
simplifiedResult[varName] = parseValue(result[varName]) | ||
} | ||
return simplifiedResult | ||
} | ||
const addAssociatedValue = (result, varName, associatedVarName, varData) => { | ||
const fullAssociatedVarName = varName + varNameSuffixMap[associatedVarName] | ||
const fullAssociatedVarData = result[fullAssociatedVarName] | ||
if (fullAssociatedVarData != null) { | ||
varData[associatedVarName] = fullAssociatedVarData.value | ||
} | ||
const addAssociatedValue = (result, varName, associatedVarName, richVarData) => { | ||
// ex: propertyType => Type | ||
var shortAssociatedVarName = associatedVarName.split(varName)[1] | ||
// ex: Type => type | ||
shortAssociatedVarName = shortAssociatedVarName[0].toLowerCase() + shortAssociatedVarName.slice(1) | ||
// ex: altLabel => aliases | ||
shortAssociatedVarName = specialNames[shortAssociatedVarName] || shortAssociatedVarName | ||
const associatedVarData = result[associatedVarName] | ||
if (associatedVarData != null) richVarData[shortAssociatedVarName] = associatedVarData.value | ||
} | ||
const varNameSuffixMap = { | ||
label: 'Label', | ||
description: 'Description', | ||
aliases: 'AltLabel' | ||
const specialNames = { | ||
altLabel: 'aliases' | ||
} |
@@ -19,3 +19,3 @@ const { isPlainObject, forceArray, shortLang } = require('../utils/utils') | ||
// titles cant be let empty | ||
if (!(titles && titles.length > 0)) throw new Error('no title provided') | ||
if (!(titles && titles.length > 0)) throw new Error('no titles provided') | ||
// default to the English Wikipedia | ||
@@ -22,0 +22,0 @@ if (!(sites && sites.length > 0)) sites = [ 'enwiki' ] |
{ | ||
"name": "wikibase-sdk", | ||
"version": "7.1.2", | ||
"version": "7.1.4", | ||
"description": "A javascript tool-suite to query wikibase and simplify its results", | ||
@@ -5,0 +5,0 @@ "main": "lib/wikibase-sdk.js", |
@@ -125,3 +125,3 @@ # Wikibase SDK | ||
* [wikidata-filter](https://github.com/maxlath/wikidata-filter): A command-line tool to filter a Wikidata dump by claim | ||
* [wikidata-subset-search-engine](https://github.com/inventaire/wikidata-subset-search-engine): Tools to setup an ElasticSearch instance fed with subsets of Wikidata | ||
* [wikidata-subset-search-engine](https://github.com/inventaire/entities-search-engine/tree/wikidata-subset-search-engine): Tools to setup an ElasticSearch instance fed with subsets of Wikidata | ||
* [wikidata-taxonomy](https://github.com/nichtich/wikidata-taxonomy): Command-line tool to extract taxonomies from Wikidata | ||
@@ -135,5 +135,5 @@ * [import-wikidata-dump-to-couchdb](https://github.com/maxlath/import-wikidata-dump-to-couchdb): Import a subset or a full Wikidata dump into a CouchDB database | ||
Do you know [inventaire.io](https://inventaire.io/)? It's a web app to share books with your friends, built on top of Wikidata! And its [libre software](http://github.com/inventaire/inventaire) too. | ||
Do you know [Inventaire](https://inventaire.io/)? It's a web app to share books with your friends, built on top of Wikidata! And its [libre software](http://github.com/inventaire/inventaire) too. | ||
## License | ||
[MIT](LICENSE.md) |
63565
1006