array-flatMap.js 2.95 KB
Newer Older
1 2 3 4
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Flags: --allow-natives-syntax
6 7 8 9 10 11 12 13 14 15 16 17 18

assertEquals(Array.prototype.flatMap.length, 1);
assertEquals(Array.prototype.flatMap.name, 'flatMap');

assertEquals(
  [1, 2, 3, 4].flatMap((element) => [element, element ** 2]),
  [1, 1, 2, 4, 3, 9, 4, 16]
);
assertEquals(
  [1, 2, 3, 4].flatMap((element) => [[element, element ** 2]]),
  [[1, 1], [2, 4], [3, 9], [4, 16]]
);

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
{
  const elements = new Set([
    -Infinity,
    -1,
    -0,
    +0,
    +1,
    Infinity,
    null,
    undefined,
    true,
    false,
    '',
    'foo',
    /./,
    [],
    {},
    Object.create(null),
    new Proxy({}, {}),
    Symbol(),
    x => x ** 2,
    String
  ]);
42

43 44 45 46 47 48 49 50 51 52
  for (const value of elements) {
    assertEquals(
      [value].flatMap((element) => [element, element]),
      [value, value]
    );
  }
}

{
  const array = [42];
53
  assertEquals(
54 55
    [array].flatMap((element) => [element, element]),
    [array, array]
56 57 58
  );
}

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
{
  const nonCallables = new Set([
    -Infinity,
    -1,
    -0,
    +0,
    +1,
    Infinity,
    null,
    undefined,
    true,
    false,
    '',
    'foo',
    /./,
    [],
    {},
    Object.create(null),
    new Proxy({}, {}),
    Symbol(),
  ]);
  for (const nonCallable of nonCallables) {
    assertThrows(() => {
      [].flatMap(nonCallable);
    }, TypeError);
  }
85 86
}

87 88 89 90 91 92 93 94 95 96 97 98
{
  const object = {
    foo: 42,
    get length() {
      object.foo = 0;
    }
  };
  const result = [object].flatMap((element) => [element, element]);
  %HeapObjectVerify(result);
  assertEquals(result, [object, object]);
  assertEquals(result[0].foo, 42);
}
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

assertThrows(() => {
  Array.prototype.flatMap.call(null, (element) => element);
}, TypeError);
assertThrows(() => {
  Array.prototype.flatMap.call(undefined, (element) => element);
}, TypeError);

assertEquals(
  Array.prototype.flatMap.call(
    {
      length: 1,
      0: 'a',
      1: 'b',
    },
    (element) => element
  ),
  ['a']
);
assertEquals(
  Array.prototype.flatMap.call(
    {
      length: 2,
      0: 'a',
      1: 'b',
    },
    (element) => element
  ),
  ['a', 'b']
);
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

{
  const result = [1, 2, 3].flatMap(function() {
    return [this];
  }, 'abc');
  assertEquals(true, result[0] == 'abc');
  assertEquals(true, result[1] == 'abc');
  assertEquals(true, result[2] == 'abc');
}

{
  const input = { 0: 'a', 1: 'b', 2: 'c', length: 'wat' };
  assertEquals(Array.prototype.flatMap.call(input, x => [x]), []);
}

{
  let count = 0;
  const input = {
    get length() { ++count; return 0; }
  };
  const result = Array.prototype.flatMap.call(input, x => [x]);
  assertEquals(count, 1);
}

{
  const descriptor = Object.getOwnPropertyDescriptor(
    Array.prototype,
    'flatMap'
  );
  assertEquals(descriptor.value, Array.prototype.flatMap);
  assertEquals(descriptor.writable, true);
  assertEquals(descriptor.enumerable, false);
  assertEquals(descriptor.configurable, true);
}