Skip to content

Commit d652756

Browse files
committed
chore: cleanup
1 parent 3e8641f commit d652756

File tree

6 files changed

+3479
-151
lines changed

6 files changed

+3479
-151
lines changed

bin/cmd.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,34 @@
33
import WikibaseRepo from './../lib/repo.js'
44
import { pickLanguages, pickKeys } from './../lib/util.js'
55

6-
void (async() => {
7-
const [source, target, ...entities] = process.argv.slice(2)
8-
const sourceRepo = new WikibaseRepo(source)
9-
const targetRepo = new WikibaseRepo(target, {
10-
oauth: {
11-
consumer_key: process.env.TARGET_WIKI_OAUTH_CONSUMER_TOKEN,
12-
consumer_secret: process.env.TARGET_WIKI_OAUTH_CONSUMER_SECRET,
13-
token: process.env.TARGET_WIKI_OAUTH_ACCESS_TOKEN,
14-
token_secret: process.env.TARGET_WIKI_OAUTH_ACCESS_SECRET
15-
}
16-
})
6+
;(async () => {
7+
const [source, target, ...entities] = process.argv.slice(2)
8+
const sourceRepo = new WikibaseRepo(source)
9+
const targetRepo = new WikibaseRepo(target, {
10+
oauth: {
11+
consumer_key: process.env.TARGET_WIKI_OAUTH_CONSUMER_TOKEN,
12+
consumer_secret: process.env.TARGET_WIKI_OAUTH_CONSUMER_SECRET,
13+
token: process.env.TARGET_WIKI_OAUTH_ACCESS_TOKEN,
14+
token_secret: process.env.TARGET_WIKI_OAUTH_ACCESS_SECRET
15+
}
16+
})
1717

18-
const contentLanguages = await targetRepo.getContentLanguages()
18+
const contentLanguages = await targetRepo.getContentLanguages()
1919

20-
let data = await sourceRepo.getEntities(...entities)
21-
data = data
22-
.map(e => pickKeys(e, 'type', 'labels', 'descriptions', 'aliases', 'datatype'))
23-
.map(e => pickLanguages(e, ...contentLanguages))
20+
let data = await sourceRepo.getEntities(...entities)
21+
data = data
22+
.map(e => pickKeys(e, 'type', 'labels', 'descriptions', 'aliases', 'datatype'))
23+
.map(e => pickLanguages(e, ...contentLanguages))
2424

25-
await targetRepo.createEntities(...data)
26-
return `Sucessfully transferred ${entities.length} entities from ${source} to ${target}.`
25+
await targetRepo.createEntities(...data)
26+
return `Sucessfully transferred ${entities.length} entities from ${source} to ${target}.`
2727
})()
28-
.then((result) => {
29-
if (result) {
30-
console.log(result)
31-
}
32-
})
33-
.catch((err) => {
34-
console.error(err)
35-
process.exit(1)
36-
})
28+
.then((result) => {
29+
if (result) {
30+
console.log(result)
31+
}
32+
})
33+
.catch((err) => {
34+
console.error(err)
35+
process.exit(1)
36+
})

lib/repo.js

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,51 @@ import WBEdit from 'wikibase-edit'
22
import { WBK } from 'wikibase-sdk'
33

44
export default class WikibaseRepository {
5-
constructor(origin, opts = {}) {
6-
this.origin = origin
5+
constructor (origin, opts = {}) {
6+
this.origin = origin
77

8-
this.read = new WBK({
9-
instance: origin
10-
})
8+
this.read = new WBK({
9+
instance: origin
10+
})
1111

12-
if (opts.oauth) {
13-
this.edit = new WBEdit({
14-
instance: origin,
15-
credentials: {
16-
oauth: opts.oauth
17-
}
18-
})
12+
if (opts.oauth) {
13+
this.edit = new WBEdit({
14+
instance: origin,
15+
credentials: {
16+
oauth: opts.oauth
1917
}
18+
})
2019
}
20+
}
2121

22-
getContentLanguages() {
23-
return fetch(`${this.origin}/w/api.php?action=query&meta=wbcontentlanguages&format=json`)
24-
.then(r => r.json())
25-
.then(body => Object.keys(body.query.wbcontentlanguages))
26-
}
22+
getContentLanguages () {
23+
return fetch(`${this.origin}/w/api.php?action=query&meta=wbcontentlanguages&format=json`)
24+
.then(r => r.json())
25+
.then(body => Object.keys(body.query.wbcontentlanguages))
26+
}
2727

28-
async createEntities(...entities) {
29-
if (!this.edit) {
30-
throw new Error('Cannot edit a read only instance.')
31-
}
32-
return Promise.all(entities.map(async entity => {
33-
return this.edit.entity.create(entity)
34-
}))
28+
async createEntities (...entities) {
29+
if (!this.edit) {
30+
throw new Error('Cannot edit a read only instance.')
3531
}
32+
return Promise.all(entities.map(async entity => {
33+
return this.edit.entity.create(entity)
34+
}))
35+
}
3636

37-
getEntities(...identifiers) {
38-
return Promise.all(identifiers.map(async identifier => {
39-
const [entityId, revision] = identifier.split('@')
40-
let url = revision
41-
? await this.read.getEntityRevision({
42-
id: entityId,
43-
revision: revision
44-
})
45-
: await this.read.getEntities({
46-
ids: [entityId]
47-
})
48-
const { entities } = await fetch(url).then(res => res.json())
49-
return entities[entityId]
50-
}))
51-
}
37+
getEntities (...identifiers) {
38+
return Promise.all(identifiers.map(async identifier => {
39+
const [entityId, revision] = identifier.split('@')
40+
const url = revision
41+
? await this.read.getEntityRevision({
42+
id: entityId,
43+
revision
44+
})
45+
: await this.read.getEntities({
46+
ids: [entityId]
47+
})
48+
const { entities } = await fetch(url).then(res => res.json())
49+
return entities[entityId]
50+
}))
51+
}
5252
}

lib/util.js

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
11
export const pickKeys = (obj, ...keys) => {
2-
const result = {}
3-
for (const [key, value] of Object.entries(obj)) {
4-
if (keys.includes(key)) {
5-
result[key] = value
6-
}
2+
return Object.entries(obj).reduce((acc, [key, value]) => {
3+
if (keys.includes(key)) {
4+
acc[key] = value
75
}
8-
return result
6+
return acc
7+
}, {})
98
}
109

1110
export const pickLanguages = (obj, ...languages) => {
12-
const result = {}
13-
for (const [key, value] of Object.entries(obj)) {
14-
if (!isObject(value)) {
15-
result[key] = value
16-
continue
17-
}
18-
const filteredChild = {}
19-
for (const [language, childValue] of Object.entries(value)) {
20-
if (languages.includes(language)) {
21-
filteredChild[language] = childValue
22-
}
23-
}
24-
result[key] = filteredChild
25-
}
26-
return result
11+
return Object.entries(obj).reduce((acc, [key, value]) => {
12+
acc[key] = isObject(value) ? pickKeys(value, ...languages) : value
13+
return acc
14+
}, {})
2715
}
2816

2917
function isObject (x) {
30-
return Object.prototype.toString.call(x) === '[object Object]'
18+
return Object.prototype.toString.call(x) === '[object Object]'
3119
}

0 commit comments

Comments
 (0)