forked from itsgreggreg/underpants
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.html
204 lines (185 loc) · 10.2 KB
/
index.html
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Underpants Library</title>
<link rel="stylesheet" href="https://code.jquery.com/qunit/qunit-1.19.0.css">
<script src="https://code.jquery.com/qunit/qunit-1.19.0.js"></script>
<meta name="description" content="Underpants: functional functions for fun">
</head>
<body>
<div id="qunit"></div>
<script src="underpants.js"></script>
<script>
/* global _ */
QUnit.test("_.identity()", function(assert) {
assert.strictEqual( _.identity(14), 14, "Should handle numbers.");
assert.deepEqual( _.identity({a: "one"}), {a: "one"}, "Should handle objects.");
assert.strictEqual(_.identity("hello there"), "hello there", "Should handle strings.");
assert.deepEqual(_.identity([1,2,3]), [1,2,3], "Should handle arrays.");
});
QUnit.test("_.typeof()", function(assert) {
assert.strictEqual(_.typeOf("a"), "string", "Should handle strings.");
assert.strictEqual(_.typeOf(10), "number", "Should handle numbers.");
assert.strictEqual(_.typeOf([1,3]), "array", "Should handle arrays.");
assert.strictEqual(_.typeOf({a: "one"}), "object", "Should handle objects.");
assert.strictEqual(_.typeOf(false), "boolean", "Should handle booleans.");
assert.strictEqual(_.typeOf(undefined), "undefined", "Should handle undefined.");
assert.strictEqual(_.typeOf(null), "null", "Should handle null.");
assert.strictEqual(_.typeOf(function(){}), "function", "Should handle functions.");
});
QUnit.test("_.first()", function(assert){
assert.deepEqual(_.first(["a","b","c"]) ,"a", "Should return the first element if no numerical argument is given.");
assert.deepEqual(_.first(["a","b","c"],2) ,["a","b"], "Should accept an argument representing the number of items to include in the output.");
assert.deepEqual(_.first(["a","b","c"], -1) ,[], "Should return empty list if numerical argument is not a positive number.");
assert.deepEqual(_.first(["a","b","c"], 5) ,["a","b","c"], "Should return the whole array if numerical argument is greater than the array's length.");
assert.deepEqual(_.first({a:"b"}, 2), [], "Should return empty array if the array param is not an array.");
});
QUnit.test("_.last()", function(assert){
assert.deepEqual(_.last(["a","b","c"]) ,"c", "Should return the last element if no numerical argument is given.");
assert.deepEqual(_.last(["a","b","c"],2) ,["b","c"], "Should accept an argument representing the number of items to include in the output.");
assert.deepEqual(_.last(["a","b","c"], -1) ,[], "Should return empty list if numerical argument is not a positive number.");
assert.deepEqual(_.last(["a","b","c"], 5) ,["a","b","c"], "Should return the whole array if numerical argument is greater than the array's length.");
assert.deepEqual(_.last({a:"b"}, 2), [], "Should return empty array if the array param is not an array.");
});
QUnit.test("_.indexOf()", function(assert){
var inputData = ["a","b","c","d"];
assert.deepEqual(_.indexOf(inputData, "b") , 1, "Should return the correct index when an element is found.");
assert.deepEqual(_.indexOf(inputData.concat("b"), "b") , 1, "Should return the index of the first occurance of a found element.");
assert.deepEqual(_.indexOf(inputData, "e") , -1, "Should return -1 if the element is not found.");
assert.deepEqual(inputData, ["a","b","c","d"], "Should not have side effects.");
});
QUnit.test("_.contains()", function(assert){
var inputData = [1,"3",4,5,"a","4","b"];
assert.strictEqual(_.contains(inputData, "a") , true, "Should return true if a list contains an element.");
assert.strictEqual(_.contains(inputData, "c") , false, "Should return false if a list doesn't contain an element.");
assert.strictEqual(_.contains(inputData, 3) , false, "Should not convert types when checking.");
assert.deepEqual(inputData, [1,"3",4,5,"a","4","b"], "Should not have side effects.");
});
QUnit.test("_.each()", function(assert){
var inputArray = [1,2,3,4,5];
inputArray.ignoreMe = "this shouldn't show up";
var inputObject = {a:"1",b:"2",c:"3",d:"4"};
_.each(inputArray, function(e, i, a){
inputArray[i] = e*a.length;
});
assert.deepEqual(inputArray ,[5,10,15,20,25] , "Should handle arrays.");
_.each(inputObject, function(v, k, o){
inputObject[v] = k + Object.keys(o).length;
delete inputObject[k];
});
assert.deepEqual(inputObject,{1:"a4", 2:"b4", 3:"c4", 4:"d4"} , "Should handle Objects.");
});
QUnit.test("_.unique()", function(assert){
var inputData = ["a",1,1,"a","c",false,"b",5,"c",null, false, null];
assert.deepEqual(_.unique(inputData),["a",1,"c",false,"b",5,null], "Should return an array with no duplicates.");
assert.deepEqual(inputData, ["a",1,1,"a","c",false,"b",5,"c",null, false, null], "Should not have side effects.");
});
QUnit.test("_.filter()", function(assert){
var inputData = ["a",1,"b",2,"c",4];
assert.deepEqual(_.filter(inputData, function(e,i,a){
return typeof e === "string" && i < a.length/2;
}), ["a","b"], "Should filter elements in an array.");
assert.deepEqual(inputData, ["a",1,"b",2,"c",4], "Should not have side effects.");
});
QUnit.test("_.reject()", function(assert){
var inputData = ["a",1,"b",2,"c",4];
assert.deepEqual(_.reject(inputData, function(e,i,a){
return typeof e === "string" || i < a.length/2;
}), [2,4], "Should reject elements in an array.");
assert.deepEqual(inputData, ["a",1,"b",2,"c",4], "Should not have side effects.");
});
QUnit.test("_.partition()", function(assert){
var inputData = ["a",1,"b",2,"c",4];
assert.deepEqual(_.partition(inputData, function(e,i,a){
return typeof e === "string";
}), [["a","b","c"],[1,2,4]], "Should reject elements in an array.");
assert.deepEqual(inputData, ["a",1,"b",2,"c",4], "Should not have side effects.");
});
QUnit.test("_.map", function(assert){
var inputArray = ["a","b","c","d"];
var inputObject = {"a":1, "b":2, "c":3, "d":4};
assert.deepEqual(_.map(inputArray, function(e,i,a){
return e + i * a.length;
}), ["a0", "b4", "c8", "d12"], "Should map through arrays.");
assert.deepEqual(_.map(inputObject, function(v,k,o){
return k + v * Object.keys(o).length;
}), ["a4", "b8", "c12", "d16"], "Should map through Objects.");
assert.deepEqual([inputArray, inputObject],[["a","b","c","d"],{"a":1, "b":2, "c":3, "d":4}],"Should not have side effects.");
});
QUnit.test("_.pluck()", function(assert){
var inputData = [
{ name: "Ralph", age: 22},
{ name: "Jimmy", age: 13},
{ name: "Carla", age: 20}
];
assert.deepEqual(_.pluck(inputData, "name"), ["Ralph","Jimmy","Carla"], "Should pluck properties out of a list of objects.");
assert.deepEqual(inputData, [
{ name: "Ralph", age: 22},
{ name: "Jimmy", age: 13},
{ name: "Carla", age: 20}
], "Should not have side effects.");
});
QUnit.test("_.every()", function(assert){
var inputData = [2,4,6,7,8];
var inputDataTruthy = [1, [], true, "a"];
var inputDataFalsy = ["",0,false,null];
var inputObject = {a:"one",b:"two",c:"three"};
assert.deepEqual(_.every(inputData, function(v){
return v % 2 === 0 || v === 7;
}) , true, "Should return true when all iterations are true");
assert.deepEqual(_.every(inputData, function(v){
return v % 2 === 0;
}) , false, "Should return false when not all iterations are true");
assert.deepEqual(_.every(inputObject, function(v,k,o){
return ["aone3","btwo3","cthree3"].indexOf(k+v+Object.keys(o).length) !== -1;
}), true, "Should handle objects");
assert.deepEqual(_.every(inputDataTruthy), true, "Should return true for truthy results when no function is passed in.");
assert.deepEqual(_.every(inputDataFalsy), false, "Should return false for falsy results when no function is passed in.");
assert.deepEqual(inputData, [2,4,6,7,8], "Should not have side effects.");
});
QUnit.test("_.some()", function(assert){
var inputData = [2,4,6,7,8];
var inputDataTruthy = [1, [], true, "a"];
var inputDataFalsy = ["",0,false,null];
var inputObject = {a:"one",b:"two",c:"three"};
assert.deepEqual(_.some(inputData, function(v){
return v === 7;
}) , true, "Should return true when at least one iteration is true");
assert.deepEqual(_.some(inputData, function(v){
return v > 10;
}) , false, "Should return false when no iterations are true");
assert.deepEqual(_.some(inputObject, function(v,k,o){
return ["aone3","btwo3"].indexOf(k+v+Object.keys(o).length) !== -1;
}), true, "Should handle objects");
assert.deepEqual(_.some(inputDataTruthy), true, "Should return true for truthy results when no function is passed in.");
assert.deepEqual(_.some(inputDataFalsy), false, "Should return false for falsy results when no function is passed in.");
assert.deepEqual(inputData, [2,4,6,7,8], "Should not have side effects.");
});
QUnit.test("_.reduce()", function(assert){
var inputArray = [10,20,30,40];
assert.strictEqual(_.reduce(inputArray, function(memo, element, i){
return memo + element + i;
}, 10), 116, "Should work with an array and a seed");
assert.strictEqual(_.reduce(inputArray, function(memo, element, i){
return memo * element * (i+1);
}), 5760000, "Should work without a seed");
assert.strictEqual(_.reduce(inputArray, function(memo, element, i){
return memo * element * (i+1);
}, 0), 0, "Should work when seed is falsy");
assert.deepEqual(inputArray, [10,20,30,40], "Should not have side effects");
});
QUnit.test("_.extend()", function(assert){
var inputData = {a:"one", b:"two"};
_.extend(inputData, {c: "three", d: "four"});
assert.deepEqual(inputData, {a: "one",b:"two",c:"three",d:"four"}, "Should extend an object.");
inputData = {a:"one", b:"two"};
_.extend(inputData, {a: "three", d: "four"});
assert.deepEqual(inputData, {a: "three",b:"two",d:"four"} , "Should overwrite existing properties");
inputData = {a:"one", b:"two"};
_.extend(inputData);
assert.deepEqual(_.extend(inputData, {a:"three",c:"four"}, {d:"five",c:"six"}), {a:"three",b:"two",c:"six",d:"five"}, "Should handle any number of arguments.");
});
</script>
</body>
</html>