Completed
Push — master ( ed7f79...6ebeeb )
by Donata
02:21
created

utils.js ➔ ... ➔ ???   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
dl 0
loc 4
rs 10
c 2
b 2
f 0
cc 1
nc 1
nop 1
1
export const utils = {
2
3
    /**
4
     * did element has class name
5
     *
6
     * @param element
7
     * @param className
8
     * @returns {boolean}
9
     */
10
    hasClass: (element, className) => {
11
        return element.classList ? element.classList.contains(className) : new RegExp('\\b' + className + '\\b').test(element.className);
12
    },
13
14
    /**
15
     * add class to the element
16
     *
17
     * @param element
18
     * @param className
19
     */
20
    addClass: (element, className) => {
21
        if (element.classList) {
22
            element.classList.add(className);
23
        } else if (!this.hasClass(element, className)) {
24
            element.className += '' + className;
25
        }
26
    },
27
28
    /**
29
     * remove class from the element
30
     *
31
     * @param element
32
     * @param className
33
     */
34
    removeClass: (element, className) => {
35
        if (element.classList) {
36
            element.classList.remove(className);
37
        } else {
38
            element.className = element.className.replace(new RegExp('\\b' + className + '\\b', 'g'), '');
39
        }
40
    },
41
42
    /**
43
     * function will generate random id string only
44
     *
45
     * @returns {string}
46
     */
47
    randomId: () => {
48
        return (0 | Math.random() * 9e6).toString(36);
49
    },
50
51
    /**
52
     * escape html entities
53
     *
54
     * @param text
55
     * @returns {*}
56
     */
57
    escapeEntity: (text) => {
58
        return text.replace(/&/g, '&')
59
            .replace(/>/g, '>')
60
            .replace(/</g, '&lt;')
61
            .replace(/"/g, '&quot;')
62
            .replace(/'/g, '&#39;')
63
            .replace(/`/g, '&#96;');
64
    },
65
66
    /**
67
     * convert escaped html entities to symbols
68
     *
69
     * @param text
70
     * @returns {*}
71
     */
72
    convertEntity: (text) => {
73
        const span = document.createElement('span');
74
75
        return text
76
            .replace(/&[#A-Za-z0-9]+;/gi, (entity) => {
77
                span.innerHTML = entity;
78
                return span.innerText;
79
            });
80
    },
81
82
    /**
83
     * function rendering given template within options included. Example
84
     * how to right use this function:
85
     *
86
     * const template = (picture, pictures) => utils.render`
87
     *  <img src="${picture}" />
88
     *
89
     *  ${pictures.map(item => utils.render`
90
     *      <img src="${item.source}" alt="${item.alt}" />
91
     *  `)}
92
     * `
93
     *
94
     * template(options.picture, options.pictures); // return string
95
     *
96
     * @param sections
97
     * @param substitutes
98
     * @returns {string}
99
     */
100
    render: (sections, ...substitutes) => {
101
        let raw = sections.raw;
102
        let result = '';
103
104
        substitutes.forEach((substitute, i) => {
105
            let lit = raw[i];
106
107
            if (Array.isArray(substitute)) {
108
                substitute = substitute.join('');
109
            }
110
111
            if (lit.endsWith('$')) {
112
                subst = this.escapeEntity(substitute);
0 ignored issues
show
Bug introduced by
The variable subst seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.subst.
Loading history...
113
                lit = lit.slice(0, -1);
114
            }
115
            result += lit;
116
            result += substitute;
117
        });
118
119
        result += raw[raw.length - 1];
120
121
        return result;
122
    }
123
};