Nodejs String Truncate truncate(n)

Here you can find the source of truncate(n)

Method Source Code

String.prototype.truncate = function(n) {
  var tooLong = this.length > n,
      string = tooLong ? this.substr(0,n-1) : this;
  string = tooLong ? string.substr(0,string.lastIndexOf(' ')) : string;
  return  tooLong ? string + '...' : string;
};

Related

  1. 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;
    };
    
  2. 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_;
    };
    
  3. 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;
    ...
    
  4. truncate(maxlength)
    String.prototype.truncate = function (maxlength) {
        if (!this) {
            return '';
        if (!maxlength) {
            maxlength = 20;
        return (this.length > maxlength) ? this.slice(0, maxlength - 3) + '...' : this;
    };
    ...
    
  5. truncate(n)
    String.prototype.truncate = function(n){
          return this.substr(0,n-1)+(this.length>n?'...':'');
    };
    
  6. truncate(options)
    String.prototype.truncate = function (options) {
      var length = (options && options.length) || 50;
      var ending = (options && options.ending) || "...";
      var truncated = this.slice(0, length);
      if (truncated.length < this.length)
        truncated += ending;
      return truncated;
    };
    
  7. truncate(options)
    String.prototype.truncate = function (options) {
      var length = (options && options.length) || 70;
      var ending = (options && options.ending) || "...";
      var truncated = this.slice(0, length);
      if (truncated.length < this.length)
        truncated += ending;
      return truncated;
    };
    
  8. truncate(x)
    String.prototype.truncate = function(x){
      return this.length > x ? this.substr(0,x) : this ;
    
  9. truncate(x)
    String.prototype.truncate = function(x){
      if(this.length < x) return this.valueOf();
      else return this.substring(0, x - 3) + "...";
    };