truncate: function truncate()

in src/dep/truncate.js [91:146]


    truncate: function truncate(text, isLastNode) {
        if (!this.keepWhitespaces) {
            text = text.replace(/\s+/g, ' ');
        }
        var byWords = this.options.byWords;
        var match = text.match(astralRange);
        var astralSafeCharacterArray = match === null ? [] : match;
        var strLen = match === null ? 0 : astralSafeCharacterArray.length;
        var idx = 0;
        var count = 0;
        var prevIsBlank = byWords;
        var curIsBlank = false;
        while (idx < strLen) {
            curIsBlank = this.isBlank(astralSafeCharacterArray[idx++]);
            // keep same then continue
            if (byWords && prevIsBlank === curIsBlank)
                { continue; }
            if (count === this.limit) {
                // reserve trailing whitespace, only when prev is blank too
                if (prevIsBlank && curIsBlank) {
                    prevIsBlank = curIsBlank;
                    continue;
                }
                // fix idx because current char belong to next words which exceed the limit
                --idx;
                break;
            }
            if (byWords) {
                curIsBlank || ++count;
            }
            else {
                (curIsBlank && prevIsBlank) || ++count;
            }
            prevIsBlank = curIsBlank;
        }
        this.limit -= count;
        if (this.limit) {
            return text;
        }
        else {
            var str;
            if (byWords) {
                str = text.substr(0, idx);
            }
            else {
                str = this.substr(astralSafeCharacterArray, idx);
            }
            if (str === text) {
                // if is lat node, no need of ellipsis, or add it
                return isLastNode ? text : text + this.ellipsis;
            }
            else {
                return str + this.ellipsis;
            }
        }
    },