author.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict'
  2. const assert = require('check-types').assert
  3. /**
  4. * An author of a {@link Change}. We want to store user IDs, and then fill in
  5. * the other properties (which the user can change over time) when changes are
  6. * loaded.
  7. *
  8. * At present, we're assuming that all authors have a user ID; we may need to
  9. * generalise this to cover users for whom we only have a name and email, e.g.
  10. * from git. For now, though, this seems to do what we need.
  11. */
  12. class Author {
  13. /**
  14. * @param {number} id
  15. * @param {string} email
  16. * @param {string} name
  17. */
  18. constructor(id, email, name) {
  19. assert.number(id, 'bad id')
  20. assert.string(email, 'bad email')
  21. assert.string(name, 'bad name')
  22. this.id = id
  23. this.email = email
  24. this.name = name
  25. }
  26. /**
  27. * Create an Author from its raw form.
  28. *
  29. * @param {Object} [raw]
  30. * @return {Author | null}
  31. */
  32. static fromRaw(raw) {
  33. if (!raw) return null
  34. return new Author(raw.id, raw.email, raw.name)
  35. }
  36. /**
  37. * Convert the Author to raw form for storage or transmission.
  38. *
  39. * @return {Object}
  40. */
  41. toRaw() {
  42. return { id: this.id, email: this.email, name: this.name }
  43. }
  44. /**
  45. * @return {number}
  46. */
  47. getId() {
  48. return this.id
  49. }
  50. /**
  51. * @return {string}
  52. */
  53. getEmail() {
  54. return this.email
  55. }
  56. /**
  57. * @return {string}
  58. */
  59. getName() {
  60. return this.name
  61. }
  62. }
  63. module.exports = Author