Nodejs String Truncate trunc

Here you can find the source of trunc

Method Source Code

// http://stackoverflow.com/questions/1199352/smart-way-to-shorten-long-strings-with-javascript
String.prototype.trunc =// w  w  w  .j a v  a  2  s .c  o m
     function (n, useWordBoundary) {
         var toLong = this.length > n,
             s_ = toLong ? this.substr(0, n - 1) : this;
         s_ = useWordBoundary && toLong ? s_.substr(0, s_.lastIndexOf(' ')) : s_;
         return toLong ? s_ + '...' : s_;
     };

String.prototype.nl2br =
         function () {
             return this.replace(new RegExp('\r?\n', 'g'), '<br />');
         };

function sortByKey(array, key) {
    return array.sort(function (a, b) {
        var x = a[key]; var y = b[key];
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    });
}

function sortByKeyDesc(array, key) {
    return array.sort(function (a, b) {
        var x = a[key]; var y = b[key];
        return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    });
}

Related

  1. trunc
    String.prototype.trunc =
         function(n,useWordBoundary){
             var toLong = this.length>n,
                 s_ = toLong ? this.substr(0,n-1) : this;
             s_ = useWordBoundary && toLong ? s_.substr(0,s_.lastIndexOf(' ')) : s_;
             return  toLong ? s_ +'...' : s_;
          };
    
  2. trunc
    String.prototype.trunc =
        function( n, useWordBoundary ){
            var isTooLong = this.length > n,
                s_ = isTooLong ? this.substr(0,n-1) : this;
            s_ = (useWordBoundary && isTooLong) ? s_.substr(0,s_.lastIndexOf(' ')) : s_;
            return  isTooLong ? s_ + '&hellip;' : s_;
        };
    
  3. trunc
    String.prototype.trunc = 
          function(n){
              return this.substr(0,n-1)+(this.length>n?'&hellip;':'');
          };
    
  4. trunc()
    String.prototype.trunc = String.prototype.trunc ||
          function(n){
              return (this.length > n) ? this.substr(0,n-1)+'&hellip;' : this;
          };
    
  5. trunc()
    String.prototype.trunc = String.prototype.trunc ||
    function(n){
      return this.length>n ? this.substr(0,n-1)+'?' : this;
    };
    
  6. trunc()
    String.prototype.trunc = String.prototype.trunc ||
      function(n){
          return this.length>n ? this.substr(0,n-1)+'...' : this;
      };
    
  7. trunc(len,suffix)
    String.prototype.trunc = function(len,suffix) {
        return this.length > len ? this.slice(0, len) + (suffix||'&hellip;') : this;
    };