Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7822: string-start-end.patch

File string-start-end.patch, 1.2 kB (added by altblue, 1 year ago)

tests + fix for endsWith

  • test/unit/string.html

    old new  
    342342      assert('hello world'.startsWith('hello')); 
    343343      assert(!'hello world'.startsWith('bye')); 
    344344      assert(!''.startsWith('bye')); 
     345      assert(!'hell'.startsWith('hello')); 
    345346    }}, 
    346347     
    347348    testEndsWith: function() {with(this) { 
     
    350351      assert(!'hello world'.endsWith('planet')); 
    351352      assert(!''.endsWith('planet')); 
    352353      assert('hello world world'.endsWith(' world')); 
     354      assert(!'z'.endsWith('az')); 
    353355    }}, 
    354356     
    355357    testBlank: function() { with(this) { 
  • src/string.js

    old new  
    176176  }, 
    177177 
    178178  startsWith: function(pattern) { 
    179     return this.indexOf(pattern) == 0; 
     179    return this.indexOf(pattern) === 0; 
    180180  }, 
    181181 
    182182  endsWith: function(pattern) { 
    183     return this.lastIndexOf(pattern) == (this.length - pattern.length); 
     183    var d = this.length - pattern.length; 
     184    return d >= 0 && this.lastIndexOf(pattern) === d; 
    184185  }, 
    185186   
    186187  empty: function() {