Nodejs String Truncate truncate(capLength)

Here you can find the source of truncate(capLength)

Method Source Code

String.prototype.truncate = function(capLength){

   var trunc = '..';
   var output = this;

   if(typeof capLength !== 'number'){
      console.log('Arg must be a number type. No truncation made.');
      return output;
   }//from  ww w .j a v a  2  s .c o  m
   
   if(output.length > capLength){
      

         if(output[capLength-1] === ' '){ //dont trunc after an empty space, looks ugly
                     capLength -= 1;
                     
                  }

                  if(output[capLength-2] === ' ' || output[capLength-3] === ' '){ // dont trunc on a single/ double letter. looks ugly
                     if(output[capLength-2] === ' '){
                        capLength -= 2;
                     }else{
                        capLength -= 3;
                     }
                     
                     }

         output = output.substring(0, capLength) + trunc;

   }
   else if(output.length === capLength){
      return output;
   }

   return output;
}

Related

  1. trunc()
    String.prototype.trunc = String.prototype.trunc ||
        function(n){
            return (this.length > n) ? this.substr(0, n-1) + '…' : this;
        };
    
  2. trunc()
    String.prototype.trunc = String.prototype.trunc ||
      function(n){
        return this.length>n ? this.substr(0,n-1)+'…' : this;
    };
    
  3. truncar(number,cadena)
    String.prototype.truncar = function(number,cadena) {
      if(number < 0)
        return this;
      if(cadena){
        return this.substr(0,number)+cadena;
      return this.substr(0,number);
    
  4. truncate( length, char )
    String.prototype.truncate = function ( length, char ) {
      var _length = length || 18;
      return this.length > _length ? this.substring( 0, _length ).replace( /\s+$/g, '' ) + ( char || '?' ) : this;
    
  5. truncate()
    String.prototype.truncate = function(){
        var re = this.match(/^.{0,50}[\S]*/);
        var l = re[0].length;
        var re = re[0].replace(/\s$/,'');
        if(l < this.length)
            re = re + "...";
        return re;
    
  6. truncate(len = 50)
    String.prototype.truncate = function(len = 50){
      var regx = new RegExp('^.{0,'+ len +'}[\S]*')
      var matches_array = this.match(regx);
      var str_length = matches_array[0].length;
      var replacement = matches_array[0].replace(/\s$/,'');
      return ((str_length > this.length) ? (replacement) : (replacement + "...") )
    
  7. truncate(length)
    String.prototype.truncate = function(length) {
      return this.substr(0, length - 1) + (this.length > length? '...' : '');
    
  8. truncate(length)
    String.prototype.truncate = function(length) {
      if (length == null) 
        length = 30;
      if (this.length > length) {
        content = this.substring(0, length);
        content += '?';
      } else {
        content = this  
      return content; 
    };
    var viewingMode;
    
  9. truncate(length)
    String.prototype.truncate = function(length) {
      length = typeof length !== 'undefined' ? length : 100;
      if (this.length > length) {
        var str = this.substring(0, length);
        str = str.substring(0, Math.min(str.length, str.lastIndexOf(" ")));
        str = str + " ...";
        return str;
      } else {
        return this;
    ...