Nodejs String Truncate trunc(len,suffix)

Here you can find the source of trunc(len,suffix)

Method Source Code

/**/*from  w  ww  .j  a v  a2  s .c o m*/
 * shorten a string, adding a suffix in place of excessive characters
 * default suffix is an html encoded ellipsis '…'
 * 
 * @function module:String.prototype.trunc
 * @param {number}     len     The lenth of the string to keep (not counting suffix)
 * @param {string}  suffix  The suffix to append (e.g. '...<a>read more</a>')
 * @return {string}
 * @example
 *  'this is a description that is too detailed'.trunc(10) === 'this is a &hellip;'
 */
String.prototype.trunc = String.prototype.trunc || function(len,suffix) {
    return this.length > len ? this.slice(0, len) + (suffix||'&hellip;') : this;
};

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_;
         };
    String.prototype.nl2br =
             function () {
    ...
    
  2. trunc()
    String.prototype.trunc = String.prototype.trunc ||
          function(n){
              return (this.length > n) ? this.substr(0,n-1)+'&hellip;' : this;
          };
    
  3. trunc()
    String.prototype.trunc = String.prototype.trunc ||
    function(n){
      return this.length>n ? this.substr(0,n-1)+'?' : this;
    };
    
  4. trunc()
    String.prototype.trunc = String.prototype.trunc ||
      function(n){
          return this.length>n ? this.substr(0,n-1)+'...' : this;
      };
    
  5. trunc(len,suffix)
    String.prototype.trunc = function(len,suffix) {
        return this.length > len ? this.slice(0, len) + (suffix||'&hellip;') : this;
    };
    
  6. trunc(length, options)
    String.prototype.trunc = function(length, options) {
        var defaults = Ext.Object.merge({omission: "..."}, options || {});
        var stop = length - defaults.omission.length;
        return (this.length > length ? this.substring(0, stop) + defaults.omission : this).toString();
    };
    
  7. trunc(n)
    String.prototype.trunc = function(n) {
      'use strict';
      return this.length > n ? this.substr(0, n-1) + '...' : this;
    };
    
  8. trunc(n)
    String.prototype.trunc = String.prototype.trunc ||
          function(n){
              return (this.length > n) ? this.substr(0,n-1)+'&hellip;' : this;
          };
    
  9. trunc(n)
    String.prototype.trunc = function(n) {
      return this.substr(0,n-1)+(this.length>n?'...':'');
    };