2024-08-07 14:51:44 +00:00
|
|
|
// @ts-check
|
2023-01-13 12:42:29 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const assert = require('check-types').assert
|
|
|
|
|
2024-08-07 14:51:44 +00:00
|
|
|
/**
|
2024-09-20 13:52:23 +00:00
|
|
|
* @import { RawLabel } from './types'
|
2024-08-07 14:51:44 +00:00
|
|
|
*/
|
|
|
|
|
2023-01-13 12:42:29 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* A user-configurable label that can be attached to a specific change. Labels
|
|
|
|
* are not versioned, and they are not stored alongside the Changes in Chunks.
|
|
|
|
* They are instead intended to provide external markers into the history of the
|
|
|
|
* project.
|
|
|
|
*/
|
2023-08-22 14:48:04 +00:00
|
|
|
class Label {
|
|
|
|
/**
|
|
|
|
* @constructor
|
|
|
|
* @param {string} text
|
2024-08-07 14:51:44 +00:00
|
|
|
* @param {number?} authorId
|
|
|
|
* @param {Date} timestamp
|
|
|
|
* @param {number} version
|
2023-08-22 14:48:04 +00:00
|
|
|
*/
|
|
|
|
constructor(text, authorId, timestamp, version) {
|
|
|
|
assert.string(text, 'bad text')
|
|
|
|
assert.maybe.integer(authorId, 'bad author id')
|
|
|
|
assert.date(timestamp, 'bad timestamp')
|
|
|
|
assert.integer(version, 'bad version')
|
2023-01-13 12:42:29 +00:00
|
|
|
|
2023-08-22 14:48:04 +00:00
|
|
|
this.text = text
|
|
|
|
this.authorId = authorId
|
|
|
|
this.timestamp = timestamp
|
|
|
|
this.version = version
|
|
|
|
}
|
2023-01-13 12:42:29 +00:00
|
|
|
|
2023-08-22 14:48:04 +00:00
|
|
|
/**
|
|
|
|
* Create a Label from its raw form.
|
|
|
|
*
|
2024-08-07 14:51:44 +00:00
|
|
|
* @param {RawLabel} raw
|
2023-08-22 14:48:04 +00:00
|
|
|
* @return {Label}
|
|
|
|
*/
|
|
|
|
static fromRaw(raw) {
|
|
|
|
return new Label(
|
|
|
|
raw.text,
|
|
|
|
raw.authorId,
|
|
|
|
new Date(raw.timestamp),
|
|
|
|
raw.version
|
|
|
|
)
|
|
|
|
}
|
2023-01-13 12:42:29 +00:00
|
|
|
|
2023-08-22 14:48:04 +00:00
|
|
|
/**
|
|
|
|
* Convert the Label to raw form for transmission.
|
|
|
|
*
|
2024-08-07 14:51:44 +00:00
|
|
|
* @return {RawLabel}
|
2023-08-22 14:48:04 +00:00
|
|
|
*/
|
|
|
|
toRaw() {
|
|
|
|
return {
|
|
|
|
text: this.text,
|
|
|
|
authorId: this.authorId,
|
|
|
|
timestamp: this.timestamp.toISOString(),
|
|
|
|
version: this.version,
|
|
|
|
}
|
2023-01-13 12:42:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 14:48:04 +00:00
|
|
|
/**
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
getText() {
|
|
|
|
return this.text
|
|
|
|
}
|
2023-01-13 12:42:29 +00:00
|
|
|
|
2023-08-22 14:48:04 +00:00
|
|
|
/**
|
|
|
|
* The ID of the author, if any. Note that we now require all saved versions to
|
|
|
|
* have an author, but this was not always the case, so we have to allow nulls
|
|
|
|
* here for historical reasons.
|
|
|
|
*
|
|
|
|
* @return {number | null | undefined}
|
|
|
|
*/
|
|
|
|
getAuthorId() {
|
|
|
|
return this.authorId
|
|
|
|
}
|
2023-01-13 12:42:29 +00:00
|
|
|
|
2023-08-22 14:48:04 +00:00
|
|
|
/**
|
|
|
|
* @return {Date}
|
|
|
|
*/
|
|
|
|
getTimestamp() {
|
|
|
|
return this.timestamp
|
|
|
|
}
|
2023-01-13 12:42:29 +00:00
|
|
|
|
2023-08-22 14:48:04 +00:00
|
|
|
/**
|
2024-08-07 14:51:44 +00:00
|
|
|
* @return {number}
|
2023-08-22 14:48:04 +00:00
|
|
|
*/
|
|
|
|
getVersion() {
|
|
|
|
return this.version
|
|
|
|
}
|
2023-01-13 12:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Label
|