all files / montage/core/shim/ string.js

85.71% Statements 6/7
50% Branches 4/8
50% Functions 1/2
85.71% Lines 6/7
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                                                                    12×     12×              
/**
 * Defines standardized shims for the intrinsic String object.
 * @see {external:String}
 * @module montage/core/shim/string
 */
 
/**
 * @external String
 */
 
/**
 * Returns whether this string begins with a given substring.
 *
 * @function external:String#startsWith
 * @param {string} substring a potential substring of this string
 * @returns {boolean} whether this string starts with the given substring
 */
Eif (!String.prototype.startsWith) {
    Object.defineProperty(String.prototype, 'startsWith', {
        value: function (start) {
            return this.length >= start.length &&
                this.slice(0, start.length) === start;
        },
        writable: true,
        configurable: true
    });
}
 
/**
 * Returns whether this string ends with a given substring.
 *
 * @function external:String#endsWith
 * @param {string} substring a potential substring of this string
 * @returns {boolean} whether this string ends with the given substring
 */
Eif (!String.prototype.endsWith) {
    Object.defineProperty(String.prototype, 'endsWith', {
        value: function (end) {
            var selfLength = this.length,
                endLength = end.length;
 
            return selfLength >= endLength && this.indexOf(end, selfLength - endLength) !== -1;
        },
        writable: true,
        configurable: true
    });
}