Cite/get.js

  1. import {validateOutputOptions as validate} from './static'
  2. import {format as formatData} from '../get/registrar'
  3. import {csl as parseCsl} from '../parse/'
  4. /**
  5. * Get a list of the data entry IDs, in the order of that list
  6. *
  7. * @memberof Cite#
  8. *
  9. * @return {Array<String>} List of IDs
  10. */
  11. const getIds = function () {
  12. return this.data.map(entry => entry.id)
  13. }
  14. /**
  15. * Get formatted data from your object.
  16. *
  17. * @memberof Cite#
  18. *
  19. * @param {String} format - format module name
  20. * @param {...*} options - module options (see relevant documentation)
  21. *
  22. * @return {String|Arrat<Object>} formatted data
  23. */
  24. const format = function (format, ...options) {
  25. return formatData(format, parseCsl(this.data), ...options)
  26. }
  27. /**
  28. * Get formatted data from your object.
  29. *
  30. * @tutorial output
  31. * @memberof Cite#
  32. * @deprecated use {@link Cite#format}
  33. *
  34. * @param {Cite~OutputOptions} [options={}] - Output options
  35. *
  36. * @return {String|Array<Object>} The formatted data
  37. */
  38. const get = function (options = {}) {
  39. try {
  40. validate(options)
  41. } catch ({message}) {
  42. logger.error('[get]', message)
  43. }
  44. const parsedOptions = Object.assign({}, this.defaultOptions, this._options.output, options)
  45. const {type, style} = parsedOptions
  46. const [styleType, styleFormat] = style.split('-')
  47. const newStyle = styleType === 'citation' ? 'bibliography' : styleType === 'csl' ? 'data' : styleType
  48. const newType = type === 'string' ? 'text' : type === 'json' ? 'object' : type
  49. let formatOptions
  50. switch (newStyle) {
  51. case 'bibliography':
  52. const {lang, append, prepend} = parsedOptions
  53. formatOptions = {template: styleFormat, lang, format: newType, append, prepend}
  54. break
  55. case 'data':
  56. case 'bibtex':
  57. case 'bibtxt':
  58. formatOptions = {type: newType}
  59. break
  60. default:
  61. logger.error('[get]', 'Invalid options')
  62. break
  63. }
  64. const result = this.format(newStyle, formatOptions)
  65. const {format} = parsedOptions
  66. if (format === 'real' && newType === 'html' && typeof document !== 'undefined' && typeof document.createElement === 'function') {
  67. const tmp = document.createElement('div')
  68. tmp.innerHTML = result
  69. return tmp.firstChild
  70. } else if (format === 'string' && typeof result === 'object') {
  71. return JSON.stringify(result)
  72. } else {
  73. return result
  74. }
  75. }
  76. export {format, getIds, get}