-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReview.js
More file actions
57 lines (47 loc) · 1.32 KB
/
Copy pathReview.js
File metadata and controls
57 lines (47 loc) · 1.32 KB
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
import { toDomElement } from './domUtils'
class Review {
constructor({ title, rate, keywords = [] }) {
this.title = title
this.rate = rate
this.keywords = new Set(keywords)
}
toString() {
let qualifier
if (typeof this.rate !== 'number') {
qualifier = 'invalid note'
} else if (this.rate === 5) {
qualifier = 'amazing'
} else if (this.rate <= 4 && this.rate >= 3) {
qualifier = 'good'
} else {
qualifier = 'bad'
}
return `${this.title} - ${qualifier} [${[...this.keywords].join(', ')}]`
}
[toDomElement]() {
const div = document.createElement('div')
div.innerHTML = String(this)
return div
}
}
Review.prototype = new Proxy(Review.prototype, {
get(target, propKey, receiver) {
if (!propKey || !propKey.match) {
return Reflect.get(target, propKey, receiver)
}
const matches = propKey.match(/^has(.+)Keyword/)
if (!matches) {
return Reflect.get(target, propKey, receiver)
}
const [, matchingKeyword] = matches
return () =>
[...receiver.keywords].some(
keyword => keyword.toLowerCase() === matchingKeyword.toLowerCase(),
)
},
})
const BLACKLIST = ['shit']
export function getInvalidKeyWords(...keywords) {
return keywords.filter(keyword => BLACKLIST.includes(keyword))
}
export default Review