Passed
Push — master ( b893da...e7ac93 )
by Andreas
03:30 queued 15s
created

script/custom/SmileyConf.js   A

Complexity

Total Complexity 4
Complexity/F 1

Size

Lines of Code 29
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 4
mnd 0
bc 0
fnc 4
bpm 0
cpm 1
noi 1

3 Functions

Rating   Name   Duplication   Size   Complexity  
A SmileyConf.escapeRegExp 0 3 1
A SmileyConf.getSmileys 0 3 1
A SmileyConf.getRegex 0 5 2
1
export default class SmileyConf {
2
    static getSmileys() {
3
        return JSINFO.SMILEY_CONF;
0 ignored issues
show
Bug introduced by
The variable JSINFO seems to be never declared. If this is a global, consider adding a /** global: JSINFO */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
4
    }
5
6
    /**
7
     * Regex escape as recommended by MDN
8
     *
9
     * @param {string} string
10
     * @returns {string}
11
     */
12
    static escapeRegExp(string) {
13
        return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
14
    }
15
16
    /**
17
     * Build Regex from conf
18
     *
19
     * Similar to DokuWiki parser but without lookbehind (currently supported only by Chrome)
20
     * @see \Doku_Parser_Mode_smiley
21
     *
22
     * @returns {RegExp}
23
     */
24
    static getRegex() {
25
        const smileyGroups = Object.keys(this.getSmileys())
26
            .map(smiley => SmileyConf.escapeRegExp(smiley));
27
        return new RegExp(`(?:\\W|^)(${smileyGroups.join('|')})(?=\\W|$)`);
28
    }
29
}
30