Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Sep 9, 2024
1 parent 8c8abec commit 2ffb1ae
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 25 deletions.
24 changes: 17 additions & 7 deletions bin/wdjskos
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import suggest from "../lib/suggest.js"
import { loadMappingSchemes, updateMappingSchemes } from "../lib/mapping-schemes.js"
import getConcepts from "../lib/queries/get-concepts.js"

const program = new Command();
const program = new Command()

program
.version(pkg.version)
Expand All @@ -27,7 +27,9 @@ program

const error = (message, showHelp) => {
console.error(chalk.red(message))
if (showHelp) { program.help() }
if (showHelp) {
program.help()
}
process.exit(1)
}

Expand All @@ -43,7 +45,7 @@ function conceptCommand (ids) {

getMappingSchemes()
.then(schemes => new WikidataService(schemes))
.then(service => getConcepts({ uri: ids.join("|"), language }))
.then(() => getConcepts({ uri: ids.join("|"), language }))
.then(ndjson.write)
}

Expand All @@ -62,8 +64,12 @@ function schemesCommand () {
function mappingsCommand (from, to) {
let direction = program.both || to === undefined ? "both" : "forward"

if (from === "-") from = undefined
if (to === "-") to = undefined
if (from === "-") {
from = undefined
}
if (to === "-") {
to = undefined
}

let languages = program.lang
let toScheme = (from || direction !== "forward") ? program.scheme : null
Expand Down Expand Up @@ -157,9 +163,13 @@ program
program.parse(process.argv)

// Show help by default
if (!program.args.length) program.help()
if (!program.args.length) {
program.help()
}

// Prevent logging an EPIPE error when piping the output
process.stdout.on("error", err => {
if (err.code !== "EPIPE") throw err
if (err.code !== "EPIPE") {
throw err
}
})
10 changes: 4 additions & 6 deletions lib/mappers.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ function mapSimpleSitelinks (sitelinks) {
subjectOf: Object.keys(sitelinks).map(site => {
try {
return {
url: wdk.getSitelinkUrl({site, title: sitelinks[site]}),
url: wdk.getSitelinkUrl({
site,
title: sitelinks[site],
}),
}
} catch (e) {
// just ignore unkown sites
Expand Down Expand Up @@ -184,15 +187,10 @@ function mapSimpleEntity (entity) {
const mapLabels = labels => {
return labels ? mapSimpleLabels(wdk.simplify.labels(labels)) : {}
}

const mapAliases = aliases => mapSimpleAliases(wdk.simplify.aliases(aliases))

const mapDescriptions = descriptions => mapSimpleDescriptions(wdk.simplify.descriptions(descriptions))

const mapClaims = claims => mapSimpleClaims(wdk.simplify.claims(claims))

const mapSitelinks = sitelinks => mapSimpleSitelinks(wdk.simplify.sitelinks(sitelinks))

const mapEntity = entity => mapSimpleEntity(wdk.simplify.entity(entity))

export {
Expand Down
8 changes: 6 additions & 2 deletions lib/queries/get-concepts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ export default async function getConcepts (query) {

const languages = selectedLanguages(query)
const props = ["info", "sitelinks", "labels", "aliases", "descriptions", "claims"]
const url = wdk.getEntities({ids: concepts.map(concept => concept.notation[0]), languages, props})
const url = wdk.getEntities({
ids: concepts.map(concept => concept.notation[0]),
languages,
props,
})

return httpRequest(url)
.then(res => res.entities || {})
Expand All @@ -25,7 +29,7 @@ export default async function getConcepts (query) {
}
const index = {}
entities.forEach(e => {
index[e.uri] = e
index[e.uri] = e
})
const rels = ["P361", "P31", "P279", "P131"]
return getBacklinks(Object.keys(index), languages, rels)
Expand Down
4 changes: 2 additions & 2 deletions lib/queries/get-mappings-to.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { labelFromSparql } from "../utils.js"
import { sparqlRequest } from "../request.js"
import * as jskosTools from "jskos-tools"
import * as jskos from "jskos-tools"

const { defaultMappingType } = jskosTools
const { defaultMappingType } = jskos

async function getMappingsTo(args, schemes) {
const { fromScheme, languages } = args
Expand Down
2 changes: 1 addition & 1 deletion lib/wikidata-concept-scheme.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ConceptScheme } from "jskos-tools"
import { ConceptScheme } from "jskos-tools"
import { mappingTypes } from "./types.js"

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/wikidata-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { selectedLanguages } from "./utils.js"

import { mappingTypeIds } from "./types.js"
import { mapMappingClaims, mapIdentifier } from "./mappers.js"
import * as jskosTools from "jskos-tools"
import * as jskos from "jskos-tools"

import debug from "./debug.js"

Expand All @@ -19,7 +19,7 @@ import ServiceError from "./service-error.js"
import getWdEdit from "./wbedit.js"
import wikidata from "./wikidata.js"

const { addMappingIdentifiers } = jskosTools
const { addMappingIdentifiers } = jskos

/**
* Returns a single Wikidata claim object by its id.
Expand Down
2 changes: 1 addition & 1 deletion lib/wikidata-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import WikidataConceptScheme from "./wikidata-concept-scheme.js"
import WikidataConceptSchemeSet from "./wikidata-concept-scheme-set.js"
import WikidataService from "./wikidata-service.js"

export * from "./types.js"
export * from "./mappers.js"

export { WikidataConceptScheme, WikidataConceptSchemeSet, WikidataService }
4 changes: 2 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import bodyParser from "body-parser"
import { WikidataService } from "./lib/wikidata-wrapper.js"
import { loadMappingSchemes } from "./lib/mapping-schemes.js"
import getConcepts from "./lib/queries/get-concepts.js"
import * as jskosTools from "jskos-tools"
import * as jskos from "jskos-tools"
import path, { dirname } from "node:path"
import { fileURLToPath } from "node:url"
import passport from "passport"
Expand All @@ -14,7 +14,7 @@ import { ExtractJwt } from "passport-jwt"

const __dirname = dirname(fileURLToPath(import.meta.url))

const { addContext, addMappingIdentifiers } = jskosTools
const { addContext, addMappingIdentifiers } = jskos

const port = config.port
const app = express()
Expand Down
2 changes: 1 addition & 1 deletion test/map-mapping.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai"
import { WikidataConceptScheme, WikidataService } from "../lib/wikidata-wrapper.js"
import { readFile } from "fs/promises"
import { readFile } from "node:fs/promises"

let service

Expand Down
1 change: 0 additions & 1 deletion test/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ describe("mapEntity", () => {
fileUrl = new URL("./Q42.json", import.meta.url)
Q42 = JSON.parse(await readFile(fileUrl, "utf8"))


fileUrl = new URL("./Q42.jskos.json", import.meta.url)
Q42concept = JSON.parse(await readFile(fileUrl, "utf8"))

Expand Down

0 comments on commit 2ffb1ae

Please sign in to comment.