Javascript String trunc()

Description

Javascript String trunc()

String.prototype.trunc = String.prototype.trunc ||
function(n){/*from   w w  w. j  av a  2  s.c o  m*/
  return this.length>n ? this.substr(0,n-1)+'?' : this;
};

Javascript String trunc()


String.prototype.trunc = //from  w  w w .  j a v  a2s. c  o  m
function(n){
  return this.substr(0,n-1)+(this.length>n?'…':'');
};

Javascript String trunc()

String.prototype.trunc = String.prototype.trunc ||
      function(n){
          return this.length>n ? this.substr(0,n-1) : this.substr(0,this.length-1);
      };//from   ww w  . ja  va 2 s .  c o  m

Javascript String trunc()

String.prototype.trunc = String.prototype.trunc ||
    function(n){/*w w w  .ja  v  a 2s  .c  o  m*/
        return (this.length > n) ? this.substr(0, n-1) + '…' : this;
    };

Javascript String trunc()

String.prototype.trunc = String.prototype.trunc ||
  function(n, ellip){
    if (!ellip) ellip = '...';
    //from www.j a  v a 2  s.c  om
    return (this.length > n) ? this.substr(0,n-1) + ellip : this;
  };

Javascript String trunc()

String.prototype.trunc =/*from ww w .  ja v a 2  s .  c  o m*/
    function(n,useWordBoundary){
        var toLong = this.toString().length>n,
            s_ = toLong ? this.toString().substr(0,n-1) : this.toString();
        s_ = useWordBoundary && toLong ? s_.substr(0,s_.lastIndexOf(' ')) : s_;
        s_ = s_.replace(/\n{3,}/g, '\n\n');
        return  toLong ? s_ + '...' : s_;
    };

Javascript String trunc()

String.prototype.trunc = String.prototype.trunc ||
      function(n){
          return this.length>n ? this.substr(0,n-1)+'…' : this;
      };//w  ww  .  j a  v  a2 s .co  m

Javascript String trunc()

String.prototype.trunc =//from ww  w.  j a v  a2s  . c  om
  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_;
    return s_;
  };

Javascript String trunc()

function shorten(text, maxLength) {
    var ret = text;
    if (ret.length > maxLength) {
        ret = ret.substr(0,maxLength-3) + "...";
    }/*  w w  w. ja  va  2s .  c o  m*/
    return ret;
}

String.prototype.trunc = String.prototype.trunc ||
      function(n){
          return this.length>n ? this.substr(0,n-1) : this;
          // return this.length>n ? this.substr(0,n-1)+'…' : this;
      };

Javascript String trunc()

String.prototype.trunc =/*from w  w w .j a va 2s .c om*/
     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.rtrunc =
     function(n,useWordBoundary){
         var rev = this.split("").reverse().join("");
         var toLong = rev.length>n,
             s_ = toLong ? rev.substr(0,n-1) : rev;
         s_ = useWordBoundary && toLong ? s_.substr(0,s_.lastIndexOf(' ')) : s_;
         var output = toLong ? s_ + '...' : s_;
         return output.split("").reverse().join("");
      };

Javascript String trunc()

// http://stackoverflow.com/questions/1199352/smart-way-to-shorten-long-strings-with-javascript
String.prototype.trunc =//w  w w  .j  av a2 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));
    });
}



PreviousNext

Related