-
Notifications
You must be signed in to change notification settings - Fork 0
/
_.js
136 lines (125 loc) · 3.33 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
let _ = {
/*clamp - takes 3 paramters, an upper and lower boundary and the number which is bound to the lower boundary
if the number is less than the lower bound and to the upper bound if number > upper bound
*/
clamp (number, lower, upper) {
let lowerClampedValue = Math.max(number, lower);
let clampedValue = Math.min(lowerClampedValue, upper);
return clampedValue;
},
/*inRange - takes 3 paramters
returns true if the number falls between the start and end parameters
otherwise returns false
*/
inRange(number,start,end) {
if (!end) {
end = start;
start = 0;
};
if (start > end) {
let temp = end;
end = start;
start = temp;
};
let isInRange = false;
if (number >= start && number < end) {
return isInRange = true;
}
else {isInRange = false}
return isInRange;
},
/*words
takes a string arguments and returns an array of word
split by a space
*/
words(string) {
let words = string.split(' ')
return words;
},
/*pad
pads the passed string with spaces to the total length
*/
pad(string,length) {
const stringLength = string.length;
if (stringLength >= length) {
return string;
}
let startPadding = Math.floor((length - stringLength)/2);
let endPadding = length - (startPadding + stringLength);
const paddedString = ' '.repeat(startPadding) + string + ' '.repeat(endPadding);
return paddedString;
},
/*has
checks that the passed object has a key specified in the arguments
*/
has(object, path) {
const hasValue = object.hasOwnProperty(path);
//console.log(hasValue);
if (!hasValue) {
return false;
}
else {
return true;
}
},
/*invert
for the passed object, reverse the key = value to value = key
*/
invert(object){
const invertedObject = {};
for (var item in object) {
let originalValue = object[item];
invertedObject[originalValue] = item;
};
return invertedObject;
},
/*findKey
iterates thro each key in the object and returns the first ket that evaluates to truthy
*/
findKey(object, predicate) {
for (var itemKey in object) {
const value = object[itemKey];
const predicateReturnValue = predicate(value);
if (predicateReturnValue) {
return itemKey;
};
};
return undefined;
},
/*drop
drops numToDrop items from the front of the passed array
*/
drop(array,numToDrop) {
if (!numToDrop) {
numToDrop = 1;
};
let droppedArray = array.slice(numToDrop);
return droppedArray;
},
/*dropWhile
returns a new array with items dropped from the front of the array where
the predicate function returned true
*/
dropWhile(array, predicate) {
const dropNumber = array.findIndex(function(element, index) {
return (!predicate(element, index, array)); })
const droppedArray = this.drop(array,dropNumber);
return droppedArray;
},
/*chunk
returns a new array chunked into size items.
*/
chunk(array, size) {
if (!size) {
size = 1;
};
let arrayChunks = [];
for (var counter = 0;counter < array.length ;counter = counter + size) {
arrayChunk = array.slice(counter, counter+size);
arrayChunks.push(arrayChunk);
};
return arrayChunks;
}
};
// Do not write or modify code below this line.
module.exports = _;