tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

string-indexof-is-startswith.js (823B)


      1 // |str.indexOf(searchStr) == 0| is compiled as |str.startsWith(searchStr)|.
      2 // |str.indexOf(searchStr) != 0| is compiled as |!str.startsWith(searchStr)|.
      3 
      4 const strings = [
      5  "",
      6  "a", "b",
      7  "ab", "ba", "ac", "ca",
      8  "aba", "abb", "abc", "aca",
      9 ];
     10 
     11 function StringIndexOf(str, searchStr) {
     12  // Prevent Warp compilation when computing the expected result.
     13  with ({});
     14  return str.indexOf(searchStr);
     15 }
     16 
     17 for (let str of strings) {
     18  for (let searchStr of strings) {
     19    let startsWith = str.indexOf(searchStr) === 0;
     20 
     21    assertEq(startsWith, str.startsWith(searchStr));
     22    assertEq(startsWith, StringIndexOf(str, searchStr) === 0);
     23 
     24    let notStartsWith = str.indexOf(searchStr) !== 0;
     25 
     26    assertEq(notStartsWith, !str.startsWith(searchStr));
     27    assertEq(notStartsWith, StringIndexOf(str, searchStr) !== 0);
     28  }
     29 }