From cec4e01cdffda8136900bb2de8d314ef1f3583be Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Fri, 17 Mar 2023 15:23:53 +0100 Subject: [PATCH 1/4] refactor: use typesave utils --- src/helpers/rank.ts | 26 ++++++++++++------------- src/helpers/simplify_text_attributes.ts | 5 +++-- src/helpers/sitelinks.ts | 20 +++++++++++-------- src/queries/cirrus_search.ts | 7 ++++--- src/utils/utils.ts | 11 +++++++++++ 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/src/helpers/rank.ts b/src/helpers/rank.ts index 0e1e6e3c..91799352 100644 --- a/src/helpers/rank.ts +++ b/src/helpers/rank.ts @@ -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> = {} + for (const claim of propertyClaims) { + const { rank } = claim + 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 || [] } @@ -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 } diff --git a/src/helpers/simplify_text_attributes.ts b/src/helpers/simplify_text_attributes.ts index 8f208ea3..bf670664 100644 --- a/src/helpers/simplify_text_attributes.ts +++ b/src/helpers/simplify_text_attributes.ts @@ -1,3 +1,4 @@ +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' @@ -5,7 +6,7 @@ type InValue = { readonly value: T } function singleValue (data: Partial>>>) { const simplified: Partial> = {} - for (const [ lang, obj ] of Object.entries(data)) { + for (const [ lang, obj ] of typedEntries(data)) { simplified[lang] = obj != null ? obj.value : null } return simplified @@ -13,7 +14,7 @@ function singleValue (data: Partial (data: Partial>>>>) { const simplified: Partial> = {} - 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 diff --git a/src/helpers/sitelinks.ts b/src/helpers/sitelinks.ts index 8d6a9c57..55cefaba 100644 --- a/src/helpers/sitelinks.ts +++ b/src/helpers/sitelinks.ts @@ -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' @@ -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)) @@ -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 @@ -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') diff --git a/src/queries/cirrus_search.ts b/src/queries/cirrus_search.ts index b54c5f24..eb869899 100644 --- a/src/queries/cirrus_search.ts +++ b/src/queries/cirrus_search.ts @@ -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' @@ -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 } } diff --git a/src/utils/utils.ts b/src/utils/utils.ts index ad30c692..093d5feb 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -56,3 +56,14 @@ export function rejectObsoleteInterface (args: IArguments): void { export function isOfType (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 (obj: Readonly>>, key: PropertyKey): key is T { + return Object.prototype.hasOwnProperty.call(obj, key) +} + +/** like Object.entries() but with typed key */ +export function typedEntries (input: Readonly>>): Array<[K, V]> { + // @ts-expect-error string is not assignable to K as K is more specific + return Object.entries(input) +} From 4bf5b47030ff89051c0f28d890e75a1f86bc139f Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Wed, 22 Mar 2023 17:30:46 +0100 Subject: [PATCH 2/4] feat: load languages from wikidata --- docs/general_helpers.md | 2 +- package.json | 2 +- .../generate_sitelinks_languages.ts | 17 - .../sitelinks_languages/get_sitelinks_sites | 5 - .../update_sitelinks_languages | 20 - scripts/update_wikimedia_constants.ts | 61 ++ src/helpers/simplify_text_attributes.ts | 9 +- src/helpers/sitelinks.ts | 87 +- src/helpers/sitelinks_languages.ts | 336 ------- src/helpers/special_sites.ts | 8 - ...elinks_sites.ts => wikimedia_constants.ts} | 916 +++++++++++++++++- src/index.ts | 1 + src/queries/get_entities.ts | 9 +- src/queries/get_entities_from_sitelinks.ts | 23 +- src/types/options.ts | 2 - src/types/sitelinks.ts | 8 +- src/types/terms.ts | 30 +- src/utils/utils.ts | 10 +- tests/sitelinks_helpers.ts | 2 +- 19 files changed, 1063 insertions(+), 485 deletions(-) delete mode 100755 scripts/sitelinks_languages/generate_sitelinks_languages.ts delete mode 100755 scripts/sitelinks_languages/get_sitelinks_sites delete mode 100755 scripts/sitelinks_languages/update_sitelinks_languages create mode 100755 scripts/update_wikimedia_constants.ts delete mode 100644 src/helpers/sitelinks_languages.ts delete mode 100644 src/helpers/special_sites.ts rename src/helpers/{sitelinks_sites.ts => wikimedia_constants.ts} (62%) diff --git a/docs/general_helpers.md b/docs/general_helpers.md index 8d2fa6c6..04251470 100644 --- a/docs/general_helpers.md +++ b/docs/general_helpers.md @@ -193,7 +193,7 @@ getSitelinkData('https://de.wikipedia.org/wiki/Kernfusion') // => { // lang: 'de', // project: 'wikipedia', -// key: 'dewiki',wwwwwwwwwwwwwwwww +// key: 'dewiki', // title: 'Kernfusion', // url: 'https://de.wikipedia.org/wiki/Kernfusion' // } diff --git a/package.json b/package.json index 09ea9355..5c60ebac 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "prepack": "npm run lint && npm test && npm run update-dist", "postpublish": "./scripts/postpublish", "update-dist": "./scripts/update_dist", - "update-sitelinks-languages": "./scripts/sitelinks_languages/update_sitelinks_languages", + "update-wikimedia-constants": "./scripts/update_wikimedia_constants.ts", "update-toc": "./scripts/update_toc", "watch": "tsc --watch" }, diff --git a/scripts/sitelinks_languages/generate_sitelinks_languages.ts b/scripts/sitelinks_languages/generate_sitelinks_languages.ts deleted file mode 100755 index c31f22b2..00000000 --- a/scripts/sitelinks_languages/generate_sitelinks_languages.ts +++ /dev/null @@ -1,17 +0,0 @@ -#!/usr/bin/env ts-node -import { sites } from '../../src/helpers/sitelinks_sites.js' -import { specialSites } from '../../src/helpers/special_sites.js' -import { uniq } from '../../src/utils/utils.js' - -const monolingualProjects = Object.keys(specialSites) - -const languagesCodes = sites - .filter(site => !monolingualProjects.includes(site)) - .map(site => site.split(/wik(i|t)/)[0]) - -const stringifiedArray = JSON.stringify(uniq(languagesCodes), null, 2) - // Prevent linting errors - .replace(/"/g, '\'') - .replace(/'\n/, '\',\n') - -console.log(`${stringifiedArray} as const`) diff --git a/scripts/sitelinks_languages/get_sitelinks_sites b/scripts/sitelinks_languages/get_sitelinks_sites deleted file mode 100755 index e4f8bf9c..00000000 --- a/scripts/sitelinks_languages/get_sitelinks_sites +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env bash -curl -s 'https://www.wikidata.org/w/api.php?action=paraminfo&modules=wbgetentities&format=json' | - jq '.paraminfo.modules[0].parameters[1].type' | - sed "s/\"/'/g" | - sed "s/'$/',/g" diff --git a/scripts/sitelinks_languages/update_sitelinks_languages b/scripts/sitelinks_languages/update_sitelinks_languages deleted file mode 100755 index 48985112..00000000 --- a/scripts/sitelinks_languages/update_sitelinks_languages +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env bash - -which jq > /dev/null || { - echo "requires to have jq (https://stedolan.github.io/jq/) installed" - exit 1 -} - -echo "Generating ./src/helpers/sitelinks_sites.ts" -tee ./src/helpers/sitelinks_sites.ts << EOF -// Generated by 'npm run update-sitelinks-languages' -export const sites = $(./scripts/sitelinks_languages/get_sitelinks_sites) as const -EOF - -echo "Generating ./src/helpers/sitelinks_languages.ts" -tee ./src/helpers/sitelinks_languages.ts << EOF -// Generated by 'npm run update-sitelinks-languages' -export const languages = $(ts-node ./scripts/sitelinks_languages/generate_sitelinks_languages.ts) -EOF - -echo "Done updating ./src/helpers/sitelinks_languages.ts" diff --git a/scripts/update_wikimedia_constants.ts b/scripts/update_wikimedia_constants.ts new file mode 100755 index 00000000..40f1ff33 --- /dev/null +++ b/scripts/update_wikimedia_constants.ts @@ -0,0 +1,61 @@ +#!/usr/bin/env ts-node +import { writeFile } from 'node:fs/promises' +import { uniq } from '../src/utils/utils.js' + +interface Parameter { + name: string + type: string[] +} + +function stringifyArray (input: string[]) { + return JSON.stringify(uniq(input), null, 2) + // Prevent linting errors + .replace(/"/g, '\'') + .replace(/'\n/, '\',\n') +} + +function stringifySimpleRecord (input: Record) { + let output = '{\n' + output += Object.entries(input).map(([ key, value ]) => ' ' + key + ': ' + '\'' + value + '\',\n').join('') + output += '}' + return output +} + +doit() +async function doit () { + const response = await fetch('https://www.wikidata.org/w/api.php?action=paraminfo&modules=wbgetentities&format=json') + const data = await response.json() + + const parameters = data.paraminfo.modules[0].parameters as Parameter[] + + const sites = parameters.find(o => o.name === 'sites')?.type + const languages = parameters.find(o => o.name === 'languages')?.type + if (!sites || !languages) throw new Error('paraminfo format changed') + + const specialSites: Record = {} + for (const site of sites) { + const project = site.match(/^(.+)wiki$/)?.[1] + if (!project) continue + if (!languages.includes(project.replace(/_/g, '-'))) { + specialSites[site] = project + } + } + + const languagesWithWiki = languages.filter(o => sites.includes(o.replace(/-/g, '_') + 'wiki')) + console.log('languages', languages.length, 'with a wiki', languagesWithWiki.length) + + const output = [ + "// Generated by 'npm run update-wikimedia-constants'", + [ + 'export type Site = typeof sites[number]', + 'export type Language = typeof languages[number]', + 'export type LanguageWithWiki = typeof languagesWithWiki[number]', + ].join('\n'), + 'export const specialSites = ' + stringifySimpleRecord(specialSites) + ' as const', + 'export const sites = ' + stringifyArray(sites) + ' as const', + 'export const languages = ' + stringifyArray(languages) + ' as const', + 'export const languagesWithWiki = ' + stringifyArray(languagesWithWiki) + ' as const', + ].join('\n\n') + '\n' + + writeFile('./src/helpers/wikimedia_constants.ts', output, 'utf-8') +} diff --git a/src/helpers/simplify_text_attributes.ts b/src/helpers/simplify_text_attributes.ts index bf670664..b2e5328d 100644 --- a/src/helpers/simplify_text_attributes.ts +++ b/src/helpers/simplify_text_attributes.ts @@ -1,19 +1,18 @@ 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 = { readonly value: T } -function singleValue (data: Partial>>>) { - const simplified: Partial> = {} +function singleValue (data: Partial>>>) { + const simplified: Partial> = {} for (const [ lang, obj ] of typedEntries(data)) { simplified[lang] = obj != null ? obj.value : null } return simplified } -function multiValue (data: Partial>>>>) { - const simplified: Partial> = {} +function multiValue (data: Partial>>>>) { + const simplified: Partial> = {} for (const [ lang, obj ] of typedEntries(data)) { simplified[lang] = obj != null ? obj.map(o => o.value) : [] } diff --git a/src/helpers/sitelinks.ts b/src/helpers/sitelinks.ts index 55cefaba..1f7fd2a6 100644 --- a/src/helpers/sitelinks.ts +++ b/src/helpers/sitelinks.ts @@ -1,13 +1,15 @@ import { fixedEncodeURIComponent, isAKey, isOfType, rejectObsoleteInterface, replaceSpaceByUnderscores } from '../utils/utils.js' -import { languages } from './sitelinks_languages.js' -import { specialSites } from './special_sites.js' -import type { Url, WmLanguageCode } from '../types/options.js' -import type { Site } from '../types/sitelinks.js' +import { languages, sites, specialSites } from './wikimedia_constants.js' +import type { Language, Site } from './wikimedia_constants.js' +import type { Url } from '../types/options.js' + +type ValueOf = T[keyof T] +type SpecialSiteProjectName = ValueOf const wikidataBase = 'https://www.wikidata.org/wiki/' export interface GetSitelinkUrlOptions { - site: Site + site: Site | SpecialSiteProjectName title: string } @@ -33,12 +35,13 @@ export function getSitelinkUrl ({ site, title }: GetSitelinkUrlOptions): Url { const wikimediaSite = (subdomain: string) => (title: string) => `https://${subdomain}.wikimedia.org/wiki/${title}` -const siteUrlBuilders = { +const siteUrlBuilders: Readonly string>> = { commons: wikimediaSite('commons'), - mediawiki: (title: string) => `https://www.mediawiki.org/wiki/${title}`, + mediawiki: title => `https://www.mediawiki.org/wiki/${title}`, meta: wikimediaSite('meta'), + sources: title => `https://wikisource.org/wiki/${title}`, species: wikimediaSite('species'), - wikidata: (entityId: string) => { + wikidata: entityId => { const prefix = prefixByEntityLetter[entityId[0]] let title = prefix ? `${prefix}:${entityId}` : entityId // Required for forms and senses @@ -46,7 +49,7 @@ const siteUrlBuilders = { return `${wikidataBase}${title}` }, wikimania: wikimediaSite('wikimania'), -} as const +} const prefixByEntityLetter = { E: 'EntitySchema', @@ -57,7 +60,7 @@ const prefixByEntityLetter = { const sitelinkUrlPattern = /^https?:\/\/([\w-]{2,10})\.(\w+)\.org\/\w+\/(.*)/ export interface SitelinkData { - lang: WmLanguageCode + lang: Language project: Project key: string title?: string @@ -71,57 +74,59 @@ export function getSitelinkData (site: Site | Url): SitelinkData { if (!matchData) throw new Error(`invalid sitelink url: ${url}`) let [ lang, project, title ] = matchData.slice(1) title = decodeURIComponent(title) - let key: string + if (lang === 'commons') { + return { lang: 'en', project: 'commons', key: 'commons', title, url } + } + + if (!isOfType(projectNames, project)) { + throw new Error(`project is unknown: ${project}`) + } + // Known case: wikidata, mediawiki if (lang === 'www') { - lang = 'en' - key = project - } else if (lang === 'commons') { - lang = 'en' - project = key = 'commons' - } else { - // Support multi-parts language codes, such as be_x_old - lang = lang.replace(/-/g, '_') - key = `${lang}${project}`.replace('wikipedia', 'wiki') + return { lang: 'en', project, key: project, title, url } + } + + if (!isOfType(languages, lang)) { + throw new Error(`sitelink language not found: ${lang}. Updating wikibase-sdk to a more recent version might fix the issue.`) } - // @ts-expect-error + + // Support multi-parts language codes, such as be_x_old + const sitelang = lang.replace(/-/g, '_') + const key = `${sitelang}${project}`.replace('wikipedia', 'wiki') + return { lang, project, key, title, url } } else { - const key = site if (isAKey(specialSites, site)) { const project = specialSites[site] - return { lang: 'en', project, key } + return { lang: 'en', project, key: site } } - let [ lang, projectSuffix, rest ] = key.split('wik') + let [ lang, projectSuffix, rest ] = site.split('wik') // Detecting cases like 'frwikiwiki' that would return [ 'fr', 'i', 'i' ] - if (rest != null) throw new Error(`invalid sitelink key: ${key}`) + if (rest != null) throw new Error(`invalid sitelink key: ${site}`) + + // Support sites such as be_x_oldwiki, which refers to be-x-old.wikipedia.org + lang = lang.replace(/_/g, '-') if (!isOfType(languages, lang)) { - throw new Error(`sitelink lang not found: ${lang}. Updating wikibase-sdk to a more recent version might fix the issue.`) + throw new Error(`sitelink language not found: ${lang}. Updating wikibase-sdk to a more recent version might fix the issue.`) } - // Support keys such as be_x_oldwiki, which refers to be-x-old.wikipedia.org - lang = lang.replace(/_/g, '-') + if (!isAKey(projectsBySuffix, projectSuffix)) { + throw new Error(`sitelink project not found: ${site}`) + } const project = projectsBySuffix[projectSuffix] - if (!project) throw new Error(`sitelink project not found: ${project}`) - - // @ts-expect-error - return { lang, project, key } + return { lang, project, key: site } } } -export const isSitelinkKey = (site: string): boolean => { - try { - // relies on getSitelinkData validation - getSitelinkData(site) - return true - } catch (err) { - return false - } -} +export const isSite = (site: string): site is Site => isOfType(sites, site) + +/** @deprecated use isSite */ +export const isSitelinkKey = isSite const projectsBySuffix = { i: 'wikipedia', diff --git a/src/helpers/sitelinks_languages.ts b/src/helpers/sitelinks_languages.ts deleted file mode 100644 index d610dfc7..00000000 --- a/src/helpers/sitelinks_languages.ts +++ /dev/null @@ -1,336 +0,0 @@ -// Generated by 'npm run update-sitelinks-languages' -export const languages = [ - 'aa', - 'ab', - 'ace', - 'ady', - 'af', - 'ak', - 'als', - 'alt', - 'ami', - 'am', - 'ang', - 'an', - 'arc', - 'ar', - 'ary', - 'arz', - 'ast', - 'as', - 'atj', - 'avk', - 'av', - 'awa', - 'ay', - 'azb', - 'az', - 'ban', - 'bar', - 'bat_smg', - 'ba', - 'bcl', - 'be_x_old', - 'be', - 'bg', - 'bh', - 'bi', - 'bjn', - 'blk', - 'bm', - 'bn', - 'bo', - 'bpy', - 'br', - 'bs', - 'bug', - 'bxr', - 'ca', - 'cbk_zam', - 'cdo', - 'ceb', - 'ce', - 'cho', - 'chr', - 'ch', - 'chy', - 'ckb', - 'co', - 'crh', - 'cr', - 'csb', - 'cs', - 'cu', - 'cv', - 'cy', - 'dag', - 'da', - 'de', - 'din', - 'diq', - 'dsb', - 'dty', - 'dv', - 'dz', - 'ee', - 'el', - 'eml', - 'en', - 'eo', - 'es', - 'et', - 'eu', - 'ext', - 'fa', - 'ff', - 'fiu_vro', - 'fi', - 'fj', - 'fo', - 'frp', - 'frr', - 'fr', - 'fur', - 'fy', - 'gag', - 'gan', - 'ga', - 'gcr', - 'gd', - 'glk', - 'gl', - 'gn', - 'gom', - 'gor', - 'got', - 'gu', - 'guw', - 'gv', - 'hak', - 'ha', - 'haw', - 'he', - 'hif', - 'hi', - 'ho', - 'hr', - 'hsb', - 'ht', - 'hu', - 'hy', - 'hyw', - 'hz', - 'ia', - 'id', - 'ie', - 'ig', - 'ii', - 'ik', - 'ilo', - 'inh', - 'io', - 'is', - 'it', - 'iu', - 'jam', - 'ja', - 'jbo', - 'jv', - 'kaa', - 'kab', - 'ka', - 'kbd', - 'kbp', - 'kcg', - 'kg', - 'ki', - 'kj', - 'kk', - 'kl', - 'km', - 'kn', - 'koi', - 'ko', - 'krc', - 'kr', - 'ksh', - 'ks', - 'ku', - 'kv', - 'kw', - 'ky', - 'lad', - 'la', - 'lbe', - 'lb', - 'lez', - 'lfn', - 'lg', - 'lij', - 'li', - 'lld', - 'lmo', - 'ln', - 'lo', - 'lrc', - 'ltg', - 'lt', - 'lv', - 'mad', - 'mai', - 'map_bms', - 'mdf', - 'mg', - 'mhr', - 'mh', - 'min', - 'mi', - 'mk', - 'ml', - 'mni', - 'mn', - 'mnw', - 'mo', - 'mrj', - 'mr', - 'ms', - 'mt', - 'mus', - 'mwl', - 'myv', - 'my', - 'mzn', - 'nah', - 'nap', - 'na', - 'nds_nl', - 'nds', - 'ne', - 'new', - 'ng', - 'nia', - 'nl', - 'nn', - 'nov', - 'no', - 'nqo', - 'nrm', - 'nso', - 'nv', - 'ny', - 'oc', - 'olo', - 'om', - 'or', - 'os', - 'pag', - 'pam', - 'pap', - 'pa', - 'pcd', - 'pcm', - 'pdc', - 'pfl', - 'pih', - 'pi', - 'pl', - 'pms', - 'pnb', - 'pnt', - 'ps', - 'pt', - 'pwn', - 'qu', - 'rm', - 'rmy', - 'rn', - 'roa_rup', - 'roa_tara', - 'ro', - 'rue', - 'ru', - 'rw', - 'sah', - 'sat', - 'sa', - 'scn', - 'sco', - 'sc', - 'sd', - 'se', - 'sg', - 'shi', - 'shn', - 'sh', - 'shy', - 'simple', - 'si', - 'skr', - 'sk', - 'sl', - 'smn', - 'sm', - 'sn', - 'sources', - 'so', - 'sq', - 'srn', - 'sr', - 'ss', - 'stq', - 'st', - 'su', - 'sv', - 'sw', - 'szl', - 'szy', - 'ta', - 'tay', - 'tcy', - 'tet', - 'te', - 'tg', - 'th', - 'ti', - 'tk', - 'tl', - 'tn', - 'to', - 'tpi', - 'trv', - 'tr', - 'ts', - 'tt', - 'tum', - 'tw', - 'tyv', - 'ty', - 'udm', - 'ug', - 'uk', - 'ur', - 'uz', - 'vec', - 'vep', - 've', - 'vi', - 'vls', - 'vo', - 'war', - 'wa', - 'wo', - 'wuu', - 'xal', - 'xh', - 'xmf', - 'yi', - 'yo', - 'yue', - 'za', - 'zea', - 'zh_classical', - 'zh_min_nan', - 'zh_yue', - 'zh', - 'zu', -] as const diff --git a/src/helpers/special_sites.ts b/src/helpers/special_sites.ts deleted file mode 100644 index ee511369..00000000 --- a/src/helpers/special_sites.ts +++ /dev/null @@ -1,8 +0,0 @@ -export const specialSites = { - commonswiki: 'commons', - mediawikiwiki: 'mediawiki', - metawiki: 'meta', - specieswiki: 'specieswiki', - wikidatawiki: 'wikidata', - wikimaniawiki: 'wikimania', -} as const diff --git a/src/helpers/sitelinks_sites.ts b/src/helpers/wikimedia_constants.ts similarity index 62% rename from src/helpers/sitelinks_sites.ts rename to src/helpers/wikimedia_constants.ts index af4ce95c..24eae4d5 100644 --- a/src/helpers/sitelinks_sites.ts +++ b/src/helpers/wikimedia_constants.ts @@ -1,4 +1,19 @@ -// Generated by 'npm run update-sitelinks-languages' +// Generated by 'npm run update-wikimedia-constants' + +export type Site = typeof sites[number] +export type Language = typeof languages[number] +export type LanguageWithWiki = typeof languagesWithWiki[number] + +export const specialSites = { + commonswiki: 'commons', + mediawikiwiki: 'mediawiki', + metawiki: 'meta', + sourceswiki: 'sources', + specieswiki: 'species', + wikidatawiki: 'wikidata', + wikimaniawiki: 'wikimania', +} as const + export const sites = [ 'aawiki', 'aawikibooks', @@ -28,6 +43,7 @@ export const sites = [ 'angwikiquote', 'angwikisource', 'angwiktionary', + 'anpwiki', 'anwiki', 'anwiktionary', 'arcwiki', @@ -289,6 +305,8 @@ export const sites = [ 'gorwiktionary', 'gotwiki', 'gotwikibooks', + 'gucwiki', + 'gurwiki', 'guwiki', 'guwikibooks', 'guwikiquote', @@ -895,3 +913,899 @@ export const sites = [ 'zuwikibooks', 'zuwiktionary', ] as const + +export const languages = [ + 'aa', + 'ab', + 'abs', + 'ace', + 'ady', + 'ady-cyrl', + 'aeb', + 'aeb-arab', + 'aeb-latn', + 'af', + 'agq', + 'ak', + 'aln', + 'als', + 'alt', + 'am', + 'ami', + 'an', + 'ang', + 'ann', + 'anp', + 'ar', + 'arc', + 'arn', + 'arq', + 'ary', + 'arz', + 'as', + 'ase', + 'ast', + 'atj', + 'av', + 'avk', + 'awa', + 'ay', + 'az', + 'azb', + 'ba', + 'bag', + 'ban', + 'ban-bali', + 'bar', + 'bas', + 'bat-smg', + 'bax', + 'bbc', + 'bbc-latn', + 'bbj', + 'bcc', + 'bci', + 'bcl', + 'be', + 'be-tarask', + 'be-x-old', + 'bfd', + 'bg', + 'bgn', + 'bh', + 'bho', + 'bi', + 'bjn', + 'bkc', + 'bkh', + 'bkm', + 'blk', + 'bm', + 'bn', + 'bo', + 'bpy', + 'bqi', + 'bqz', + 'br', + 'brh', + 'bs', + 'btm', + 'bto', + 'bug', + 'bxr', + 'byv', + 'ca', + 'cak', + 'cbk-zam', + 'cdo', + 'ce', + 'ceb', + 'ch', + 'cho', + 'chr', + 'chy', + 'ckb', + 'cnh', + 'co', + 'cps', + 'cr', + 'crh', + 'crh-cyrl', + 'crh-latn', + 'cs', + 'csb', + 'cu', + 'cv', + 'cy', + 'da', + 'dag', + 'de', + 'de-at', + 'de-ch', + 'de-formal', + 'dga', + 'din', + 'diq', + 'dsb', + 'dtp', + 'dty', + 'dua', + 'dv', + 'dz', + 'ee', + 'egl', + 'el', + 'eml', + 'en', + 'en-ca', + 'en-gb', + 'en-us', + 'eo', + 'es', + 'es-419', + 'es-formal', + 'et', + 'eto', + 'etu', + 'eu', + 'ewo', + 'ext', + 'fa', + 'fat', + 'ff', + 'fi', + 'fit', + 'fiu-vro', + 'fj', + 'fkv', + 'fmp', + 'fo', + 'fon', + 'fr', + 'frc', + 'frp', + 'frr', + 'fur', + 'fy', + 'ga', + 'gaa', + 'gag', + 'gan', + 'gan-hans', + 'gan-hant', + 'gcr', + 'gd', + 'gl', + 'gld', + 'glk', + 'gn', + 'gom', + 'gom-deva', + 'gom-latn', + 'gor', + 'got', + 'gpe', + 'grc', + 'gsw', + 'gu', + 'guc', + 'gur', + 'guw', + 'gv', + 'gya', + 'ha', + 'hak', + 'haw', + 'he', + 'hi', + 'hif', + 'hif-latn', + 'hil', + 'ho', + 'hr', + 'hrx', + 'hsb', + 'hsn', + 'ht', + 'hu', + 'hu-formal', + 'hy', + 'hyw', + 'hz', + 'ia', + 'id', + 'ie', + 'ig', + 'igl', + 'ii', + 'ik', + 'ike-cans', + 'ike-latn', + 'ilo', + 'inh', + 'io', + 'is', + 'isu', + 'it', + 'iu', + 'ja', + 'jam', + 'jbo', + 'jut', + 'jv', + 'ka', + 'kaa', + 'kab', + 'kbd', + 'kbd-cyrl', + 'kbp', + 'kcg', + 'kea', + 'ker', + 'kg', + 'khw', + 'ki', + 'kiu', + 'kj', + 'kjh', + 'kjp', + 'kk', + 'kk-arab', + 'kk-cn', + 'kk-cyrl', + 'kk-kz', + 'kk-latn', + 'kk-tr', + 'kl', + 'km', + 'kn', + 'ko', + 'ko-kp', + 'koi', + 'kr', + 'krc', + 'kri', + 'krj', + 'krl', + 'ks', + 'ks-arab', + 'ks-deva', + 'ksf', + 'ksh', + 'ksw', + 'ku', + 'ku-arab', + 'ku-latn', + 'kum', + 'kus', + 'kv', + 'kw', + 'ky', + 'la', + 'lad', + 'lb', + 'lbe', + 'lem', + 'lez', + 'lfn', + 'lg', + 'li', + 'lij', + 'liv', + 'lki', + 'lld', + 'lmo', + 'ln', + 'lns', + 'lo', + 'loz', + 'lrc', + 'lt', + 'ltg', + 'lus', + 'luz', + 'lv', + 'lzh', + 'lzz', + 'mad', + 'mag', + 'mai', + 'map-bms', + 'mcn', + 'mcp', + 'mdf', + 'mg', + 'mh', + 'mhr', + 'mi', + 'min', + 'mk', + 'ml', + 'mn', + 'mni', + 'mnw', + 'mo', + 'mos', + 'mr', + 'mrh', + 'mrj', + 'ms', + 'ms-arab', + 'mt', + 'mua', + 'mus', + 'mwl', + 'my', + 'myv', + 'mzn', + 'na', + 'nah', + 'nan', + 'nan-hani', + 'nap', + 'nb', + 'nds', + 'nds-nl', + 'ne', + 'new', + 'ng', + 'nge', + 'nia', + 'niu', + 'nl', + 'nl-informal', + 'nla', + 'nmg', + 'nmz', + 'nn', + 'nnh', + 'nnz', + 'no', + 'nod', + 'nog', + 'nov', + 'nqo', + 'nrm', + 'nso', + 'nv', + 'ny', + 'nyn', + 'nys', + 'oc', + 'ojb', + 'olo', + 'om', + 'or', + 'os', + 'osa-latn', + 'ota', + 'pa', + 'pag', + 'pam', + 'pap', + 'pap-aw', + 'pcd', + 'pcm', + 'pdc', + 'pdt', + 'pfl', + 'pi', + 'pih', + 'pl', + 'pms', + 'pnb', + 'pnt', + 'prg', + 'ps', + 'pt', + 'pt-br', + 'pwn', + 'qu', + 'quc', + 'qug', + 'rgn', + 'rif', + 'rki', + 'rm', + 'rmc', + 'rmf', + 'rmy', + 'rn', + 'ro', + 'roa-rup', + 'roa-tara', + 'rsk', + 'ru', + 'rue', + 'rup', + 'ruq', + 'ruq-cyrl', + 'ruq-latn', + 'rw', + 'rwr', + 'ryu', + 'sa', + 'sah', + 'sat', + 'sc', + 'scn', + 'sco', + 'sd', + 'sdc', + 'sdh', + 'se', + 'se-fi', + 'se-no', + 'se-se', + 'sei', + 'ses', + 'sg', + 'sgs', + 'sh', + 'sh-cyrl', + 'sh-latn', + 'shi', + 'shi-latn', + 'shi-tfng', + 'shn', + 'shy', + 'shy-latn', + 'si', + 'simple', + 'sjd', + 'sje', + 'sju', + 'sk', + 'skr', + 'skr-arab', + 'sl', + 'sli', + 'sm', + 'sma', + 'smj', + 'smn', + 'sms', + 'sn', + 'so', + 'sq', + 'sr', + 'sr-ec', + 'sr-el', + 'srn', + 'sro', + 'srq', + 'ss', + 'st', + 'stq', + 'sty', + 'su', + 'sv', + 'sw', + 'syl', + 'szl', + 'szy', + 'ta', + 'tay', + 'tcy', + 'tdd', + 'te', + 'tet', + 'tg', + 'tg-cyrl', + 'tg-latn', + 'th', + 'ti', + 'tk', + 'tl', + 'tly', + 'tly-cyrl', + 'tn', + 'to', + 'tok', + 'tpi', + 'tr', + 'tru', + 'trv', + 'ts', + 'tt', + 'tt-cyrl', + 'tt-latn', + 'tum', + 'tvu', + 'tw', + 'ty', + 'tyv', + 'tzm', + 'udm', + 'ug', + 'ug-arab', + 'ug-latn', + 'uk', + 'ur', + 'uz', + 'uz-cyrl', + 'uz-latn', + 've', + 'vec', + 'vep', + 'vi', + 'vls', + 'vmf', + 'vmw', + 'vo', + 'vot', + 'vro', + 'vut', + 'wa', + 'wal', + 'war', + 'wes', + 'wls', + 'wo', + 'wuu', + 'wya', + 'xal', + 'xh', + 'xmf', + 'xsy', + 'yas', + 'yat', + 'yav', + 'ybb', + 'yi', + 'yo', + 'yrl', + 'yue', + 'za', + 'zea', + 'zgh', + 'zh', + 'zh-classical', + 'zh-cn', + 'zh-hans', + 'zh-hant', + 'zh-hk', + 'zh-min-nan', + 'zh-mo', + 'zh-my', + 'zh-sg', + 'zh-tw', + 'zh-yue', + 'zu', +] as const + +export const languagesWithWiki = [ + 'aa', + 'ab', + 'ace', + 'ady', + 'af', + 'ak', + 'als', + 'alt', + 'am', + 'ami', + 'an', + 'ang', + 'anp', + 'ar', + 'arc', + 'ary', + 'arz', + 'as', + 'ast', + 'atj', + 'av', + 'avk', + 'awa', + 'ay', + 'az', + 'azb', + 'ba', + 'ban', + 'bar', + 'bat-smg', + 'bcl', + 'be', + 'be-x-old', + 'bg', + 'bh', + 'bi', + 'bjn', + 'blk', + 'bm', + 'bn', + 'bo', + 'bpy', + 'br', + 'bs', + 'bug', + 'bxr', + 'ca', + 'cbk-zam', + 'cdo', + 'ce', + 'ceb', + 'ch', + 'cho', + 'chr', + 'chy', + 'ckb', + 'co', + 'cr', + 'crh', + 'cs', + 'csb', + 'cu', + 'cv', + 'cy', + 'da', + 'dag', + 'de', + 'din', + 'diq', + 'dsb', + 'dty', + 'dv', + 'dz', + 'ee', + 'el', + 'eml', + 'en', + 'eo', + 'es', + 'et', + 'eu', + 'ext', + 'fa', + 'ff', + 'fi', + 'fiu-vro', + 'fj', + 'fo', + 'fr', + 'frp', + 'frr', + 'fur', + 'fy', + 'ga', + 'gag', + 'gan', + 'gcr', + 'gd', + 'gl', + 'glk', + 'gn', + 'gom', + 'gor', + 'got', + 'gu', + 'guc', + 'gur', + 'guw', + 'gv', + 'ha', + 'hak', + 'haw', + 'he', + 'hi', + 'hif', + 'ho', + 'hr', + 'hsb', + 'ht', + 'hu', + 'hy', + 'hyw', + 'hz', + 'ia', + 'id', + 'ie', + 'ig', + 'ii', + 'ik', + 'ilo', + 'inh', + 'io', + 'is', + 'it', + 'iu', + 'ja', + 'jam', + 'jbo', + 'jv', + 'ka', + 'kaa', + 'kab', + 'kbd', + 'kbp', + 'kcg', + 'kg', + 'ki', + 'kj', + 'kk', + 'kl', + 'km', + 'kn', + 'ko', + 'koi', + 'kr', + 'krc', + 'ks', + 'ksh', + 'ku', + 'kv', + 'kw', + 'ky', + 'la', + 'lad', + 'lb', + 'lbe', + 'lez', + 'lfn', + 'lg', + 'li', + 'lij', + 'lld', + 'lmo', + 'ln', + 'lo', + 'lrc', + 'lt', + 'ltg', + 'lv', + 'mad', + 'mai', + 'map-bms', + 'mdf', + 'mg', + 'mh', + 'mhr', + 'mi', + 'min', + 'mk', + 'ml', + 'mn', + 'mni', + 'mnw', + 'mo', + 'mr', + 'mrj', + 'ms', + 'mt', + 'mus', + 'mwl', + 'my', + 'myv', + 'mzn', + 'na', + 'nah', + 'nap', + 'nds', + 'nds-nl', + 'ne', + 'new', + 'ng', + 'nia', + 'nl', + 'nn', + 'no', + 'nov', + 'nqo', + 'nrm', + 'nso', + 'nv', + 'ny', + 'oc', + 'olo', + 'om', + 'or', + 'os', + 'pa', + 'pag', + 'pam', + 'pap', + 'pcd', + 'pcm', + 'pdc', + 'pfl', + 'pi', + 'pih', + 'pl', + 'pms', + 'pnb', + 'pnt', + 'ps', + 'pt', + 'pwn', + 'qu', + 'rm', + 'rmy', + 'rn', + 'ro', + 'roa-rup', + 'roa-tara', + 'ru', + 'rue', + 'rw', + 'sa', + 'sah', + 'sat', + 'sc', + 'scn', + 'sco', + 'sd', + 'se', + 'sg', + 'sh', + 'shi', + 'shn', + 'si', + 'simple', + 'sk', + 'skr', + 'sl', + 'sm', + 'smn', + 'sn', + 'so', + 'sq', + 'sr', + 'srn', + 'ss', + 'st', + 'stq', + 'su', + 'sv', + 'sw', + 'szl', + 'szy', + 'ta', + 'tay', + 'tcy', + 'te', + 'tet', + 'tg', + 'th', + 'ti', + 'tk', + 'tl', + 'tn', + 'to', + 'tpi', + 'tr', + 'trv', + 'ts', + 'tt', + 'tum', + 'tw', + 'ty', + 'tyv', + 'udm', + 'ug', + 'uk', + 'ur', + 'uz', + 've', + 'vec', + 'vep', + 'vi', + 'vls', + 'vo', + 'wa', + 'war', + 'wo', + 'wuu', + 'xal', + 'xh', + 'xmf', + 'yi', + 'yo', + 'za', + 'zea', + 'zh', + 'zh-classical', + 'zh-min-nan', + 'zh-yue', + 'zu', +] as const diff --git a/src/index.ts b/src/index.ts index 765c7b6c..1672ed99 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ export * from './wikibase-sdk.js' export * from './helpers/helpers.js' export * from './helpers/rank.js' export * from './helpers/sitelinks.js' +export * from './helpers/wikimedia_constants.js' export * as parse from './helpers/parse_responses.js' export * as simplify from './helpers/simplify.js' export * from './helpers/simplify_claims.js' diff --git a/src/queries/get_entities.ts b/src/queries/get_entities.ts index fd3b5b04..f27398b6 100644 --- a/src/queries/get_entities.ts +++ b/src/queries/get_entities.ts @@ -1,13 +1,14 @@ import * as validate from '../helpers/validate.js' -import { forceArray, rejectObsoleteInterface, shortLang } from '../utils/utils.js' +import { forceArray, rejectObsoleteInterface } from '../utils/utils.js' +import type { Language } from '../helpers/wikimedia_constants.js' import type { EntityId } from '../types/entity.js' -import type { Props, Url, UrlResultFormat, WmLanguageCode } from '../types/options.js' +import type { Props, Url, UrlResultFormat } from '../types/options.js' import type { WbGetEntities } from '../types/wbgetentities.js' import type { BuildUrlFunction } from '../utils/build_url.js' export interface GetEntitiesOptions { ids: EntityId | EntityId[] - languages?: WmLanguageCode | WmLanguageCode[] + languages?: Language | Language[] props?: Props | Props[] format?: UrlResultFormat redirects?: boolean @@ -51,7 +52,7 @@ export function getEntitiesFactory (buildUrl: BuildUrlFunction) { if (redirects === false) query.redirects = 'no' if (languages) { - languages = forceArray(languages).map(shortLang) + languages = forceArray(languages) query.languages = languages.join('|') } diff --git a/src/queries/get_entities_from_sitelinks.ts b/src/queries/get_entities_from_sitelinks.ts index 4b676ac4..1f25b21b 100644 --- a/src/queries/get_entities_from_sitelinks.ts +++ b/src/queries/get_entities_from_sitelinks.ts @@ -1,14 +1,14 @@ -import { languages } from '../helpers/sitelinks_languages.js' -import { forceArray, shortLang, rejectObsoleteInterface, isOfType } from '../utils/utils.js' -import type { Props, Url, UrlResultFormat, WmLanguageCode } from '../types/options.js' -import type { Site } from '../types/sitelinks.js' +import { sites } from '../helpers/wikimedia_constants.js' +import { forceArray, rejectObsoleteInterface, isOfType } from '../utils/utils.js' +import type { Language, LanguageWithWiki, Site } from '../helpers/wikimedia_constants.js' +import type { Props, Url, UrlResultFormat } from '../types/options.js' import type { WbGetEntities } from '../types/wbgetentities.js' import type { BuildUrlFunction } from '../utils/build_url.js' export interface GetEntitiesFromSitelinksOptions { titles: string | string[] sites?: Site | Site[] - languages?: WmLanguageCode | WmLanguageCode[] + languages?: Language | Language[] props?: Props | Props[] format?: UrlResultFormat redirects?: boolean @@ -50,7 +50,7 @@ export function getEntitiesFromSitelinksFactory (buildUrl: BuildUrlFunction) { } if (languages) { - languages = forceArray(languages).map(shortLang) + languages = forceArray(languages) query.languages = languages.join('|') } @@ -65,12 +65,11 @@ export function getEntitiesFromSitelinksFactory (buildUrl: BuildUrlFunction) { } /** convert language code to Wikipedia sitelink code */ -function parseSite (site: Site | WmLanguageCode): Site { - if (isOfType(languages, site)) { - // The `as Site` conversion shouldnt be needed but WmLanguageCode and Site do not seem to be perfectly in sync? - // Both are created by scripts so this is also out of sync on the Wikimedia projects? - return `${site}wiki` as Site - } else { +function parseSite (site: Site | LanguageWithWiki): Site { + if (isOfType(sites, site)) { return site } + + const wiki = site.replace(/-/g, '_') + 'wiki' + return wiki as Site } diff --git a/src/types/options.ts b/src/types/options.ts index 66667f81..a6f76741 100644 --- a/src/types/options.ts +++ b/src/types/options.ts @@ -1,5 +1,4 @@ import type { SimplifySnaksOptions } from './simplify_claims.js' -import type { languages } from '../helpers/sitelinks_languages.js' export interface InstanceConfig { instance?: string @@ -9,7 +8,6 @@ export interface InstanceConfig { export type Props = 'info' | 'sitelinks' | 'sitelinks/urls' | 'aliases' | 'labels' | 'descriptions' | 'claims' | 'datatype' export type UrlResultFormat = 'xml' | 'json' -export type WmLanguageCode = typeof languages[number] export type ApiQueryParameters = Record diff --git a/src/types/sitelinks.ts b/src/types/sitelinks.ts index 97b84bfe..74c1ad44 100644 --- a/src/types/sitelinks.ts +++ b/src/types/sitelinks.ts @@ -1,12 +1,6 @@ import type { ItemId } from './entity.js' import type { Url } from './options.js' -import type { sites } from '../helpers/sitelinks_sites.js' -import type { specialSites } from '../helpers/special_sites.js' - -type ValueOf = T[keyof T] -type SpecialSiteName = ValueOf - -export type Site = typeof sites[number] | SpecialSiteName +import type { Site } from '../helpers/wikimedia_constants.js' export interface Sitelink { site: Site diff --git a/src/types/terms.ts b/src/types/terms.ts index 327b2130..7992bf89 100644 --- a/src/types/terms.ts +++ b/src/types/terms.ts @@ -1,24 +1,24 @@ -import type { WmLanguageCode } from './options.js' +import type { Language } from '../helpers/wikimedia_constants.js' -type WmLanguageRecord = Partial>> +type LanguageRecord = Partial>> export type Term = { - readonly language: WmLanguageCode + readonly language: Language readonly value: string } -export type Labels = WmLanguageRecord -export type Descriptions = WmLanguageRecord -export type Aliases = WmLanguageRecord -export type Lemmas = WmLanguageRecord -export type Representations = WmLanguageRecord -export type Glosses = WmLanguageRecord +export type Labels = LanguageRecord +export type Descriptions = LanguageRecord +export type Aliases = LanguageRecord +export type Lemmas = LanguageRecord +export type Representations = LanguageRecord +export type Glosses = LanguageRecord export type SimplifiedTerm = string -export type SimplifiedLabels = WmLanguageRecord -export type SimplifiedDescriptions = WmLanguageRecord -export type SimplifiedAliases = WmLanguageRecord -export type SimplifiedLemmas = WmLanguageRecord -export type SimplifiedRepresentations = WmLanguageRecord -export type SimplifiedGlosses = WmLanguageRecord +export type SimplifiedLabels = LanguageRecord +export type SimplifiedDescriptions = LanguageRecord +export type SimplifiedAliases = LanguageRecord +export type SimplifiedLemmas = LanguageRecord +export type SimplifiedRepresentations = LanguageRecord +export type SimplifiedGlosses = LanguageRecord diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 093d5feb..6cda2fe3 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,11 +1,3 @@ -import type { WmLanguageCode } from '../types/options.js' - -/** Example: keep only 'fr' in 'fr_FR' */ -export function shortLang (language: string): WmLanguageCode { - const lang = language.toLowerCase().split('_')[0] as WmLanguageCode - return lang -} - /** * a polymorphism helper: * accept either a string or an array and return an array @@ -51,7 +43,7 @@ export function rejectObsoleteInterface (args: IArguments): void { /** * Checks if the `element` is of one of the entries of `all` - * @example const isWmLanguageCode: lang is WmLanguageCode = isOfType(languages, lang) + * @example const isLanguage: lang is Language = isOfType(languages, lang) */ export function isOfType (all: readonly T[], element: unknown): element is T { return typeof element === 'string' && (all as readonly string[]).includes(element) diff --git a/tests/sitelinks_helpers.ts b/tests/sitelinks_helpers.ts index 2646cf68..4e43bf7c 100644 --- a/tests/sitelinks_helpers.ts +++ b/tests/sitelinks_helpers.ts @@ -156,7 +156,7 @@ describe('getSitelinkData', () => { it('should support multi-part language codes', () => { const data = getSitelinkData('https://be-x-old.wikipedia.org/wiki/Беларускі_клясычны_правапіс') data.title.should.equal('Беларускі_клясычны_правапіс') - data.lang.should.equal('be_x_old') + data.lang.should.equal('be-x-old') data.project.should.equal('wikipedia') data.key.should.equal('be_x_oldwiki') }) From 7fb9c4832afdd9df3c43516ebea698a36730c3e5 Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Fri, 24 Mar 2023 16:00:12 +0100 Subject: [PATCH 3/4] fix: do not remove shortLang Its exported currently so removing it would be a breaking change. This method can not make any type guarantees other than string. So fix the types but dont remove it. --- src/utils/utils.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 6cda2fe3..2dad6b86 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,3 +1,8 @@ +/** Example: keep only 'de' in 'de_CH' or 'de-ch' */ +export function shortLang (language: string): string { + return language.toLowerCase().split(/_-/)[0] +} + /** * a polymorphism helper: * accept either a string or an array and return an array From 0469b47718ea93db01260cae483307cf4974406e Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Fri, 24 Mar 2023 16:01:01 +0100 Subject: [PATCH 4/4] fix: do not remove WmLanguageCode removing it would be breaking which this shouldnt be. Mark it as deprecated to be removed with the next major release. --- src/types/options.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/types/options.ts b/src/types/options.ts index a6f76741..2a550db5 100644 --- a/src/types/options.ts +++ b/src/types/options.ts @@ -1,4 +1,5 @@ import type { SimplifySnaksOptions } from './simplify_claims.js' +import type { Language } from '../helpers/wikimedia_constants.js' export interface InstanceConfig { instance?: string @@ -9,6 +10,9 @@ export interface InstanceConfig { export type Props = 'info' | 'sitelinks' | 'sitelinks/urls' | 'aliases' | 'labels' | 'descriptions' | 'claims' | 'datatype' export type UrlResultFormat = 'xml' | 'json' +/** @deprecated use Language */ +export type WmLanguageCode = Language + export type ApiQueryParameters = Record // export type Url = `http${string}`