-
Notifications
You must be signed in to change notification settings - Fork 1
/
_.js
240 lines (209 loc) · 5.25 KB
/
_.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
const _ = {
clamp(number, lower, upper) {
let lowerClampedValue = Math.max(number, lower);
let clampedValue = Math.min(lowerClampedValue, upper);
return clampedValue;
},
inRange(number, start, end) {
if (end === undefined) {
end = start;
start = 0;
}
if (start > end) {
let temp = end;
end = start;
start = temp;
}
let isInRange = start <= number && number < end;
return isInRange;
},
words(string) {
let words = string.split(' ');
return words;
},
pad(string, length) {
if (length <= string.length) {
return string;
}
let startPaddingLength = Math.floor((length - string.length) / 2);
let endPaddingLength = length - string.length - startPaddingLength;
let paddedString = ' '.repeat(startPaddingLength) + string + ' '.repeat(endPaddingLength);
return paddedString;
},
has(object, key) {
let hasValue = object[key];
if (object[key] === undefined) {
hasValue = false;
}
else if (object[key]) {
hasValue = true;
}
return hasValue;
},
invert(object) {
let invertedObject = {};
for (let i in object) {
let originalValue = object[i];
invertedObject = { originalValue: i };
}
return invertedObject;
},
findKey(object, predicate) {
for (let i in object) {
let value = object[i];
let predicateReturnValue = predicate(value);
if (predicateReturnValue) {
return i;
}
}
return undefined;
},
drop(array, n) {
if (!n) {
n = 1;
}
let droppedArray = array.slice(n);
return droppedArray;
},
dropWhile(array, predicate) {
let dropNumber = array.findIndex(function (element, index) {
return !(predicate(element, index, array))
});
let droppedArray = this.drop(array, dropNumber);
return droppedArray;
},
chunk(array, size) {
if (size === undefined) {
size = 1;
}
let arrayChunks = [];
for (let i = 0; i < array.length; i += size) {
let arrayChunk = array.slice(i, i + size);
arrayChunks.push(arrayChunk);
}
return arrayChunks;
},
stringReverse(string) {
return string.split('').reverse().join('');
},
isPalindrome(string) {
if(string === this.stringReverse(string)) {
return true;
}
return false;
},
everyNth(array, nth) {
return array.filter((e, i) => i % nth === nth - 1);
},
filterFalsy(array) {
return array.filter(Boolean);
},
bifurcate(array, filter) {
return array.reduce((acc, v, i) => {
acc[filter[i] ? 0 : 1].push(v);
return acc;
},[[], []]);
},
capAllElements(arr) {
arr.forEach((el, index, array) => {
array[index] = el.toUpperCase();
});
return arr;
},
intersection(array1, array2) {
const s = new Set(array2);
return array1.filter(x => s.has(x));
},
union(array1, array2) {
return Array.from(new Set([...array1, ...array2]));
},
reject(predicate, array) {
return array.filter((e) => !predicate(e));
},
sample(array) {
return array[Math.floor(Math.random() * array.length)];
},
unique(array) {
return [...new Set(array)];
},
none(array, predicate = Boolean) {
return !array.some(predicate);
},
readFileLines(filename) {
const fs = require('fs');
return fs
.readFileSync(filename)
.toString('UTF8')
.split('\n');
},
move(array, offset) {
return [...array.slice(offset), ...array.slice(0,offset)];
},
isObjectLike(param) {
return param !== null && typeof param === 'object';
},
// a set of ready to use filters designed to use with array.filter()
/* inBetween usage
let array = [1, 2, 3, 4, 5, 6, 7];
console.log(array.filter(inBetween(3, 6)));
// 3,4,5,6
*/
inBetween(a, b) {
return function(element) {
return element >= a && element <= b;
};
},
/* inArray usage
let array = [1, 2, 3, 4, 5, 6, 7];
console.log(array.filter(inArray([1, 2, 10])) );
// 1,2
*/
inArray(array) {
return function(element) {
return array.includes(element);
};
},
//helper function to sort by field
/* byField usage
let users = [
{ name: "John", age: 20, surname: "Johnson" },
{ name: "Pete", age: 18, surname: "Peterson" },
{ name: "Ann", age: 19, surname: "Hathaway" }
];
users.sort(byField('name'));
users.forEach(user => console.log(user.name));
// Ann, John, Pete
users.sort(byField('age'));
users.forEach(user => console.log(user.name));
// Pete, Ann, Joh
*/
byField(field) {
return (a, b) => a[field] > b[field] ? 1 : -1; //uses comparefunction concept in array.sort()
},
//this is a function that will take another function as argument and convert it into a curried function
/*
Usage is as follows:
function abc(a, b, c) {
return a + b + c;
}
var curriedAbc = curry(abc);
console.log(curriedAbc(1)(2)(3)); //6
*/
curry(fx) {
let arity = fx.length; //arity of a function is the number of arguments or operands that the function takes
return function f1() {
let args = Array.prototype.slice.call(arguments, 0);
if(args.length >= arity) {
return fx.apply(null, args);
}
else{
return function f2() {
let args2 = Array.prototype.slice.call(arguments, 0);
return f1.apply(null, args.concat(args2));
};
}
};
}
};
// Do not write or modify code below this line
module.exports = _;