-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcommenter.js
64 lines (56 loc) · 1.38 KB
/
commenter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
export default class Commenter {
/**
* Unified commenter module
*
* @constructor
* @public
*/
constructor() {
this.comments = [];
}
/**
* Add a comment to the queue
*
* @memberof Commenter
* @public
* @param {String} msg The message to post
* @param {Commenter.priority} priority The priority of the comment
*/
addComment(msg, priority) {
if (!msg || typeof priority !== 'number' || priority < Commenter.priority.Low || priority > Commenter.priority.High) {
throw new Error('Missing message or priority');
}
this.comments.push({
msg,
priority
});
}
/**
* Flush the comment queue to a sorted string
*
* @memberof Commenter
* @public
* @returns {String} All the queued comments, sorted by descending order, concatenated into a string with appropriate formatting
*/
flushToString() {
if (this.comments.length === 0) return null;
this.comments.sort((a, b) => {
return a.priority === b.priority ? 0 : a.priority > b.priority ? -1 : 1; // eslint-disable-line no-nested-ternary
});
const commentList = this.comments.map(c => c.msg).join('\n\n---\n\n');
this.comments = [];
return commentList;
}
/**
* Priority options to pass to `addComment`
*
* @enum {Number}
* @readonly
* @public
*/
static priority = {
Low: 0,
Medium: 1,
High: 2
};
}