Nodejs String Truncate trunc(n,useWordBoundary)

Here you can find the source of trunc(n,useWordBoundary)

Method Source Code

/*//w  ww. j a va2  s  . co m
 * Copyright 2012-2013 inBloom, Inc. and its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

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.startsWith = function (str){
    return this.slice(0, str.length) == str;
};

Related

  1. trunc(n)
    String.prototype.trunc = function(n){
       var isTooLong = this.length > n;
       var string = isTooLong ? this.substr(0,n) : this;
       string = isTooLong ? string.substr(0, string.lastIndexOf(' ')) : string;
       return  isTooLong ? string + '...' : string;
    };
    
  2. trunc(n)
    String.prototype.trunc = function(n) {
      var longStr = this.length > n;
      var str     = longStr ? this.slice(0, n) : this;
      longStr ? str = str.slice(0, str.lastIndexOf(' ')) : str;
      return longStr ? str + '...' : str;
    
  3. trunc(n)
    String.prototype.trunc = String.prototype.trunc || function(n){
        return this.length>n ? this.substr(0, n-1) + '...' : this;
    };
    function inArray(needle, haystack) {
        var length = haystack.length;
        for(var i = 0; i < length; i++) {
            if(haystack[i] == needle) return true;
        return false;
    ...
    
  4. trunc(n)
    String.prototype.trunc = String.prototype.trunc || function(n){
        return this.length>n ? this.substr(0, n-1) + '...' : this;
    };
    function inArray(needle, haystack) {
        var length = haystack.length;
        for(var i = 0; i < length; i++) {
            if(haystack[i] == needle) return true;
        return false;
    ...
    
  5. trunc(n,useWordBoundary)
    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_;
    };
    
  6. trunc()
    String.prototype.trunc = String.prototype.trunc ||
      function(n){
        return this.length > n ? this.substr(0,n-1) + "\u2026" : this;
      };
    
  7. trunc()
    String.prototype.trunc = String.prototype.trunc ||
        function(n){
            return (this.length > n) ? this.substr(0, n-1) + '&hellip;' : this;
        };
    
  8. trunc()
    String.prototype.trunc = String.prototype.trunc ||
      function(n){
        return this.length>n ? this.substr(0,n-1)+'&hellip;' : this;
    };
    
  9. 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);