v2_doc_versions.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. 'use strict'
  2. const _ = require('lodash')
  3. /**
  4. * @typedef {import("./file")} File
  5. * @typedef {import("./types").RawV2DocVersions} RawV2DocVersions
  6. */
  7. /**
  8. * @constructor
  9. * @param {RawV2DocVersions} data
  10. * @classdesc
  11. */
  12. function V2DocVersions(data) {
  13. this.data = data || {}
  14. }
  15. V2DocVersions.fromRaw = function v2DocVersionsFromRaw(raw) {
  16. if (!raw) return undefined
  17. return new V2DocVersions(raw)
  18. }
  19. /**
  20. * @abstract
  21. */
  22. V2DocVersions.prototype.toRaw = function () {
  23. if (!this.data) return null
  24. const raw = _.clone(this.data)
  25. return raw
  26. }
  27. /**
  28. * Clone this object.
  29. *
  30. * @return {V2DocVersions} a new object of the same type
  31. */
  32. V2DocVersions.prototype.clone = function v2DocVersionsClone() {
  33. return V2DocVersions.fromRaw(this.toRaw())
  34. }
  35. V2DocVersions.prototype.applyTo = function v2DocVersionsApplyTo(snapshot) {
  36. // Only update the snapshot versions if we have new versions
  37. if (!_.size(this.data)) return
  38. // Create v2DocVersions in snapshot if it does not exist
  39. // otherwise update snapshot v2docversions
  40. if (!snapshot.v2DocVersions) {
  41. snapshot.v2DocVersions = this.clone()
  42. } else {
  43. _.assign(snapshot.v2DocVersions.data, this.data)
  44. }
  45. }
  46. module.exports = V2DocVersions