Code coverage report for string/truncate.js

Statements: 100% (13 / 13)      Branches: 100% (8 / 8)      Functions: 100% (3 / 3)      Lines: 100% (12 / 12)     

All files » string/ » truncate.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 211       1 15 15 15   15 15 4   11   11 11   1    
define(['../lang/toString', './trim'], function(toString, trim){
    /**
     * Limit number of chars.
     */
    function truncate(str, maxChars, append, onlyFullWords){
        str = toString(str);
        append = append || '...';
        maxChars = onlyFullWords? maxChars + 1 : maxChars;
 
        str = trim(str);
        if(str.length <= maxChars){
            return str;
        }
        str = str.substr(0, maxChars - append.length);
        //crop at last space or remove trailing whitespace
        str = onlyFullWords? str.substr(0, str.lastIndexOf(' ')) : trim(str);
        return str + append;
    }
    return truncate;
});