From d2f6c2d709f5749ab84d70146e6471dbe3f65dcf Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Thu, 1 Aug 2024 09:37:35 +0200 Subject: [PATCH] chore: cleanup --- bin/cmd.js | 54 ++++++++++++++-------------- lib/repo.js | 78 ++++++++++++++++++++-------------------- lib/util.js | 37 +++++++++---------- test/test.js | 100 +++++++++++++++++++++++++-------------------------- 4 files changed, 133 insertions(+), 136 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 3808ea0..81276b4 100644 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -3,34 +3,34 @@ import WikibaseRepo from './../lib/repo.js' import { pickLanguages, pickKeys } from './../lib/util.js' -void (async() => { - const [source, target, ...entities] = process.argv.slice(2) - const sourceRepo = new WikibaseRepo(source) - const targetRepo = new WikibaseRepo(target, { - oauth: { - consumer_key: process.env.TARGET_WIKI_OAUTH_CONSUMER_TOKEN, - consumer_secret: process.env.TARGET_WIKI_OAUTH_CONSUMER_SECRET, - token: process.env.TARGET_WIKI_OAUTH_ACCESS_TOKEN, - token_secret: process.env.TARGET_WIKI_OAUTH_ACCESS_SECRET - } - }) +;(async () => { + const [source, target, ...entities] = process.argv.slice(2) + const sourceRepo = new WikibaseRepo(source) + const targetRepo = new WikibaseRepo(target, { + oauth: { + consumer_key: process.env.TARGET_WIKI_OAUTH_CONSUMER_TOKEN, + consumer_secret: process.env.TARGET_WIKI_OAUTH_CONSUMER_SECRET, + token: process.env.TARGET_WIKI_OAUTH_ACCESS_TOKEN, + token_secret: process.env.TARGET_WIKI_OAUTH_ACCESS_SECRET + } + }) - const contentLanguages = await targetRepo.getContentLanguages() + const contentLanguages = await targetRepo.getContentLanguages() - let data = await sourceRepo.getEntities(...entities) - data = data - .map(e => pickKeys(e, 'type', 'labels', 'descriptions', 'aliases', 'datatype')) - .map(e => pickLanguages(e, ...contentLanguages)) + let data = await sourceRepo.getEntities(...entities) + data = data + .map(e => pickKeys(e, 'type', 'labels', 'descriptions', 'aliases', 'datatype')) + .map(e => pickLanguages(e, ...contentLanguages)) - await targetRepo.createEntities(...data) - return `Sucessfully transferred ${entities.length} entities from ${source} to ${target}.` + await targetRepo.createEntities(...data) + return `Sucessfully transferred ${entities.length} entities from ${source} to ${target}.` })() - .then((result) => { - if (result) { - console.log(result) - } - }) - .catch((err) => { - console.error(err) - process.exit(1) - }) + .then((result) => { + if (result) { + console.log(result) + } + }) + .catch((err) => { + console.error(err) + process.exit(1) + }) diff --git a/lib/repo.js b/lib/repo.js index 1dc735e..e07faec 100644 --- a/lib/repo.js +++ b/lib/repo.js @@ -2,51 +2,51 @@ import WBEdit from 'wikibase-edit' import { WBK } from 'wikibase-sdk' export default class WikibaseRepository { - constructor(origin, opts = {}) { - this.origin = origin + constructor (origin, opts = {}) { + this.origin = origin - this.read = new WBK({ - instance: origin - }) + this.read = new WBK({ + instance: origin + }) - if (opts.oauth) { - this.edit = new WBEdit({ - instance: origin, - credentials: { - oauth: opts.oauth - } - }) + if (opts.oauth) { + this.edit = new WBEdit({ + instance: origin, + credentials: { + oauth: opts.oauth } + }) } + } - getContentLanguages() { - return fetch(`${this.origin}/w/api.php?action=query&meta=wbcontentlanguages&format=json`) - .then(r => r.json()) - .then(body => Object.keys(body.query.wbcontentlanguages)) - } + getContentLanguages () { + return fetch(`${this.origin}/w/api.php?action=query&meta=wbcontentlanguages&format=json`) + .then(r => r.json()) + .then(body => Object.keys(body.query.wbcontentlanguages)) + } - async createEntities(...entities) { - if (!this.edit) { - throw new Error('Cannot edit a read only instance.') - } - return Promise.all(entities.map(async entity => { - return this.edit.entity.create(entity) - })) + async createEntities (...entities) { + if (!this.edit) { + throw new Error('Cannot edit a read only instance.') } + return Promise.all(entities.map(async entity => { + return this.edit.entity.create(entity) + })) + } - getEntities(...identifiers) { - return Promise.all(identifiers.map(async identifier => { - const [entityId, revision] = identifier.split('@') - let url = revision - ? await this.read.getEntityRevision({ - id: entityId, - revision: revision - }) - : await this.read.getEntities({ - ids: [entityId] - }) - const { entities } = await fetch(url).then(res => res.json()) - return entities[entityId] - })) - } + getEntities (...identifiers) { + return Promise.all(identifiers.map(async identifier => { + const [entityId, revision] = identifier.split('@') + const url = revision + ? await this.read.getEntityRevision({ + id: entityId, + revision + }) + : await this.read.getEntities({ + ids: [entityId] + }) + const { entities } = await fetch(url).then(res => res.json()) + return entities[entityId] + })) + } } diff --git a/lib/util.js b/lib/util.js index fe133f1..1a3b204 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,31 +1,28 @@ export const pickKeys = (obj, ...keys) => { - const result = {} - for (const [key, value] of Object.entries(obj)) { - if (keys.includes(key)) { - result[key] = value - } + return Object.entries(obj).reduce((acc, [key, value]) => { + if (keys.includes(key)) { + acc[key] = value } - return result + return acc + }, {}) } export const pickLanguages = (obj, ...languages) => { - const result = {} - for (const [key, value] of Object.entries(obj)) { - if (!isObject(value)) { - result[key] = value - continue - } - const filteredChild = {} - for (const [language, childValue] of Object.entries(value)) { - if (languages.includes(language)) { - filteredChild[language] = childValue - } + return Object.entries(obj).reduce((acc, [key, value]) => { + if (!isObject(value)) { + acc[key] = value + } else { + acc[key] = Object.entries(value).reduce((acc, [language, childValue]) => { + if (languages.includes(language)) { + acc[language] = childValue } - result[key] = filteredChild + return acc + }, {}) } - return result + return acc + }, {}) } function isObject (x) { - return Object.prototype.toString.call(x) === '[object Object]' + return Object.prototype.toString.call(x) === '[object Object]' } diff --git a/test/test.js b/test/test.js index 3672edd..247363d 100755 --- a/test/test.js +++ b/test/test.js @@ -1,66 +1,66 @@ #!/usr/bin/env node -import assert from 'assert' +import assert from 'node:assert' import test from 'node:test' import { pickKeys, pickLanguages } from './../lib/util.js' test('util.pickKeys', async t => { - await t.test('empty', t => { - assert.deepStrictEqual(pickKeys({}), {}) - }) - await t.test('picks keys', t => { - const result = pickKeys({ - foo: 'bar', - baz: { - foo: 'bar', - }, - qux: 12 - }, 'baz', 'qux') + await t.test('empty', t => { + assert.deepStrictEqual(pickKeys({}), {}) + }) + await t.test('picks keys', t => { + const result = pickKeys({ + foo: 'bar', + baz: { + foo: 'bar' + }, + qux: 12 + }, 'baz', 'qux') - assert.deepStrictEqual(result, { - baz: { - foo: 'bar' - }, - qux: 12 - }) + assert.deepStrictEqual(result, { + baz: { + foo: 'bar' + }, + qux: 12 }) + }) }) test('util.pickLanguages', async t => { - await t.test('empty', t => { - assert.deepStrictEqual(pickLanguages({}), {}) - }) + await t.test('empty', t => { + assert.deepStrictEqual(pickLanguages({}), {}) + }) - await t.test('skip languages', t => { - const result = pickLanguages({ - labels: { - en: { - language: 'en', - value: 'pipe', - }, - fr: { - language: 'fr', - value: 'pipe', - }, - de: { - language: 'de', - value: 'Pfeife', - } - } - }, 'en', 'fr') + await t.test('skip languages', t => { + const result = pickLanguages({ + labels: { + en: { + language: 'en', + value: 'pipe' + }, + fr: { + language: 'fr', + value: 'pipe' + }, + de: { + language: 'de', + value: 'Pfeife' + } + } + }, 'en', 'fr') - assert.deepStrictEqual(result, { - labels: { - en: { - language: 'en', - value: 'pipe', - }, - fr: { - language: 'fr', - value: 'pipe', - } - } - }) + assert.deepStrictEqual(result, { + labels: { + en: { + language: 'en', + value: 'pipe' + }, + fr: { + language: 'fr', + value: 'pipe' + } + } }) + }) })