Nodejs String Trim trim()

Here you can find the source of trim()

Method Source Code

Array.isArray = Array.isArray || function (obj) {
   return toString.call(obj) === '[object Array]';
};

String.prototype.trim = String.prototype.trim || function () {
   var str = this.replace(/^\s\s*/, ''),
      i = str.length;/*from w w  w.java  2  s.co m*/

   for (let rgxp = /\s/; rgxp.test(str.charAt(--i));) {}
   return str.substring(0, i + 1);
};

Object.create = Object.create || function (proto) {
   /** @constructor */
   var F = function () {};
   F.prototype = proto;
   return new F();
};

if (!isFunction(Object.getPrototypeOf)) {
   if (typeof 'test'.__proto__ === 'object') {
      Object.getPrototypeOf = (object) => object.__proto__;

   } else {
      Object.getPrototypeOf = (object) => object.constructor.prototype;
   }
}

var spliceTest = {
   0: 1,
   1: 2,
   length: 2
};

splice.call(spliceTest, 1, 1);
if (spliceTest[1] !== undefined || spliceTest.length !== 1) {
   splice = function (from, pos) {
      if (isArray(this)) {
         return this.splice(from, pos);
      }

      var res = [];
      for (; from <= pos; from++) {
         res.push(this[from]);
         delete this[from];
         this.length--;
      }

      return res;
   };
}

Related

  1. trim()
    String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ''); }
    function isProbablyUrl(string) {
      var substr = string.substring(0,4).toLowerCase();
      if (substr == 'ftp:' || substr == 'www.') return true;
      var substr = string.substring(0,5).toLowerCase();
      if (substr == 'http:') return true;
      var substr = string.substring(0,6).toLowerCase();
      if (substr == 'https:') return true;
      var substr = string.substring(0,7).toLowerCase();
    ...
    
  2. trim()
    String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g, "");
    
  3. trim()
    String.prototype.trim=function()
      return this.replace(/^\s+|\s+$/g, '');
    String.prototype.ltrim=function()
      return this.replace(/^\s+/,'');
    String.prototype.rtrim=function()
    ...
    
  4. trim()
    String.prototype.trim = function() {
      this.replace(/(^\s*)|(\s*$)/gi,'');
      this.replace(/[ ]{2,}/gi,' ');
      this.replace(/\n /,'\n');
      return this;
    };
    
  5. trim()
    String.prototype.trim = function(){
      var start,end;
      start=0;
      end=this.length-1;
      while(start<=end && this.charAt(start)==' '){
        start++;
      while(start<=end && this.charAt(end)==" "){
        end--;
    ...
    
  6. trim()
    String.prototype.trim = function() {
      return this.replace(/^\s+|\s+$/g,"");
    
  7. trim()
    String.prototype.trim = function() {
      return this.replace(/\s/g, "");
    
  8. trim()
    String.prototype.trim= function()
        var str= this.replace(/^\s+/, '');
      for (var i = str.length - 1; i > 0; --i)
        if (/\S/.test(str.charAt(i)))
          str = str.substring(0, i + 1);
          break;
      return str;
    
  9. trim()
    String.prototype.trim = function () {
        var start, end;
        start = 0;
        end = this.length - 1;
        while (start <= end && str.charAt(start) == ' ') {
            start++;
        while (start <= end && str.charAt(end) == ' ') {
            end--;
    ...