label.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // @ts-check
  2. 'use strict'
  3. const assert = require('check-types').assert
  4. /**
  5. * @typedef {import('./types').RawLabel} RawLabel
  6. */
  7. /**
  8. * @classdesc
  9. * A user-configurable label that can be attached to a specific change. Labels
  10. * are not versioned, and they are not stored alongside the Changes in Chunks.
  11. * They are instead intended to provide external markers into the history of the
  12. * project.
  13. */
  14. class Label {
  15. /**
  16. * @constructor
  17. * @param {string} text
  18. * @param {number?} authorId
  19. * @param {Date} timestamp
  20. * @param {number} version
  21. */
  22. constructor(text, authorId, timestamp, version) {
  23. assert.string(text, 'bad text')
  24. assert.maybe.integer(authorId, 'bad author id')
  25. assert.date(timestamp, 'bad timestamp')
  26. assert.integer(version, 'bad version')
  27. this.text = text
  28. this.authorId = authorId
  29. this.timestamp = timestamp
  30. this.version = version
  31. }
  32. /**
  33. * Create a Label from its raw form.
  34. *
  35. * @param {RawLabel} raw
  36. * @return {Label}
  37. */
  38. static fromRaw(raw) {
  39. return new Label(
  40. raw.text,
  41. raw.authorId,
  42. new Date(raw.timestamp),
  43. raw.version
  44. )
  45. }
  46. /**
  47. * Convert the Label to raw form for transmission.
  48. *
  49. * @return {RawLabel}
  50. */
  51. toRaw() {
  52. return {
  53. text: this.text,
  54. authorId: this.authorId,
  55. timestamp: this.timestamp.toISOString(),
  56. version: this.version,
  57. }
  58. }
  59. /**
  60. * @return {string}
  61. */
  62. getText() {
  63. return this.text
  64. }
  65. /**
  66. * The ID of the author, if any. Note that we now require all saved versions to
  67. * have an author, but this was not always the case, so we have to allow nulls
  68. * here for historical reasons.
  69. *
  70. * @return {number | null | undefined}
  71. */
  72. getAuthorId() {
  73. return this.authorId
  74. }
  75. /**
  76. * @return {Date}
  77. */
  78. getTimestamp() {
  79. return this.timestamp
  80. }
  81. /**
  82. * @return {number}
  83. */
  84. getVersion() {
  85. return this.version
  86. }
  87. }
  88. module.exports = Label