parse/modules/wikidata/json.js

  1. /**
  2. * @module input/wikidata
  3. */
  4. import wdk from 'wikidata-sdk'
  5. import {
  6. parse as parseWikidataProp,
  7. parseAsync as parseWikidataPropAsync
  8. } from './prop'
  9. /**
  10. * Format Wikidata data (async)
  11. *
  12. * @access protected
  13. * @method parseWikidataJSONAsync
  14. *
  15. * @param {Object} data - The input data
  16. *
  17. * @return {Promise<Array<CSL>>} The formatted input data
  18. */
  19. const parseWikidataJSONAsync = async function (data) {
  20. return Promise.all(Object.keys(data.entities).map(async function (entityKey) {
  21. const {labels, claims} = data.entities[entityKey]
  22. const entity = wdk.simplifyClaims(claims, null, null, true)
  23. const json = {
  24. _wikiId: entityKey,
  25. id: entityKey
  26. }
  27. await Promise.all(Object.keys(entity).map(async prop => {
  28. const field = await parseWikidataPropAsync(prop, entity[prop], 'en')
  29. if (field) {
  30. const [fieldName, fieldValue] = field
  31. if (Array.isArray(json[fieldName])) {
  32. json[fieldName] = json[fieldName].concat(fieldValue)
  33. } else if (fieldValue !== undefined) {
  34. json[fieldName] = fieldValue
  35. }
  36. }
  37. }))
  38. if (Array.isArray(json.author)) {
  39. json.author.sort(({_ordinal: a}, {_ordinal: b}) => a - b)
  40. }
  41. if (!json.title) {
  42. json.title = labels['en'].value
  43. }
  44. return json
  45. }))
  46. }
  47. /**
  48. * Format Wikidata data
  49. *
  50. * @access protected
  51. * @method parseWikidataJSON
  52. *
  53. * @param {Object} data - The input data
  54. *
  55. * @return {Array<CSL>} The formatted input data
  56. */
  57. const parseWikidataJSON = function (data) {
  58. return Object.keys(data.entities).map((entityKey) => {
  59. const {labels, claims} = data.entities[entityKey]
  60. const entity = wdk.simplifyClaims(claims, null, null, true)
  61. const json = {
  62. _wikiId: entityKey,
  63. id: entityKey
  64. }
  65. Object.keys(entity).forEach((prop) => {
  66. const field = parseWikidataProp(prop, entity[prop], 'en')
  67. if (field) {
  68. const [fieldName, fieldValue] = field
  69. if (Array.isArray(json[fieldName])) {
  70. json[fieldName] = json[fieldName].concat(fieldValue)
  71. } else if (fieldValue !== undefined) {
  72. json[fieldName] = fieldValue
  73. }
  74. }
  75. })
  76. if (Array.isArray(json.author)) {
  77. json.author.sort(({_ordinal: a}, {_ordinal: b}) => a - b)
  78. }
  79. if (!json.title) {
  80. json.title = labels['en'].value
  81. }
  82. return json
  83. })
  84. }
  85. export {
  86. parseWikidataJSON as parse,
  87. parseWikidataJSONAsync as parseAsync,
  88. parseWikidataJSON as default
  89. }