label.js 1.8 KB

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