summaryrefslogtreecommitdiff
path: root/test/built-ins/Array/prototype/findIndex/return-index-predicate-result-is-true.js
blob: bddf1e3e4cda3910009827f1cc8356afc1c83a4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (C) 2015 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-array.prototype.findindex
es6id: 22.1.3.9
description: >
  Return index if predicate return a boolean true value.
info: |
  22.1.3.9 Array.prototype.findIndex ( predicate[ , thisArg ] )

  ...
  8. Repeat, while k < len
    ...
    d. Let testResult be ToBoolean(Call(predicate, T, «kValue, k, O»)).
    e. ReturnIfAbrupt(testResult).
    f. If testResult is true, return k.
  ...
features: [Symbol]
---*/

var arr = ['Shoes', 'Car', 'Bike'];
var called = 0;

var result = arr.findIndex(function(val) {
  called++;
  return true;
});

assert.sameValue(result, 0);
assert.sameValue(called, 1, 'predicate was called once');

called = 0;
result = arr.findIndex(function(val) {
  called++;
  return val === 'Bike';
});

assert.sameValue(called, 3, 'predicate was called three times');
assert.sameValue(result, 2);

result = arr.findIndex(function(val) {
  return 'string';
});
assert.sameValue(result, 0, 'coerced string');

result = arr.findIndex(function(val) {
  return {};
});
assert.sameValue(result, 0, 'coerced object');

result = arr.findIndex(function(val) {
  return Symbol('');
});
assert.sameValue(result, 0, 'coerced Symbol');

result = arr.findIndex(function(val) {
  return 1;
});
assert.sameValue(result, 0, 'coerced number');

result = arr.findIndex(function(val) {
  return -1;
});
assert.sameValue(result, 0, 'coerced negative number');