-
Notifications
You must be signed in to change notification settings - Fork 1
/
array.test.ts
59 lines (47 loc) · 1.51 KB
/
array.test.ts
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
import { range, head, tail, map } from '../src/arrays';
describe('range', () => {
it('makes an array of the specified length', () => {
const arr = range(3);
expect(Array.isArray(arr)).toEqual(true);
expect(arr.length).toEqual(3);
});
it('uses indexes for array values', () => {
expect(range(4)).toEqual([0, 1, 2, 3]);
});
it('creates an empty array', () => {
expect(range(0)).toEqual([]);
});
});
describe('head', () => {
it('gets the first item in an array', () => {
expect(head([7, 8, 9])).toEqual(7);
});
it('returns undefined if there is no head', () => {
expect(head([])).toEqual(undefined);
});
});
describe('tail', () => {
it('gets every item from an array other than the first', () => {
expect(tail([7, 8, 9])).toEqual([8, 9]);
});
it('returns an empty array if there is no tail', () => {
expect(tail(['foo'])).toEqual([]);
});
});
describe('map', () => {
it('applies an iteratee function to the items in an array', () => {
const nums = map(['4', '5', '6'], Number);
expect(nums).toEqual([4, 5, 6]);
});
it('passes the index as the second argument', () => {
const indexes = map(['foo', 'bar', 'baz'], (_, i) => i);
expect(indexes).toEqual([0, 1, 2]);
});
it('passes the array as the third argument', () => {
const trues = map([true, false], (_, __, arr) => head(arr));
expect(trues).toEqual([true, true]);
});
it('returns an empty array if passed an empty array', () => {
expect(map([], () => {})).toEqual([]);
});
});