-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy path_.js
89 lines (78 loc) · 2.09 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
const _ = {
clamp(number, lower, upper) {
const lowerClampedValue = Math.max(number, lower);
const clampedValue = Math.min(lowerClampedValue, upper);
return clampedValue;
},
inRange(number, start, end) {
if (end == null) {
end = start;
start = 0;
} else if (start > end) {
const temp = end
end = start
start = temp
};
const isInRange = Boolean(start<=number && number<end);
return isInRange;
},
words(string) {
const words = string.split(' ');
return words;
},
pad(string, length) {
if (string.length >= 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] !== undefined;
return hasValue;
},
invert(object) {
let invertedObject = {};
for (let key in object) {
const originalValue = key;
invertedObject = {originalValue: key}
}
return invertedObject
},
findKey(object, predicate) {
for (let key in object) {
const value = object[key];
const predicateReturnValue = predicate(value);
if (predicateReturnValue === true) {
return key;
}
} return undefined;
},
drop(array, number) {
if (number == null) {
number = 1;
};
const droppedArray = array.slice(number);
return droppedArray;
},
dropWhile(array, predicate) {
const dropNumber = array.findIndex((element,index) => {return !predicate(element,index,array)});
const droppedArray = this.drop(array, dropNumber);
return droppedArray;
},
chunk(array,size) {
if (size == null) {
size = 1;
};
const arrayChunks = [];
for (let i =0; i<array.length; i+size) {
const arrayChunk = array.slice(i);
arrayChunks.push(arrayChunk);
};
return arrayChunk;
}
};
// Do not write or modify code below this line.
module.exports = _;