Cite/set.js

  1. import {chain as parseInput, chainAsync as parseInputAsync} from '../parse/'
  2. import fetchId from '../util/fetchId'
  3. /**
  4. * Add an object to the array of objects
  5. *
  6. * @memberof Cite#
  7. *
  8. * @param {Cite~InputData} data - The data to add to your object
  9. * @param {Object} [options={}] - [Options](../#cite.in.options)
  10. * @param {Boolean} [log=false] - Show this call in the log
  11. *
  12. * @return {Cite} The updated parent object
  13. */
  14. const add = function (data, options = {}, log = false) {
  15. if (options === true || log === true) {
  16. this.save()
  17. }
  18. this.data.push(...parseInput(data, options))
  19. this.data.filter(entry => !entry.hasOwnProperty('id')).forEach(entry => {
  20. entry.id = fetchId(this.getIds(), 'temp_id_')
  21. })
  22. return this
  23. }
  24. /**
  25. * Add an object to the array of objects
  26. *
  27. * @memberof Cite#
  28. *
  29. * @param {Cite~InputData} data - The data to add to your object
  30. * @param {Object} [options={}] - [Options](../#cite.in.options)
  31. * @param {Boolean} [log=false] - Show this call in the log
  32. *
  33. * @return {Promise<Cite>} The updated parent object
  34. */
  35. const addAsync = async function (data, options = {}, log = false) {
  36. if (options === true || log === true) {
  37. this.save()
  38. }
  39. this.data.push(...await parseInputAsync(data, options))
  40. this.data.filter(entry => !entry.hasOwnProperty('id')).forEach(entry => {
  41. entry.id = fetchId(this.getIds(), 'temp_id_')
  42. })
  43. return this
  44. }
  45. /**
  46. * Recreate a `Cite` object with almost any kind of data, and manipulate it with its default methods.
  47. *
  48. * @memberof Cite#
  49. *
  50. * @param {Cite~InputData} data - Replacement data
  51. * @param {Object} [options={}] - [Options](../#cite.in.options)
  52. * @param {Boolean} [log=false] - Show this call in the log
  53. *
  54. * @return {Cite} The updated parent object
  55. */
  56. const set = function (data, options = {}, log = false) {
  57. if (options === true || log === true) {
  58. this.save()
  59. }
  60. this.data = []
  61. return typeof options !== 'boolean' ? this.add(data, options) : this.add(data)
  62. }
  63. /**
  64. * Recreate a `Cite` object with almost any kind of data, and manipulate it with its default methods.
  65. *
  66. * @memberof Cite#
  67. *
  68. * @param {Cite~InputData} data - Replacement data
  69. * @param {Object} [options={}] - [Options](../#cite.in.options)
  70. * @param {Boolean} [log=false] - Show this call in the log
  71. *
  72. * @return {Promise<Cite>} The updated parent object
  73. */
  74. const setAsync = async function (data, options = {}, log = false) {
  75. if (options === true || log === true) {
  76. this.save()
  77. }
  78. this.data = []
  79. return typeof options !== 'boolean' ? this.addAsync(data, options) : this.addAsync(data)
  80. }
  81. /**
  82. * Reset a `Cite` object.
  83. *
  84. * @memberof Cite#
  85. *
  86. * @param {Boolean} [log=false] - Show this call in the log
  87. *
  88. * @return {Cite} The updated, empty parent object (except the log, the log lives)
  89. */
  90. const reset = function (log) {
  91. if (log) {
  92. this.save()
  93. }
  94. this.data = []
  95. this._options = {}
  96. return this
  97. }
  98. export { add, addAsync, set, setAsync, reset }