-
Notifications
You must be signed in to change notification settings - Fork 0
/
Re-creating the Lodash Library.js
87 lines (86 loc) · 2.37 KB
/
Re-creating the Lodash Library.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
const _ = {
clamp (number, lower, upper) {
var lowerClampedValue = Math.max(number,lower);
var 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;
}
const isInRange = (start<=number && number<end)
return isInRange
},
words (string) {
var 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];
if (hasValue != undefined) {
return true;
}
return false;
},
invert (object) {
var invertedObject = {}
for (key in object) {
var originalValue = object[key]
invertedObject[originalValue] = key
}
return invertedObject
},
findKey (object, predicate) {
for (key in object) {
var value = object[key]
var predicateReturnValue = predicate(value)
if (predicateReturnValue) {
return key
}
}
return undefined
},
drop (array, n) {
if (n === undefined) {
n = 1
}
var 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) {
if (size === undefined) {
size = 1
}
var arrayChunks = []
for (let i = 0; i < array.length; i += size) {
var arrayChunk = array.slice(i,i+size)
arrayChunks.push(arrayChunk)
}
return arrayChunks
}
};
// Do not write or modify code below this line.
module.exports = _;