tor-browser

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

string-endswith.js (12469B)


      1 /*
      2 * Copyright (c) 2013 Mathias Bynens. All rights reserved.
      3 *
      4 * Permission is hereby granted, free of charge, to any person obtaining a
      5 * copy of this software and associated documentation files (the "Software"),
      6 * to deal in the Software without restriction, including without limitation
      7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8 * and/or sell copies of the Software, and to permit persons to whom the
      9 * Software is furnished to do so, subject to the following conditions:
     10 *
     11 * The above copyright notice and this permission notice shall be included in
     12 * all copies or substantial portions of the Software.
     13 *
     14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     20 * DEALINGS IN THE SOFTWARE.
     21 */
     22 
     23 function assertThrows(fun, errorType) {
     24  try {
     25    fun();
     26    assertEq(true, false, "Expected error, but none was thrown");
     27  } catch (e) {
     28    assertEq(e instanceof errorType, true, "Wrong error type thrown");
     29  }
     30 }
     31 assertEq(String.prototype.endsWith.length, 1);
     32 assertEq(String.prototype.propertyIsEnumerable('endsWith'), false);
     33 
     34 assertEq('undefined'.endsWith(), true);
     35 assertEq('undefined'.endsWith(undefined), true);
     36 assertEq('undefined'.endsWith(null), false);
     37 assertEq('null'.endsWith(), false);
     38 assertEq('null'.endsWith(undefined), false);
     39 assertEq('null'.endsWith(null), true);
     40 
     41 assertEq('abc'.endsWith(), false);
     42 assertEq('abc'.endsWith(''), true);
     43 assertEq('abc'.endsWith('\0'), false);
     44 assertEq('abc'.endsWith('c'), true);
     45 assertEq('abc'.endsWith('b'), false);
     46 assertEq('abc'.endsWith('a'), false);
     47 assertEq('abc'.endsWith('ab'), false);
     48 assertEq('abc'.endsWith('bc'), true);
     49 assertEq('abc'.endsWith('abc'), true);
     50 assertEq('abc'.endsWith('bcd'), false);
     51 assertEq('abc'.endsWith('abcd'), false);
     52 assertEq('abc'.endsWith('bcde'), false);
     53 
     54 assertEq('abc'.endsWith('', NaN), true);
     55 assertEq('abc'.endsWith('\0', NaN), false);
     56 assertEq('abc'.endsWith('c', NaN), false);
     57 assertEq('abc'.endsWith('b', NaN), false);
     58 assertEq('abc'.endsWith('a', NaN), false);
     59 assertEq('abc'.endsWith('ab', NaN), false);
     60 assertEq('abc'.endsWith('bc', NaN), false);
     61 assertEq('abc'.endsWith('abc', NaN), false);
     62 assertEq('abc'.endsWith('bcd', NaN), false);
     63 assertEq('abc'.endsWith('abcd', NaN), false);
     64 assertEq('abc'.endsWith('bcde', NaN), false);
     65 
     66 assertEq('abc'.endsWith('', false), true);
     67 assertEq('abc'.endsWith('\0', false), false);
     68 assertEq('abc'.endsWith('c', false), false);
     69 assertEq('abc'.endsWith('b', false), false);
     70 assertEq('abc'.endsWith('a', false), false);
     71 assertEq('abc'.endsWith('ab', false), false);
     72 assertEq('abc'.endsWith('bc', false), false);
     73 assertEq('abc'.endsWith('abc', false), false);
     74 assertEq('abc'.endsWith('bcd', false), false);
     75 assertEq('abc'.endsWith('abcd', false), false);
     76 assertEq('abc'.endsWith('bcde', false), false);
     77 
     78 assertEq('abc'.endsWith('', undefined), true);
     79 assertEq('abc'.endsWith('\0', undefined), false);
     80 assertEq('abc'.endsWith('c', undefined), true);
     81 assertEq('abc'.endsWith('b', undefined), false);
     82 assertEq('abc'.endsWith('a', undefined), false);
     83 assertEq('abc'.endsWith('ab', undefined), false);
     84 assertEq('abc'.endsWith('bc', undefined), true);
     85 assertEq('abc'.endsWith('abc', undefined), true);
     86 assertEq('abc'.endsWith('bcd', undefined), false);
     87 assertEq('abc'.endsWith('abcd', undefined), false);
     88 assertEq('abc'.endsWith('bcde', undefined), false);
     89 
     90 assertEq('abc'.endsWith('', null), true);
     91 assertEq('abc'.endsWith('\0', null), false);
     92 assertEq('abc'.endsWith('c', null), false);
     93 assertEq('abc'.endsWith('b', null), false);
     94 assertEq('abc'.endsWith('a', null), false);
     95 assertEq('abc'.endsWith('ab', null), false);
     96 assertEq('abc'.endsWith('bc', null), false);
     97 assertEq('abc'.endsWith('abc', null), false);
     98 assertEq('abc'.endsWith('bcd', null), false);
     99 assertEq('abc'.endsWith('abcd', null), false);
    100 assertEq('abc'.endsWith('bcde', null), false);
    101 
    102 assertEq('abc'.endsWith('', -Infinity), true);
    103 assertEq('abc'.endsWith('\0', -Infinity), false);
    104 assertEq('abc'.endsWith('c', -Infinity), false);
    105 assertEq('abc'.endsWith('b', -Infinity), false);
    106 assertEq('abc'.endsWith('a', -Infinity), false);
    107 assertEq('abc'.endsWith('ab', -Infinity), false);
    108 assertEq('abc'.endsWith('bc', -Infinity), false);
    109 assertEq('abc'.endsWith('abc', -Infinity), false);
    110 assertEq('abc'.endsWith('bcd', -Infinity), false);
    111 assertEq('abc'.endsWith('abcd', -Infinity), false);
    112 assertEq('abc'.endsWith('bcde', -Infinity), false);
    113 
    114 assertEq('abc'.endsWith('', -1), true);
    115 assertEq('abc'.endsWith('\0', -1), false);
    116 assertEq('abc'.endsWith('c', -1), false);
    117 assertEq('abc'.endsWith('b', -1), false);
    118 assertEq('abc'.endsWith('a', -1), false);
    119 assertEq('abc'.endsWith('ab', -1), false);
    120 assertEq('abc'.endsWith('bc', -1), false);
    121 assertEq('abc'.endsWith('abc', -1), false);
    122 assertEq('abc'.endsWith('bcd', -1), false);
    123 assertEq('abc'.endsWith('abcd', -1), false);
    124 assertEq('abc'.endsWith('bcde', -1), false);
    125 
    126 assertEq('abc'.endsWith('', -0), true);
    127 assertEq('abc'.endsWith('\0', -0), false);
    128 assertEq('abc'.endsWith('c', -0), false);
    129 assertEq('abc'.endsWith('b', -0), false);
    130 assertEq('abc'.endsWith('a', -0), false);
    131 assertEq('abc'.endsWith('ab', -0), false);
    132 assertEq('abc'.endsWith('bc', -0), false);
    133 assertEq('abc'.endsWith('abc', -0), false);
    134 assertEq('abc'.endsWith('bcd', -0), false);
    135 assertEq('abc'.endsWith('abcd', -0), false);
    136 assertEq('abc'.endsWith('bcde', -0), false);
    137 
    138 assertEq('abc'.endsWith('', +0), true);
    139 assertEq('abc'.endsWith('\0', +0), false);
    140 assertEq('abc'.endsWith('c', +0), false);
    141 assertEq('abc'.endsWith('b', +0), false);
    142 assertEq('abc'.endsWith('a', +0), false);
    143 assertEq('abc'.endsWith('ab', +0), false);
    144 assertEq('abc'.endsWith('bc', +0), false);
    145 assertEq('abc'.endsWith('abc', +0), false);
    146 assertEq('abc'.endsWith('bcd', +0), false);
    147 assertEq('abc'.endsWith('abcd', +0), false);
    148 assertEq('abc'.endsWith('bcde', +0), false);
    149 
    150 assertEq('abc'.endsWith('', 1), true);
    151 assertEq('abc'.endsWith('\0', 1), false);
    152 assertEq('abc'.endsWith('c', 1), false);
    153 assertEq('abc'.endsWith('b', 1), false);
    154 assertEq('abc'.endsWith('ab', 1), false);
    155 assertEq('abc'.endsWith('bc', 1), false);
    156 assertEq('abc'.endsWith('abc', 1), false);
    157 assertEq('abc'.endsWith('bcd', 1), false);
    158 assertEq('abc'.endsWith('abcd', 1), false);
    159 assertEq('abc'.endsWith('bcde', 1), false);
    160 
    161 assertEq('abc'.endsWith('', 2), true);
    162 assertEq('abc'.endsWith('\0', 2), false);
    163 assertEq('abc'.endsWith('c', 2), false);
    164 assertEq('abc'.endsWith('b', 2), true);
    165 assertEq('abc'.endsWith('ab', 2), true);
    166 assertEq('abc'.endsWith('bc', 2), false);
    167 assertEq('abc'.endsWith('abc', 2), false);
    168 assertEq('abc'.endsWith('bcd', 2), false);
    169 assertEq('abc'.endsWith('abcd', 2), false);
    170 assertEq('abc'.endsWith('bcde', 2), false);
    171 
    172 assertEq('abc'.endsWith('', +Infinity), true);
    173 assertEq('abc'.endsWith('\0', +Infinity), false);
    174 assertEq('abc'.endsWith('c', +Infinity), true);
    175 assertEq('abc'.endsWith('b', +Infinity), false);
    176 assertEq('abc'.endsWith('a', +Infinity), false);
    177 assertEq('abc'.endsWith('ab', +Infinity), false);
    178 assertEq('abc'.endsWith('bc', +Infinity), true);
    179 assertEq('abc'.endsWith('abc', +Infinity), true);
    180 assertEq('abc'.endsWith('bcd', +Infinity), false);
    181 assertEq('abc'.endsWith('abcd', +Infinity), false);
    182 assertEq('abc'.endsWith('bcde', +Infinity), false);
    183 
    184 assertEq('abc'.endsWith('', true), true);
    185 assertEq('abc'.endsWith('\0', true), false);
    186 assertEq('abc'.endsWith('c', true), false);
    187 assertEq('abc'.endsWith('b', true), false);
    188 assertEq('abc'.endsWith('ab', true), false);
    189 assertEq('abc'.endsWith('bc', true), false);
    190 assertEq('abc'.endsWith('abc', true), false);
    191 assertEq('abc'.endsWith('bcd', true), false);
    192 assertEq('abc'.endsWith('abcd', true), false);
    193 assertEq('abc'.endsWith('bcde', true), false);
    194 
    195 assertEq('abc'.endsWith('', 'x'), true);
    196 assertEq('abc'.endsWith('\0', 'x'), false);
    197 assertEq('abc'.endsWith('c', 'x'), false);
    198 assertEq('abc'.endsWith('b', 'x'), false);
    199 assertEq('abc'.endsWith('a', 'x'), false);
    200 assertEq('abc'.endsWith('ab', 'x'), false);
    201 assertEq('abc'.endsWith('bc', 'x'), false);
    202 assertEq('abc'.endsWith('abc', 'x'), false);
    203 assertEq('abc'.endsWith('bcd', 'x'), false);
    204 assertEq('abc'.endsWith('abcd', 'x'), false);
    205 assertEq('abc'.endsWith('bcde', 'x'), false);
    206 
    207 assertEq('[a-z]+(bar)?'.endsWith('(bar)?'), true);
    208 assertThrows(function() { '[a-z]+(bar)?'.endsWith(/(bar)?/); }, TypeError);
    209 assertEq('[a-z]+(bar)?'.endsWith('[a-z]+', 6), true);
    210 assertThrows(function() { '[a-z]+(bar)?'.endsWith(/(bar)?/); }, TypeError);
    211 assertThrows(function() { '[a-z]+/(bar)?/'.endsWith(/(bar)?/); }, TypeError);
    212 var global = newGlobal();
    213 global.eval('this.re = /(bar)?/');
    214 assertThrows(function() { '[a-z]+/(bar)?/'.endsWith(global.re); }, TypeError);
    215 
    216 // http://mathiasbynens.be/notes/javascript-unicode#poo-test
    217 var string = 'I\xF1t\xEBrn\xE2ti\xF4n\xE0liz\xE6ti\xF8n\u2603\uD83D\uDCA9';
    218 assertEq(string.endsWith(''), true);
    219 assertEq(string.endsWith('\xF1t\xEBr'), false);
    220 assertEq(string.endsWith('\xF1t\xEBr', 5), true);
    221 assertEq(string.endsWith('\xE0liz\xE6'), false);
    222 assertEq(string.endsWith('\xE0liz\xE6', 16), true);
    223 assertEq(string.endsWith('\xF8n\u2603\uD83D\uDCA9'), true);
    224 assertEq(string.endsWith('\xF8n\u2603\uD83D\uDCA9', 23), true);
    225 assertEq(string.endsWith('\u2603'), false);
    226 assertEq(string.endsWith('\u2603', 21), true);
    227 assertEq(string.endsWith('\uD83D\uDCA9'), true);
    228 assertEq(string.endsWith('\uD83D\uDCA9', 23), true);
    229 
    230 assertThrows(function() { String.prototype.endsWith.call(undefined); }, TypeError);
    231 assertThrows(function() { String.prototype.endsWith.call(undefined, 'b'); }, TypeError);
    232 assertThrows(function() { String.prototype.endsWith.call(undefined, 'b', 4); }, TypeError);
    233 assertThrows(function() { String.prototype.endsWith.call(null); }, TypeError);
    234 assertThrows(function() { String.prototype.endsWith.call(null, 'b'); }, TypeError);
    235 assertThrows(function() { String.prototype.endsWith.call(null, 'b', 4); }, TypeError);
    236 assertEq(String.prototype.endsWith.call(42, '2'), true);
    237 assertEq(String.prototype.endsWith.call(42, '4'), false);
    238 assertEq(String.prototype.endsWith.call(42, 'b', 4), false);
    239 assertEq(String.prototype.endsWith.call(42, '2', 1), false);
    240 assertEq(String.prototype.endsWith.call(42, '2', 4), true);
    241 assertEq(String.prototype.endsWith.call({ 'toString': function() { return 'abc'; } }, 'b', 0), false);
    242 assertEq(String.prototype.endsWith.call({ 'toString': function() { return 'abc'; } }, 'b', 1), false);
    243 assertEq(String.prototype.endsWith.call({ 'toString': function() { return 'abc'; } }, 'b', 2), true);
    244 assertThrows(function() { String.prototype.endsWith.call({ 'toString': function() { throw RangeError(); } }, /./); }, RangeError);
    245 assertThrows(function() { String.prototype.endsWith.call({ 'toString': function() { return 'abc' } }, /./); }, TypeError);
    246 
    247 assertThrows(function() { String.prototype.endsWith.apply(undefined); }, TypeError);
    248 assertThrows(function() { String.prototype.endsWith.apply(undefined, ['b']); }, TypeError);
    249 assertThrows(function() { String.prototype.endsWith.apply(undefined, ['b', 4]); }, TypeError);
    250 assertThrows(function() { String.prototype.endsWith.apply(null); }, TypeError);
    251 assertThrows(function() { String.prototype.endsWith.apply(null, ['b']); }, TypeError);
    252 assertThrows(function() { String.prototype.endsWith.apply(null, ['b', 4]); }, TypeError);
    253 assertEq(String.prototype.endsWith.apply(42, ['2']), true);
    254 assertEq(String.prototype.endsWith.apply(42, ['4']), false);
    255 assertEq(String.prototype.endsWith.apply(42, ['b', 4]), false);
    256 assertEq(String.prototype.endsWith.apply(42, ['2', 1]), false);
    257 assertEq(String.prototype.endsWith.apply(42, ['2', 4]), true);
    258 assertEq(String.prototype.endsWith.apply({ 'toString': function() { return 'abc'; } }, ['b', 0]), false);
    259 assertEq(String.prototype.endsWith.apply({ 'toString': function() { return 'abc'; } }, ['b', 1]), false);
    260 assertEq(String.prototype.endsWith.apply({ 'toString': function() { return 'abc'; } }, ['b', 2]), true);
    261 assertThrows(function() { String.prototype.endsWith.apply({ 'toString': function() { throw RangeError(); } }, [/./]); }, RangeError);
    262 assertThrows(function() { String.prototype.endsWith.apply({ 'toString': function() { return 'abc' } }, [/./]); }, TypeError);