-
Notifications
You must be signed in to change notification settings - Fork 0
/
foreach-map-filter.test.js
165 lines (152 loc) · 4.85 KB
/
foreach-map-filter.test.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
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
163
164
165
describe("#doubleValues", function() {
it("doubles values in an array", function() {
expect(doubleValues([1, 2, 3])).toEqual([2, 4, 6]);
});
it("works for negative numbers", function() {
expect(doubleValues([1, -2, -3])).toEqual([2, -4, -6]);
});
});
describe("#onlyEvenValues", function() {
it("returns a new array of only even values", function() {
expect(onlyEvenValues([1, 2, 3, 4, 5, 6])).toEqual([2, 4, 6]);
});
});
describe("#showFirstAndLast", function() {
it("returns an array of only the first and last characters in an array", function() {
expect(showFirstAndLast(["elie", "colt", "matt", "tim"])).toEqual([
"ee",
"ct",
"mt",
"tm"
]);
});
});
describe("#addKeyAndValue", function() {
it("adds a key and value to an array of objects", function() {
var arr = [
{ name: "Elie" },
{ name: "Tim" },
{ name: "Matt" },
{ name: "Colt" }
];
var updatedArr = [
{ name: "Elie", title: "instructor" },
{ name: "Tim", title: "instructor" },
{ name: "Matt", title: "instructor" },
{ name: "Colt", title: "instructor" }
];
expect(addKeyAndValue(arr, "title", "instructor")).toEqual(updatedArr);
});
});
describe("#vowelCount", function() {
it("returns an object with the keys as vowels and the values as the count", function() {
expect(vowelCount("elie")).toEqual({ e: 2, i: 1 });
expect(vowelCount("tim")).toEqual({ i: 1 });
expect(vowelCount("matt")).toEqual({ a: 1 });
expect(vowelCount("hmmm")).toEqual({});
expect(vowelCount("i am awesome and so are you")).toEqual({
i: 1,
a: 4,
e: 3,
o: 3,
u: 1
});
});
});
describe("#doubleValuesWithMap", function() {
it("doubles values in an array", function() {
expect(doubleValuesWithMap([1, 2, 3])).toEqual([2, 4, 6]);
});
it("works for negative numbers", function() {
expect(doubleValuesWithMap([1, -2, -3])).toEqual([2, -4, -6]);
});
});
describe("#valTimesIndex", function() {
it("returns a new array with each value multiplied by the index", function() {
expect(valTimesIndex([1, 2, 3])).toEqual([0, 2, 6]);
});
it("works for negative numbers", function() {
expect(valTimesIndex([1, -2, -3])).toEqual([0, -2, -6]);
});
});
describe("#extractKey", function() {
it("returns a new array with the value of each key in an array objects", function() {
var arr = [
{ name: "Elie" },
{ name: "Tim" },
{ name: "Matt" },
{ name: "Colt" }
];
expect(extractKey(arr, "name")).toEqual(["Elie", "Tim", "Matt", "Colt"]);
});
});
describe("#extractFulName", function() {
var arr = [
{ first: "Elie", last: "Schoppik" },
{ first: "Tim", last: "Garcia" },
{ first: "Matt", last: "Lane" },
{ first: "Colt", last: "Steele" }
];
it("returns a new array with the value of each key in an array objects", function() {
expect(extractFullName(arr, "name")).toEqual([
"Elie Schoppik",
"Tim Garcia",
"Matt Lane",
"Colt Steele"
]);
});
});
describe("#filterByValue", function() {
var arr = [
{ first: "Elie", last: "Schoppik" },
{ first: "Tim", last: "Garcia", isCatOwner: true },
{ first: "Matt", last: "Lane" },
{ first: "Colt", last: "Steele", isCatOwner: true }
];
it("returns a new array of objects that contain a key", function() {
expect(filterByValue(arr, "isCatOwner")).toEqual([
{ first: "Tim", last: "Garcia", isCatOwner: true },
{ first: "Colt", last: "Steele", isCatOwner: true }
]);
});
});
describe("#find", function() {
var arr = [1, 2, 3, 4, 5];
it("returns the first value found in an array", function() {
expect(find(arr, 3)).toEqual(3);
});
it("returns undefined if the value is not found", function() {
expect(find(arr, 10)).toEqual(undefined);
});
});
describe("#findInObj", function() {
var arr = [
{ first: "Elie", last: "Schoppik" },
{ first: "Tim", last: "Garcia", isCatOwner: true },
{ first: "Matt", last: "Lane" },
{ first: "Colt", last: "Steele", isCatOwner: true }
];
it("returns the first value found in an array", function() {
expect(findInObj(arr, "isCatOwner", true)).toEqual({
first: "Tim",
last: "Garcia",
isCatOwner: true
});
});
it("returns undefined if the value is not found", function() {
expect(findInObj(arr, "isCatOwner", false)).toEqual(undefined);
});
});
describe("#removeVowels", function() {
it("removes all vowels from a string", function() {
expect(removeVowels("elie")).toEqual("l");
expect(removeVowels("TIM")).toEqual("tm");
expect(removeVowels("ZZZZZZ")).toEqual("zzzzzz");
});
});
describe("#doubleOddNumbers", function() {
it("returns an array of all odd numbers doubled", function() {
expect(doubleOddNumbers([1, 2, 3, 4, 5])).toEqual([2, 6, 10]);
expect(doubleOddNumbers([4, 4, 4, 4, 4])).toEqual([]);
});
});