v2_doc_versions.js 1.1 KB

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