-
Notifications
You must be signed in to change notification settings - Fork 0
/
_.js
156 lines (132 loc) · 4.69 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
const _ = {
clamp(number, lower, upper){
// if(number < lower){
// return lower;
// }else if(number > upper){
// return upper;
// }
// return number;
//alt implementation
const maxNumber = Math.max(number, lower);
return Math.min(maxNumber, upper);
},
/**
* checks to see if the provided number falls
* within the range specified by the start and end values
*/
inRange(number, start, end){
// If no end value is provided to the method, the start value will be 0 and the end value will be the provided start value
if(end === undefined){
// end = start;
// start = 0;
[end, start] = [start, 0]
}
// If the provided start value is larger than the provided end value, the two values should be swapped
if(start > end){
[start,end] = [end,start]
}
// If the provided number is within the start and end values, .inRange() will return true
// If the provided number is smaller than the start value, .inRange() will return false
// If the provided number is larger than or equal to the end value, .inRange() will return false
return number >= start && number < end;
},
/**
* Splits string into an array of its words.
*/
words(string){
return string.split(' ');
},
/**
* Pads string with witspaces
*/
pad(string, length){
//.pad() takes two arguments: a string and a length
//if string.length is equeal to length parameter, then return string arg without modification
// .pad() adds spaces evenly to both sides of the string to make it reach the desired length
// Extra padding is added to the end of the string if an odd amount of padding is required to reach the specified length
if(string.length === length || string.length > length ){
return string;
}
const padLength = length-string.length
const padStartSize = Math.floor(padLength / 2)
const padEndSize = padLength - padStartSize;
const paddedString = ' '.repeat(padStartSize) + string + ' '.repeat(padEndSize);
return paddedString;
},
/**
* Checks if object has any value at passed key
*/
has(object, property){
return object[property] !== undefined;
},
/**
* Swap object properties and its values
*/
invert(object){
const invertedOject = {};
for(const property in object){
invertedOject[object[property]] = property;
}
return invertedOject;
},
/**
* Return object key for that predicate function returns true value
*/
findKey(object, func){
for(const property in object){
isTrue = func(object[property])
if(isTrue){
return property;
}
}
return undefined;
},
/**
* Creates a slice of array with n elements dropped from the beginning.
* @param {*} array
* @param {*} number
* @returns
*/
drop(array, number=1){
// .drop() takes two arguments: an array and a number representing the number
// of items to drop from the beginning of the array
// .drop() returns a new array which contains the elements
//from the original array, excluding the specified number of elements
//from the beginning of the array
// If the number of elements to drop is unspecified,
// your method should drop one element
return array.slice(number);
},
/**
* Creates a slice of array excluding elements dropped from the beginning.
* Elements are dropped until predicate returns falsey.
* The predicate is invoked with three arguments: (value, index, array).
*
* @param {*} array
* @param {*} predicate
*/
dropWhile(array, predicate){
const dropNumber = array.findIndex((number, index)=>{
return !predicate(number, index, array)
});
const droppedArray = this.drop(array, dropNumber)
return droppedArray;
},
/**
*
* Creates an array of elements split into groups the length of size.
* If array can't be split evenly,
* the final chunk will be the remaining elements.
*
*/
chunk(array, sizeOfChunk=1){
const arrayChunks = []
for(let i=0;i < array.length;i+=sizeOfChunk){
const arrayChunk=array.slice(i, i+sizeOfChunk);
arrayChunks.push(arrayChunk);
}
return arrayChunks;
}
}
// Do not write or modify code below this line.
module.exports = _;