/// <reference path="IStringTruncateOptions.ts" />
var econda;
(function (econda) {
    var util;
    (function (util) {
        /**
         * Helper functions for strings
         * @class econda.util.StringUtils
         * @static
         */
        var StringUtils = (function () {
            function StringUtils() {
            }
            /**
             * Remove whitespaces at the beginning and and of string
             * @static
             * @param {String} text
             */
            StringUtils.trim = function (text) {
                if (typeof String.prototype.trim !== 'function') {
                    this.trim = function (text) { return String(text).replace(/^\s+|\s+$/g, ''); };
                }
                else {
                    this.trim = function (text) { return String(text).trim(); };
                }
                return this.trim(text);
            };
            /**
             * Escape html special chars
             * @static
             * @param {String} text
             * @return {String} encoded string
             */
            StringUtils.html = function (text) {
                var me = this;
                return String(text).replace(/[&<>"'\/]/g, function (s) {
                    return me._entityMap[s];
                });
            };
            /**
             * Truncates string to given max length
             * @static
             * @param {String} text
             * @param {Object} options Options object or maxLength as number
             * @param [options.maxLength=100] Max length
             * @param [options.wordBounderies=true] Truncate on word boundaries
             * @param [options.ellipsis] String to add if text is longer than maxLength. Include space before.
             * @param [options.minLength] Ignore wordBounderies=true if string would be truncated to less characters.
             * @returns {String}
             */
            StringUtils.truncate = function (text, options) {
                if (options === void 0) { options = null; }
                options = options ? options : {};
                var maxLength = options.maxLength || 100;
                var wordBoundaries = options.wordBoundaries == true;
                var ellipsis = options.ellipsis || ' ...';
                var minLength = options.minLength || 1;
                var maxTextLength = maxLength - ellipsis.length;
                var ret;
                if (text.length > maxLength) {
                    ret = text.substr(0, maxTextLength);
                    if (wordBoundaries) {
                        var endIndex = ret.lastIndexOf(' ') - 1;
                        if (endIndex >= minLength) {
                            ret = ret.substr(0, endIndex);
                        }
                    }
                    ret = ret + ellipsis;
                }
                else {
                    ret = text;
                }
                return ret;
            };
            /**
             * Converts first character of string to uppercase.
             * @static
             * @param {String} text
             * @returns {String}
             */
            StringUtils.ucFirst = function (text) {
                return text.charAt(0).toUpperCase() + text.slice(1);
            };
            /**
            * Defines characters to encode if we want to output text in html
            * @static
            * @private
            */
            StringUtils._entityMap = {
                "&": "&amp;",
                "<": "&lt;",
                ">": "&gt;",
                '"': '&quot;',
                "'": '&#39;',
                "/": '&#x2F;'
            };
            return StringUtils;
        }());
        util.StringUtils = StringUtils; // end of class
    })(util = econda.util || (econda.util = {}));
})(econda || (econda = {})); // end of module