tor-browser

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

object-rest.js (2113B)


      1 function test() {
      2    var from, to;
      3 
      4    // From values.
      5    from = {x: 1, y: 2};
      6    ({...to} = from);
      7    assertEq(to.y, 2);
      8 
      9    var z;
     10    from = {x: 1, y: 2};
     11    ({x: z, ...to} = from);
     12    assertEq(z, 1);
     13    assertEq(to.y, 2);
     14 
     15    // From getter.
     16    var c = 7;
     17    from = {x: 1, get y() { return ++c; }};
     18    ({...to} = from);
     19    assertEq(c, 8);
     20    assertEq(to.y, 8);
     21 
     22    from = {x: 1, get y() { return ++c; }};
     23    ({y: z, ...to} = from);
     24    assertEq(c, 9);
     25    assertEq(z, 9);
     26    assertEq(to.y, undefined);
     27 
     28    // Array with dense elements.
     29    from = [1, 2, 3];
     30    ({...to} = from);
     31    assertEq(to[2], 3);
     32    assertEq("length" in to, false);
     33 
     34    from = [1, 2, 3];
     35    ({2: z, ...to} = from);
     36    assertEq(z, 3);
     37    assertEq(to[2], undefined);
     38    assertEq(to[0], 1);
     39    assertEq("length" in to, false);
     40 
     41    // Object with sparse elements and symbols.
     42    from = {x: 1, 1234567: 2, 1234560: 3, [Symbol.iterator]: 5, z: 3};
     43    ({...to} = from);
     44    assertEq(to[1234567], 2);
     45    assertEq(Object.keys(to).toString(), "1234560,1234567,x,z");
     46    assertEq(to[Symbol.iterator], 5);
     47 
     48    from = {x: 1, 1234567: 2, 1234560: 3, [Symbol.iterator]: 5, z: 3};
     49    ({[Symbol.iterator]: z, ...to} = from);
     50    assertEq(to[1234567], 2);
     51    assertEq(Object.keys(to).toString(), "1234560,1234567,x,z");
     52    assertEq(to[Symbol.iterator], undefined);
     53    assertEq(z, 5);
     54 
     55    // Typed array.
     56    from = new Int32Array([1, 2, 3]);
     57    ({...to} = from);
     58    assertEq(to[1], 2);
     59 
     60    from = new Int32Array([1, 2, 3]);
     61    ({1: z, ...to} = from);
     62    assertEq(z, 2);
     63    assertEq(to[1], undefined);
     64    assertEq(to[2], 3);
     65 
     66    // Primitive string.
     67    from = "foo";
     68    ({...to} = from);
     69    assertEq(to[0], "f");
     70 
     71    from = "foo";
     72    ({0: z, ...to} = from);
     73    assertEq(z, "f");
     74    assertEq(to[0], undefined);
     75    assertEq(to[1], "o");
     76 
     77    // String object.
     78    from = new String("bar");
     79    ({...to} = from);
     80    assertEq(to[2], "r");
     81 
     82    from = new String("bar");
     83    ({1: z, ...to} = from);
     84    assertEq(z, "a");
     85    assertEq(to[1], undefined);
     86    assertEq(to[2], "r");
     87 }
     88 test();
     89 test();
     90 test();