Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/helpers/rank.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import type { Claims, PropertyClaims } from '../types/claim.js'
import { typedEntries } from '../utils/utils.js'
import type { Claim, Claims, PropertyClaims, Rank } from '../types/claim.js'

export function truthyPropertyClaims (propertyClaims: PropertyClaims): PropertyClaims {
const aggregate = propertyClaims.reduce(aggregatePerRank, {})
const aggregate: Partial<Record<Rank, Claim[]>> = {}
for (const claim of propertyClaims) {
const { rank } = claim
aggregate[rank] = aggregate[rank] || []
aggregate[rank].push(claim)
}

// on truthyness: https://www.mediawiki.org/wiki/Wikibase/Indexing/RDF_Dump_Format#Truthy_statements
return aggregate.preferred || aggregate.normal || []
}
Expand All @@ -10,17 +17,10 @@ export function nonDeprecatedPropertyClaims (propertyClaims: PropertyClaims): Pr
return propertyClaims.filter(claim => claim.rank !== 'deprecated')
}

const aggregatePerRank = (aggregate, claim) => {
const { rank } = claim
aggregate[rank] || (aggregate[rank] = [])
aggregate[rank].push(claim)
return aggregate
}

export function truthyClaims (claims: Claims): Claims {
const truthClaimsOnly = {}
Object.keys(claims).forEach(property => {
truthClaimsOnly[property] = truthyPropertyClaims(claims[property])
})
const truthClaimsOnly: Claims = {}
for (const [ property, value ] of typedEntries(claims)) {
truthClaimsOnly[property] = truthyPropertyClaims(value)
}
return truthClaimsOnly
}
5 changes: 3 additions & 2 deletions src/helpers/simplify_text_attributes.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { typedEntries } from '../utils/utils.js'
import type { WmLanguageCode } from '../types/options.js'
import type { Aliases, Descriptions, Glosses, Labels, Lemmas, Representations, SimplifiedAliases, SimplifiedDescriptions, SimplifiedGlosses, SimplifiedLabels, SimplifiedLemmas, SimplifiedRepresentations } from '../types/terms.js'

type InValue<T> = { readonly value: T }

function singleValue<V> (data: Partial<Readonly<Record<WmLanguageCode, InValue<V>>>>) {
const simplified: Partial<Record<WmLanguageCode, V>> = {}
for (const [ lang, obj ] of Object.entries(data)) {
for (const [ lang, obj ] of typedEntries(data)) {
simplified[lang] = obj != null ? obj.value : null
}
return simplified
}

function multiValue<V> (data: Partial<Readonly<Record<WmLanguageCode, ReadonlyArray<InValue<V>>>>>) {
const simplified: Partial<Record<WmLanguageCode, readonly V[]>> = {}
for (const [ lang, obj ] of Object.entries(data)) {
for (const [ lang, obj ] of typedEntries(data)) {
simplified[lang] = obj != null ? obj.map(o => o.value) : []
}
return simplified
Expand Down
20 changes: 12 additions & 8 deletions src/helpers/sitelinks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { fixedEncodeURIComponent, isOfType, rejectObsoleteInterface, replaceSpaceByUnderscores } from '../utils/utils.js'
import { fixedEncodeURIComponent, isAKey, isOfType, rejectObsoleteInterface, replaceSpaceByUnderscores } from '../utils/utils.js'
import { languages } from './sitelinks_languages.js'
import { specialSites } from './special_sites.js'
import type { EntityId } from '../types/entity.js'
import type { Url, WmLanguageCode } from '../types/options.js'
import type { Site } from '../types/sitelinks.js'

Expand All @@ -18,9 +17,14 @@ export function getSitelinkUrl ({ site, title }: GetSitelinkUrlOptions): Url {
if (!site) throw new Error('missing a site')
if (!title) throw new Error('missing a title')

if (isAKey(siteUrlBuilders, site)) {
return siteUrlBuilders[site](title)
}

const shortSiteKey = site.replace(/wiki$/, '')
const specialUrlBuilder = siteUrlBuilders[shortSiteKey] || siteUrlBuilders[site]
if (specialUrlBuilder) return specialUrlBuilder(title)
if (isAKey(siteUrlBuilders, shortSiteKey)) {
return siteUrlBuilders[shortSiteKey](title)
}

const { lang, project } = getSitelinkData(site)
title = fixedEncodeURIComponent(replaceSpaceByUnderscores(title))
Expand All @@ -34,7 +38,7 @@ const siteUrlBuilders = {
mediawiki: (title: string) => `https://www.mediawiki.org/wiki/${title}`,
meta: wikimediaSite('meta'),
species: wikimediaSite('species'),
wikidata: (entityId: EntityId) => {
wikidata: (entityId: string) => {
const prefix = prefixByEntityLetter[entityId[0]]
let title = prefix ? `${prefix}:${entityId}` : entityId
// Required for forms and senses
Expand Down Expand Up @@ -84,9 +88,9 @@ export function getSitelinkData (site: Site | Url): SitelinkData {
return { lang, project, key, title, url }
} else {
const key = site
const specialProjectName = specialSites[key]
if (specialProjectName) {
return { lang: 'en', project: specialProjectName, key }
if (isAKey(specialSites, site)) {
const project = specialSites[site]
return { lang: 'en', project, key }
}

let [ lang, projectSuffix, rest ] = key.split('wik')
Expand Down
7 changes: 4 additions & 3 deletions src/queries/cirrus_search.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// See https://www.wikidata.org/w/api.php?action=help&modules=query%2Bsearch

import { rejectObsoleteInterface } from '../utils/utils.js'
import { isAKey, rejectObsoleteInterface } from '../utils/utils.js'
import type { Url, UrlResultFormat } from '../types/options.js'
import type { BuildUrlFunction } from '../utils/build_url.js'

Expand All @@ -23,11 +23,12 @@ export function cirrusSearchPagesFactory (buildUrl: BuildUrlFunction) {
rejectObsoleteInterface(arguments)

// Accept sr parameters with or without prefix
for (const key in options) {
for (const [ key, value ] of Object.entries(options)) {
if (key.startsWith('sr')) {
const shortKey = key.replace(/^sr/, '')
if (!isAKey(options, shortKey)) throw new Error(`${key} is not a valid option`)
if (options[shortKey] != null) throw new Error(`${shortKey} and ${key} are the same`)
options[shortKey] = options[key]
options[shortKey] = value
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,14 @@ export function rejectObsoleteInterface (args: IArguments): void {
export function isOfType<T extends string> (all: readonly T[], element: unknown): element is T {
return typeof element === 'string' && (all as readonly string[]).includes(element)
}

/** key is a key on the object */
export function isAKey<T extends PropertyKey> (obj: Readonly<Partial<Record<T, unknown>>>, key: PropertyKey): key is T {
return Object.prototype.hasOwnProperty.call(obj, key)
}

/** like Object.entries() but with typed key */
export function typedEntries<K extends string, V> (input: Readonly<Partial<Record<K, V>>>): Array<[K, V]> {
// @ts-expect-error string is not assignable to K as K is more specific
return Object.entries(input)
}