forked from OperationSpark/lodown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
307 lines (279 loc) · 8.55 KB
/
index.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// This makes the arguments variable behave the way we want it to and a few
// other things. For more info:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode
'use strict';
const _ = {};
/**
* START OF OUR LIBRARY!
* Implement each function below it's instructions
*/
/** _.typeOf
* Arguments:
* 1) Any value
* Objectives:
* 1) Return the type of <value> as a string
* Types are one of:
* - "string"
* - "array"
* - "object"
* - "undefined"
* - "number"
* - "boolean"
* - "null"
* - "function"
* Examples:
* _.typeOf(134) -> "number"
* _.typeOf("javascript") -> "string"
* _.typeOf([1,2,3]) -> "array"
*/
_.typeOf = function(value){
if (typeof value === "number") {
return "number";
} if(typeof value === "string"){
return "string"
} if(value === null){
return "null"
} if (Array.isArray(value) === true){
return "array"
} if (typeof value === "object"){
return "object"
} if(typeof value === "boolean"){
return "boolean"
} if (typeof value === "undefined"){
return "undefined"
}
};
_.funcName = function(params){}
/** _.first
* Arguments:
* 1) An array
* 2) A number
* Objectives:
* 1) If <array> is not an array, return []
* 2) If <number> is not given or not a number, return just the first element in <array>.
* 3) Otherwise, return the first <number> items of <array>
* Edge Cases:
* 1) What if <number> is negative?
* 2) What if <number> is greater than <array>.length?
* Examples:
* _.first("ponies", 1) -> []
* _.first(["a", "b", "c"], "ponies") -> "a"
* _.first(["a", "b", "c"], 1) -> "a"
* _.first(["a", "b", "c"], 2) -> ["a", "b"]
*/
// }
/** _.last
* Arguments:
* 1) An array
* 2) A number
* Objectives:
* 1) If <array> is not an array, return []
* 2) If <number> is not given or not a number, return just the last element in <array>.
* 3) Otherwise, return the last <number> items of <array>
* Edge Cases:
* 1) What if <number> is negative?
* 2) What if <number> is greater than <array>.length?
* Examples:
* _.last("ponies", 2) -> []
* _.last(["a", "b", "c"], "ponies") -> "c"
* _.last(["a", "b", "c"], 1) -> "c"
* _.last(["a", "b", "c"], 2) -> ["b", "c"]
*/
/** _.indexOf
* Arguments:
* 1) An array
* 2) A value
* Objectives:
* 1) Return the index of <array> that is the first occurrance of <value>
* 2) Return -1 if <value> is not in <array>
* 3) Do not use [].indexOf()!
* Edge Cases:
* 1) What if <array> has multiple occurances of val?
* 2) What if <val> isn't in <array>?
* Examples:
* _.indexOf(["a","b","c"], "c") -> 2
* _.indexOf(["a","b","c"], "d") -> -1
*/
// _.typeOf = function(array ,value){
// var result = 0;
// for (var i = 0; i < array.length; i++){
// if (array[i]=== value){
// result = i;
// }
// }
// return result
_.indexOf = function(array, value){
for(var i =0; array.length < i; i++){
if(indexOF [i] = null){
return -1
}
return false;
}
return true;}
/** _.contains
* Arguments:
* 1) An array
* 2) A value
* Objectives:
* 1) Return true if <array> contains <value>
* 2) Return false otherwise
* 3) You must use the ternary operator in your implementation.
* Edge Cases:
* 1) did you use === ?
* 2) what if no <value> is given?
* Examples:
* _.contains([1,"two", 3.14], "two") -> true
*/
/** _.each
* Arguments:
* 1) A collection
* 2) A function
* Objectives:
* 1) if <collection> is an array, call <function> once for each element
* with the arguments:
* the element, it's index, <collection>
* 2) if <collection> is an object, call <function> once for each property
* with the arguments:
* the property's value, it's key, <collection>
* Examples:
* _.each(["a","b","c"], function(e,i,a){ console.log(e)});
* -> should log "a" "b" "c" to the console
*/
_.each = function(collection, fn){
if(Array.isArray(collection)){
for(let i = 0; i < collection.length; i++){
fn(collection[i] , i , collection);
}
}else{
for(let key in collection){
fn(collection[key], key, collection);
}
}
}
/** _.filter
* Arguments:
* 1) An array
* 2) A function
* Objectives:
* 1) call <function> for each element in <array> passing the arguments:
* the element, it's index, <array>
* 2) return a new array of elements for which calling <function> returned true
* Edge Cases:
* 1) What if <function> returns something other than true or false?
* Examples:
* _.filter([1,2,3,4,5], function(x){return x%2 === 0}) -> [2,4]
* Extra Credit:
* use _.each in your implementation
*/
/** _.map
* Arguments:
* 1) A collection
* 2) a function
* Objectives:
* 1) call <function> for each element in <collection> passing the arguments:
* if <collection> is an array:
* the element, it's index, <collection>
* if <collection> is an object:
* the value, it's key, <collection>
* 2) save the return value of each <function> call in a new array
* 3) return the new array
* Examples:
* _.map([1,2,3,4], function(e){return e * 2}) -> [2,4,6,8]
*/
/** _.reject
* Arguments:
* 1) An array
* 2) A function
* Objectives:
* 1) call <function> for each element in <array> passing the arguments:
* the element, it's index, <array>
* 2) return a new array of elements for which calling <function> returned false
* 3) This is the logical inverse if _.filter(), you must use _.filter() in your implementation
* Examples:
* _.reject([1,2,3,4,5], function(e){return e%2 === 0}) -> [1,3,5]
*/
/** _.partition
* Arguments:
* 1) An array
* 2) A function
* Objectives:
* 1) Call <function> for each element in <array> passing it the arguments:
* element, key, <array>
* 2) Return an array that is made up of 2 sub arrays:
* 0) An array that contains all the values for which <function> returned something truthy
* 1) An array that contains all the values for which <function> returned something falsy
* Edge Cases:
* 1) This is going to return an array of arrays.
* Examples:
* _.partition([1,2,3,4,5], function(element,index,arr){
* return element % 2 === 0;
* }); -> [[2,4],[1,3,5]]
}
*/
/** _.every
* Arguments:
* 1) A collection
* 2) A function
* Objectives:
* 1) Call <function> for every element of <collection> with the paramaters:
* if <collection> is an array:
* current element, it's index, <collection>
* if <collection> is an object:
* current value, current key, <collection>
* 2) If the return value of calling <function> for every element is true, return true
* 3) If even one of them returns false, return false
* 4) If <function> is not provided, return true if every element is truthy, otherwise return false
* Edge Cases:
* 1) what if <function> doesn't return a boolean
* 2) What if <function> is not given?
* Examples:
* _.every([2,4,6], function(e){return e % 2 === 0}) -> true
* _.every([1,2,3], function(e){return e % 2 === 0}) -> false
*/
_.every = function(collection, test){
let allTrue = true;
_.each(collection, function(value, loc, collection){
if(!test(value, loc, collection)){
allTrue = false;
}
});
return allTrue;
}
/** _.some
* Arguments:
* 1) A collection
* 2) A function
* Objectives:
* 1) Call <function> for every element of <collection> with the paramaters:
* if <collection> is an array:
* current element, it's index, <collection>
* if <collection> is an object:
* current value, current key, <collection>
* 2) If the return value of calling <function> is true for at least one element, return true
* 3) If it is false for all elements, return false
* 4) If <function> is not provided return true if at least one element is truthy, otherwise return false
* Edge Cases:
* 1) what if <function> doesn't return a boolean
* 2) What if <function> is not given?
* Examples:
* _.some([1,3,5], function(e){return e % 2 === 0}) -> false
* _.some([1,2,3], function(e){return e % 2 === 0}) -> true
*/
/** _.pluck
* Arguments:
* 1) An array of objects
* 2) A property
* Objectives:
* 1) Return an array containing the value of <property> for every element in <array>
* 2) You must use _.map() in your implementation.
* Examples:
* _.pluck([{a: "one"}, {a: "two"}], "a") -> ["one", "two"]
*/
//////////////////////////////////////////////////////////////////////
// DON'T REMOVE THIS CODE ////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
if((typeof process !== 'undefined') &&
(typeof process.versions.node !== 'undefined')) {
// here, export any references you need for tests //
module.exports = _;
}