label.js 1.8 KB

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