-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
171 lines (136 loc) · 3.78 KB
/
index.js
File metadata and controls
171 lines (136 loc) · 3.78 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
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
var _ = {};
/*********IDENTITY**********/
_.identity = function (val) {
return val;
};
/*********FIRST**********/
_.first = function (array, n) {
if (n) {
for (let i = 0; i < n; i++) {
return array[i];
}
} else {
return array[0];
}
};
/*********LAST**********/
_.last = function (array, n) {
if (n) {
for (let i = array.length - 1; i > array.length - (n + 1); i--) {
return array[i];
}
} else {
return array[array.length - 1];
}
};
/*********EACH**********/
_.each = function (list, iteratee, context) {
if (context) {
iteratee.bind(context);
}
if (Array.isArray(list)) {
for (let i = 0; i < list.length; i++) {
iteratee(list[i], i, list);
}
} else if (typeof list === "object" && !Array.isArray(list)) {
for (const [key, value] of Object.entries(list)) {
iteratee(value, key, list);
}
}
return list;
};
/*********INDEXOF**********/
_.indexOf = function (array, target, isSorted) {
function binarySearch(arr, tar) {
let low = 0;
let high = array.length - 1;
let mid;
while (high >= low) {
mid = Math.floor((low + high) / 2);
if (arr[mid] == tar) {
return mid;
} else if (arr[mid] > tar) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
if (isSorted && typeof isSorted === "boolean" && isSorted === "true") {
binarySearch(array, target);
} else if (isSorted && typeof isSorted === "number") {
for (let i = isSorted; i < array.length; i++) {
if (array[i] === target) {
return i;
}
}
return -1;
} else {
for (let i = 0; i < array.length; i++) {
if (array[i] === target) {
return i;
}
}
return -1;
}
};
/*********FILTER**********/
_.filter = function (collection, test, context) {
if (context) {
test.bind(context);
}
let resultArr = [];
for (let i = 0; i < collection.length; i++) {
if (test(collection[i])) {
resultArr.push(collection[i]);
}
}
return resultArr;
// "predicate is transformed through iteratee to facilitate shorthand syntaxes."
// WTF DOES THAT MEAN AVI????
};
/*********REJECT**********/
_.reject = function (collection, test) {};
/*********UNIQ**********/
_.uniq = function (array) {};
/*********MAP**********/
_.map = function (collection, iterator) {};
/*********PLUCK**********/
_.pluck = function (collection, key) {};
/*********INVOKE*********/
_.invoke = function (collection, func, args) {};
/*********REDUCE**********/
_.reduce = function (collection, iterator, accum) {};
/*********CONTAINS*********/
_.contains = function (collection, target) {};
/*********EVERY**********/
_.every = function (collection, iterator) {};
/********SOME**********/
_.some = function (collection, iterator) {};
/*********EXTEND**********/
_.extend = function (obj) {};
/*********DEFAULTS**********/
_.defaults = function (obj) {};
/*********ONCE**********/
_.once = function (func) {};
/*********MEMOIZE*************/
_.memoize = function (func) {};
/*********DELAY*********/
_.delay = function (func, wait) {};
/*********SHUFFLE**********/
_.shuffle = function (array) {};
/*********SORTBY**********/
_.sortBy = function (collection, iterator) {};
/*********ALTERNATE SORTBY USING MANUAL SORT**********/
_.sortByManual = function (collection, iterator) {};
/*********ZIP**********/
_.zip = function () {};
/*********FLATTEN**********/
_.flatten = function (nestedArray) {};
/*********UNION**********/
_.union = function () {};
/*********INTERSECTION**********/
_.intersection = function () {};
/*********DIFFERENCE**********/
_.difference = function (array) {};