Nodejs String Truncate truncate(length, substitute)

Here you can find the source of truncate(length, substitute)

Method Source Code

/**//from  w w w .j  a va  2s.  co  m
 * Truncates the string to the given length and replaces superfluous characters
 * with the given substitute.
 *
 * @example  'This is a longer text'.truncate(7)
 *           'This is?'
 *
 * @param    [length] {Number} The number of characters after which the text
 *                             should be truncated. (default: 30)
 * @param    [substitute] {String} The substitute to append if the text exceeds
 *                                 the given limit. (default: '?')
 *
 * @return   {String} Returns the truncated string, followed by the substitute.
 */

String.prototype.truncate = String.prototype.truncate || function (length, substitute) {
  return this.length > (length || 30) ? this.substr(0, (length || 30)).replace(/\s$/, '') + (substitute || '?') : this.toString()
}

Related

  1. truncate(capLength)
    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;
      if(output.length > capLength){
          if(output[capLength-1] === ' '){ 
    ...
    
  2. 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 + "...") )
    
  3. truncate(length)
    String.prototype.truncate = function(length) {
      return this.substr(0, length - 1) + (this.length > length? '...' : '');
    
  4. 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;
    
  5. 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;
    ...
    
  6. truncate(length, truncation)
    String.prototype.truncate = function(length, truncation) {
      length = length || 30;
      truncation = truncation === undefined ? '...' : truncation;
      return this.length > length ?
        this.slice(0, length - truncation.length) + truncation : String(this);
    };
    
  7. truncate(length, truncation)
    String.prototype.truncate = function(length, truncation) {
        length = length || 30;
        truncation = (typeof truncation == 'undefined') ? '…' : truncation;
        return this.length > length
            ? this.slice(0, length) + truncation
            : this;
    };
    
  8. truncate(length, useWordBoundary)
    String.prototype.truncate = function(length, useWordBoundary) {
        var toLong = this.length > length,
        s_ = toLong ? this.substr(0, length - 1) : this;
        s_ = useWordBoundary && toLong ? s_.substr(0, s_.lastIndexOf(' ')) : s_;
        return toLong ? s_ + '…' : s_;
    };
    
  9. truncate(max_len, appender)
    'use strict';
    String.prototype.truncate = function(max_len, appender) {
      max_len  = max_len || 80;
      appender = appender || '...';
      if (appender.length >= max_len) {
        throw Error('appender length is greater or equal to max_len');
      if (this.length <= max_len) {
        return this;
    ...