Issues (72)

src/utils/string.js (1 issue)

1
/**
2
 * Class: jaxon.utils.string
3
 *
4
 * global: jaxon
5
 */
6
7
(function(self) {
8
    /**
9
     * Replace all occurances of the single quote character with a double quote character.
10
     *
11
     * @param {string=} haystack The source string to be scanned
12
     *
13
     * @returns {string|false} A new string with the modifications applied. False on error.
14
     */
15
    self.doubleQuotes = haystack => haystack === undefined ?
16
        false : haystack.replace(new RegExp("'", 'g'), '"');
17
18
    /**
19
     * Replace all occurances of the double quote character with a single quote character.
20
     *
21
     * @param {string=} haystack The source string to be scanned
22
     *
23
     * @returns {string|false} A new string with the modification applied
24
     */
25
    self.singleQuotes = haystack => haystack === undefined ?
26
        false : haystack.replace(new RegExp('"', 'g'), "'");
27
28
    /**
29
     * Detect, and if found, remove the prefix 'on' from the specified string.
30
     * This is used while working with event handlers.
31
     *
32
     * @param {string} sEventName The string to be modified
33
     *
34
     * @returns {string} The modified string
35
     */
36
    self.stripOnPrefix = (sEventName) => {
37
        sEventName = sEventName.toLowerCase();
38
        return sEventName.indexOf('on') === 0 ? sEventName.replace(/on/, '') : sEventName;
39
    };
40
41
    /**
42
     * Detect, and add if not found, the prefix 'on' from the specified string.
43
     * This is used while working with event handlers.
44
     *
45
     * @param {string} sEventName The string to be modified
46
     *
47
     * @returns {string} The modified string
48
     */
49
    self.addOnPrefix = (sEventName) => {
50
        sEventName = sEventName.toLowerCase();
51
        return sEventName.indexOf('on') !== 0 ? 'on' + sEventName : sEventName;
52
    };
53
54
    /**
55
     * String functions for Jaxon
56
     * See http://javascript.crockford.com/remedial.html for more explanation
57
     */
58
    if (!String.prototype.supplant) {
59
        /**
60
         * Substitute variables in the string
61
         *
62
         * @param {object} values The substitution values
63
         *
64
         * @returns {string}
65
         */
66
        String.prototype.supplant = function(values) {
0 ignored issues
show
Compatibility Best Practice introduced by
You are extending the built-in type String. This may have unintended consequences on other objects using this built-in type. Consider subclassing instead.
Loading history...
67
            return this.replace(
68
                /\{([^{}]*)\}/g,
69
                (a, b) => {
70
                    const r = values[b];
71
                    const t = typeof r;
72
                    return t === 'string' || t === 'number' ? r : a;
73
                }
74
            );
75
        };
76
    }
77
})(jaxon.utils.string);
78