UNPKG

wikibase-sdk

Version:

utils functions to query a Wikibase instance and simplify its results

30 lines 1.14 kB
import { typedKeys } from '../utils/utils.js'; import { getSitelinkUrl } from './sitelinks.js'; export function simplifySitelinks(sitelinks, options = {}) { let { addUrl, keepBadges, keepAll } = options; keepBadges = keepBadges || keepAll; return typedKeys(sitelinks).reduce(aggregateValuesFactory(sitelinks, addUrl, keepBadges), {}); } function aggregateValuesFactory(sitelinks, addUrl, keepBadges) { return function aggregateValues(index, key) { // Accomodating for wikibase-cli, which might set the sitelink to null // to signify that a requested sitelink was not found if (sitelinks[key] == null) { index[key] = null; return index; } const { title, badges } = sitelinks[key]; if (addUrl || keepBadges) { index[key] = { title }; if (addUrl) index[key].url = getSitelinkUrl({ site: key, title }); if (keepBadges) index[key].badges = badges; } else { index[key] = title; } return index; }; } //# sourceMappingURL=simplify_sitelinks.js.map