-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRule.js
More file actions
34 lines (31 loc) · 858 Bytes
/
Rule.js
File metadata and controls
34 lines (31 loc) · 858 Bytes
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
export default class Rule {
constructor(regex, assign, template, index) {
this.regex = regex
this.assign = assign
this.template = template
this.index = index
}
getAssigned(content) {
let assigned = []
const matches = [...content.matchAll(this.regex)]
matches.forEach(match => {
let entry = this.assign(match)
entry.index = match.index
entry.length = match[0].length
entry.end = match.index + match[0].length - 1
assigned = [...assigned, entry]
})
return assigned
}
getAssignedMarkups(content) {
const assigned = this.getAssigned(content)
let entries = []
assigned.forEach(entry => {
let withMarkup = {}
Object.assign(withMarkup, entry)
withMarkup.markup = this.template(entry)
entries = [...entries, withMarkup]
})
return entries
}
}