-
Notifications
You must be signed in to change notification settings - Fork 1
/
_.js
99 lines (83 loc) · 2.26 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
const _ ={
clamp(number, lower, upper){
var lowerClampedValue = Math.max(number, lower);
var clamedValue = Math.min(lowerClampedValue, upper);
return clamedValue;
},
inRange(number, start, end){
if(end == undefined){
end = start
start = 0
}
if(start > end){
var temp = end
end = start
start = temp
}
var isInRange = start <= number && number < end
return isInRange
} ,
words(string){
var words = string.split(' ');
return words;
} ,
pad(string, length){
if(string.lengrh >= length){
return string;
};
const startPaddingLength = Math.floor((length - string.length) / 2);
const endPaddingLength = length - string.length - startPaddingLength;
const paddedString = ' '.repeat(startPaddingLength) + string + ' '.repeat(endPaddingLength);
return paddedString;
},
has(object, key){
const hasValue = object[key];
if(hasValue != true){
return true;
} return false;
},
invert(object){
let invertedObject = {};
for(let key in object){
const originalValue = object[key];
invertedObject = {originalValue: key}
}
return invertedObject;
},
findKey(object, predicate){
for(let key in object){
let value = object[key];
let predicateReturnValue = predicate(value);
if(predicateReturnValue){
return key;
};
};
undefined
return undefined;
},
drop(array, n){
if(n === undefined){
n = 1;
}
let droppedArray = array.slice(n, array.length);
return droppedArray;
},
dropWhile(array, predicate){
const cb = (element, index) => {
return !predicate(element, index, array);
};
let dropNumber = array.findIndex(cb);
let droppedArray = this.drop(array, dropNumber);
return droppedArray;
} ,
chunk(array, 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;
}
};
// Do not write or modify code below this line.
module.exports = _;