Shorten String - Node.js String

Node.js examples for String:String Value

Description

Shorten String

Demo Code

String.prototype.shorten = function(len, end, ellipses) {
    if (this.length <= len) {
        return this;
    }//from   w  ww . j  a va  2s  .c om
    end = end || false;
    ellipses = ellipses || "...";
    len -= ellipses.length;

    if (end) {
        return this.substr(0, len) + ellipses;
    } else {
        len /= 2;
        var begin = this.substr(0, len);
        var end = this.substr(this.length - len);
        return begin + ellipses + end;
    }
};

Related Tutorials