File size: 1,522 Bytes
cbc76af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use strict';

module.exports = {

    /**
     * Creates an event.
     * @param {string} name - The event name
     * @param {object} [options] - Options to be passed to event
     */
    createEvent: function (name, options) {
        let event;
        options = options || {};
        options.bubbles = options.bubbles || true;
        options.cancelable = options.cancelable|| true;

        event = document.createEvent('CustomEvent');
        event.initCustomEvent(name, options.bubbles, options.cancelable, null);
        return event;
    },

    /**
     * Creates an HTML Element from an html string.
     * @param {string} html - String of html
     * @returns {HTMLElement} - Returns and html element node
     */
    createHtmlElement: function (html) {
        let tempParentEl,
            el;
        if (html) {
            html = this.trim(html);
            tempParentEl = document.createElement('div');
            tempParentEl.innerHTML = html;
            el = tempParentEl.childNodes[0];
            return tempParentEl.removeChild(el);
        }
    },

    /**
     * Zaps whitespace from both ends of a string.
     * @param {string} val - The string value to trim
     * @returns {string} Returns a trimmed string
     */
    trim: function (val) {
        if (!String.prototype.trim) {
            String.prototype.trim = function () {
                return val.replace(/^\s+|\s+$/g, '');
            };
        } else {
            val = val.trim();
        }
        return val;
    }

};