all files / TypeScript.NET/source/System/Text/ Utility.js

57.32% Statements 47/82
48% Branches 24/50
53.85% Functions 7/13
58.23% Lines 46/79
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119                                                                                    87× 86×       84×                                                        
/*!
 * @author electricessence / https://github.com/electricessence/
 * Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
 */
(function (factory) {
    Eif (typeof module === 'object' && typeof module.exports === 'object') {
        var v = factory(require, exports); Iif (v !== undefined) module.exports = v;
    }
    else if (typeof define === 'function' && define.amd) {
        define(["require", "exports", "../Types"], factory);
    }
})(function (require, exports) {
    "use strict";
    var Types_1 = require("../Types");
    exports.EMPTY = '';
    function getHashCode(source) {
        var hash = 0 | 0;
        if (source.length == 0)
            return hash;
        for (var i = 0, l = source.length; i < l; i++) {
            var ch = source.charCodeAt(i);
            hash = ((hash << 5) - hash) + ch;
            hash |= 0;
        }
        return hash;
    }
    exports.getHashCode = getHashCode;
    function repeat(source, count) {
        var result = exports.EMPTY;
        if (!isNaN(count)) {
            for (var i = 0; i < count; i++) {
                result += source;
            }
        }
        return result;
    }
    exports.repeat = repeat;
    function fromChars(chOrChars, count) {
        if (count === void 0) { count = 1; }
        if (Array.isArray(chOrChars)) {
            var result = exports.EMPTY;
            for (var _i = 0, chOrChars_1 = chOrChars; _i < chOrChars_1.length; _i++) {
                var char = chOrChars_1[_i];
                result += String.fromCharCode(char);
            }
            return result;
        }
        else {
            return repeat(String.fromCharCode(chOrChars), count);
        }
    }
    exports.fromChars = fromChars;
    function escapeRegExp(source) {
        return source.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
    }
    exports.escapeRegExp = escapeRegExp;
    function trim(source, chars, ignoreCase) {
        if (chars === exports.EMPTY)
            return source;
        if (chars) {
            var escaped = escapeRegExp(Array.isArray(chars) ? chars.join() : chars);
            return source.replace(new RegExp('^[' + escaped + ']+|[' + escaped + ']+$', 'g' + (ignoreCase
                ? 'i'
                : '')), exports.EMPTY);
        }
        return source.replace(/^\s+|\s+$/g, exports.EMPTY);
    }
    exports.trim = trim;
    function format(source) {
        var args = [];
        for (var _i = 1; _i < arguments.length; _i++) {
            args[_i - 1] = arguments[_i];
        }
        return supplant(source, args);
    }
    exports.format = format;
    function supplant(source, params) {
        var oIsArray = Array.isArray(params);
        return source.replace(/\{([^{}]*)\}/g, function (a, b) {
            var n = b;
            if (oIsArray) {
                var i = parseInt(b);
                if (!isNaN(i))
                    n = i;
            }
            var r = params[n];
            switch (typeof r) {
                case Types_1.Type.STRING:
                case Types_1.Type.NUMBER:
                case Types_1.Type.BOOLEAN:
                    return r;
                default:
                    return (r && Types_1.Type.hasMemberOfType(r, "toString", Types_1.Type.FUNCTION))
                        ? r.toString()
                        : a;
            }
        });
    }
    exports.supplant = supplant;
    function canMatch(source, match) {
        if (!Types_1.Type.isString(source) || !match)
            return false;
        if (source === match)
            return true;
        if (match.length < source.length)
            return null;
    }
    function startsWith(source, pattern) {
        var m = canMatch(source, pattern);
        return Types_1.Type.isBoolean(m) ? m : source.indexOf(pattern) == 0;
    }
    exports.startsWith = startsWith;
    function endsWith(source, pattern) {
        var m = canMatch(source, pattern);
        return Types_1.Type.isBoolean(m) ? m : source.lastIndexOf(pattern) == (source.length - pattern.length);
    }
    exports.endsWith = endsWith;
});
//# sourceMappingURL=Utility.js.map