• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
16
17function mustThrow(str) {
18  try {
19    eval(str);
20    assert(false);
21  } catch (e) {
22    assert(e instanceof TypeError);
23  }
24}
25
26function assertArrayEqual(actual, expected) {
27  assert(actual.length === expected.length);
28
29  for (var i = 0; i < actual.length; i++) {
30    assert(actual[i] === expected[i]);
31  }
32}
33
34// Spread syntax
35function sum(x, y, z) {
36  return x + y + z;
37}
38
39const numbers = [1, 2, 3];
40
41assert(sum(...numbers) === 6);
42
43// Replace apply()
44function myFunction (v, w, x, y, z) {
45  return v + w + x + y + z;
46}
47var args = [0, 1];
48
49assert(myFunction (-1, ...args, 2, ...[3]) == 5);
50
51// Apply for new
52var dateFields = [1970, 0, 1];
53var d = new Date(...dateFields);
54
55assert(d.toString().substring(0, 24) === "Thu Jan 01 1970 00:00:00");
56
57function applyAndNew(constructor, args) {
58   function partial () {
59      return constructor.apply (this, args);
60   };
61   if (typeof constructor.prototype === "object") {
62      partial.prototype = Object.create(constructor.prototype);
63   }
64   return partial;
65}
66
67function myConstructor () {
68   assertArrayEqual(arguments, myArguments);
69   this.prop1 = "val1";
70   this.prop2 = "val2";
71};
72
73var myArguments = ["hi", "how", "are", "you", "mr", null];
74var myConstructorWithArguments = applyAndNew(myConstructor, myArguments);
75
76var obj = new myConstructorWithArguments;
77assert(Object.keys(obj).length === 2);
78assert(obj.prop1 === "val1");
79assert(obj.prop2 === "val2");
80
81// Test spread prop call
82var o = { f(a,b,c) { return a + b + c },
83          g(a,b) { throw new TypeError ("5") }
84        };
85
86assert (o.f(...["a", "b", "c"]) === "abc");
87
88mustThrow ("o.g (...[1,2])")
89
90// Test spread super call
91class MyArray extends Array {
92  constructor(...args) {
93    super(...args);
94  }
95}
96
97var array = new MyArray(1, 2, 3);
98assertArrayEqual(array, [1,2,3]);
99assert(array instanceof MyArray);
100assert(array instanceof Array);
101
102function argumentOrderTest() {
103  var result = []
104  for (i = 0; i < arguments.length; i++) {
105      result.push(arguments[i]);
106  }
107
108  return result;
109}
110
111assertArrayEqual(argumentOrderTest(1, 2, ...[3, 4]), [1, 2, 3, 4]);
112