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
65
66
67
68
69
70
71
72
73
74
| 'use strict';
|
| var test = require('tape').test;
| var find = require('./find.js');
|
| test('can find an element', function (t) {
| var arr = [1, 2, 3, 4, 5];
| var found = find(arr, function (el) {
| return el === 2;
| });
| t.equal(2, found);
| t.end();
| });
|
| test('can find an element from an object', function (t) {
| var arr = [{name: 'adam'}, {name: 'eve'}, {name: 'john'}];
| var found = find(arr, function (el) {
| return el.name === 'eve';
| });
| t.deepEqual({name: 'eve'}, found);
| t.end();
| });
|
| test('returns undefined if no callback returns true', function (t) {
| var arr = [6, 7, 8];
| var found = find(arr, function (el) {
| return el === 1;
| });
| t.equal(found, undefined);
| t.end();
| });
|
| test('setting object as thisArg', function (t) {
| t.plan(3);
| function Car() {}
| var car = new Car();
| var arr = [1, 2, 3];
| find(arr, function (el) {
| t.ok(this instanceof Car, 'this should be instanceof Car');
| }, car);
| });
|
| test('loop should exit on first find', function (t) {
| t.plan(3);
| var arr = [1, 2, 3, 4, 5];
| find(arr, function (el, index) {
| t.ok(true, 'element ' + index);
| return el === 3;
| });
| });
|
| test(
| 'third callback argument array should be equal to first argument array',
| function (t) {
| t.plan(3);
| var arr = [1, 2, 3];
| find(arr, function (el, index, array) {
| t.deepEqual(arr, array);
| });
| }
| );
|
| test(
| 'should throw TypeError if second argument is not a function',
| function (t) {
| t.plan(1);
| var arr = [1, 2, 3];
| try {
| find(arr, 'string');
| } catch (e) {
| t.ok(e instanceof TypeError);
| }
| }
| );
|
|