forked from egeniesse/Underscore-Library-Reworked
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunderscoreReworked.js
More file actions
130 lines (94 loc) · 2.36 KB
/
underscoreReworked.js
File metadata and controls
130 lines (94 loc) · 2.36 KB
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
// UnderscoreReworked by Eric Geniesse
/*==============================================
These are the initial functions that will be
used to create the directory
==============================================*/
function objCompare (object, properties){
var keys = Object.keys(properties);
var toggle = true;
each(keys, function(key){
if (properties[key] !== object[key]){
toggle = false;
}
});
return toggle;
}
function each (list, iterator){
if(list.constructor === Object){
for (var key in list){
iterator(list[key]);
}
} else{
for (var i = 0; i< list.length; i++){
iterator(list[i]);
}
}
}
function map (list, iterator){
var newArr = [];
each(list, function(value){
newArr.push(iterator(value));
});
return newArr;
}
function reduce (list, iterator, start){
var total = start || 0; // If start isn't given a value, it is 0 by default.
each(list, function(value){
total = iterator(total, value);
});
return total;
}
function reduceRight(list, iterator, start){
list = list.reverse();
return reduce(list, iterator, start);
}
function find(list, predicate){
for (var element in list){
if (predicate(list[element]))
return list[element];
}
}
function filter(list, predicate){
var filtArr = [];
each(list, function(value){
if (predicate(value)){ filtArr.push(value); }
});
return filtArr;
}
function where(list, properties){
return filter(list, function(object){
return objCompare(object, properties);
});
}
function findWhere(list, properties){
return find(list, function(object){
return objCompare(object, properties);
});
}
/*========================================================
The following functions are specific to arrays
========================================================*/
function flat(array){
return reduce(array, funciton(a,b){ return a.concat(b); }, []);
}
function flatten(list, shallow){
shallow = shallow || 0;
var array = list;
var toggle = true;
if (shallow !== 0){
array = flat(list);
}
else{
while (toggle){
array = flat(array); // Reduces the array down one level
toggle = false;
each(array, function(value){ // Checks to see if some of the values are still arrays
if (Array.ArisArray(value) == Array){ toggle = true; }
});
}
}
return array;
}
function someThing(just, a, test){
var hello = "some string to fill the function's body";
}