File size: 2,544 Bytes
e7c953d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
'use strict';

function noop() {
    //No operation
}

exports.noop = noop;

function bind(func, context) {
    return function boundFunc() {
        return func.apply(context, arguments);
    };
}

exports.bind = bind;

function debounce(func, wait, immediate) {
    var timeout;

    return function debouncedFunc() {
        var callNow = immediate && !timeout,
            context = this,
            args = arguments;

        clearTimeout(timeout);

        timeout = setTimeout(function() {
            timeout = null;

            if (!immediate) func.apply(context, args);
        }, wait);

        if (callNow) func.apply(context, args);
    };
}

exports.debounce = debounce;

function encodeHTMLEntities(str) {
    if (typeof str !== 'string') return str;

    str = str.replace(/&/g, '&')
             .replace(/'/g, ''')
             .replace(/"/g, '"')
             .replace(/</g, '&lt;')
             .replace(/>/g, '&gt;');

    return str;
}

exports.encodeHTMLEntities = encodeHTMLEntities;

function decodeHTMLEntities(str) {
    if (typeof str !== 'string') return str;

    str = str.replace(/&#39;|&apos;/g, '\'')
             .replace(/&#34;|&quot;/g, '"')
             .replace(/&amp;/g, '&')
             .replace(/&lt;/g, '<')
             .replace(/&gt;/g, '>');

    return str;
}

exports.decodeHTMLEntities = decodeHTMLEntities;

function clone(obj, options) {
    options = options || {};

    if (options.deep === undefined) options.deep = false;
    if (options.exclude === undefined) options.exclude = [];

    function copy(obj, level) {
        if (obj == null || typeof obj !== 'object') return obj;

        var clone, i;

        if (obj instanceof Array) {
            clone = [];

            for (i = 0; i < obj.length; i++) {
                if (!obj.hasOwnProperty(i)) continue;
                if (options.deep && level < 4) clone.push(copy(obj[i], level + 1));
                else clone.push(obj[i]);
            }
        } else {
            clone = {};

            for (i in obj) {
                if (!obj.hasOwnProperty(i)) continue;
                if (options.exclude[level] !== undefined && options.exclude[level].indexOf(i) !== -1) continue;
                if (options.deep && level < 4) clone[i] = copy(obj[i], level + 1);
                else clone[i] = obj[i];
            }
        }

        return clone;
    }

    return copy(obj, 0);
}

exports.clone = clone;