author.js 1.4 KB

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