This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
prototype_functions.js
133 lines (108 loc) · 2.64 KB
/
prototype_functions.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
/*************************
* AUXILIARY FUNCTIONS *
**************************/
//find the total times that a check passes. An example use would be to
//find the total number of online games in a game list, as such:
//games.total( function(game) {return game.isOnline;});
Object.total = function(testFn)
{
var total = 0;
for (var key in this)
{
if (testFn(this[key]) === true)
{
total++;
}
}
return total;
}
Array.prototype.forEachAsync = function(asyncFn, callback)
{
var index = 0;
//the context of 'this' will change in the loop
var array = this;
(function loop()
{
if (index >= array.length)
{
if (callback != null)
{
callback();
}
return;
}
asyncFn(array[index], index++, function()
{
loop();
});
})();
};
//Displays the items in an array of strings as a list, with each item
//separated by a linebreak and an identical total width
Array.prototype.listStrings = function()
{
var list = "";
this.forEach(function(string)
{
list += string + "\n";
});
return list;
};
//Displays the items in an array of strings as a numbered list, with each item
//separated by a linebreak and an identical total width
Array.prototype.numberedList = function(width, postIndexChar = ".")
{
var list = "";
this.forEach(function(string, index)
{
list += index + postIndexChar + " " + string.width(width) + "\n";
});
return list;
};
Number.isFloat = function(n)
{
return Number(n) === n && n % 1 !== 0;
};
//Sets a limit for a number; if it would go above it,
//it sets it at the limit
Number.prototype.cap = function(limit)
{
if (this > limit)
{
return limit;
}
else return this;
};
//Sets a lower limit for a number; if it would go below it,
//it sets it at the limit
Number.prototype.lowerCap = function(limit)
{
if (this < limit)
{
return limit;
}
else return this;
};
String.prototype.capitalize = function()
{
return this.charAt(0).toUpperCase() + this.slice(1);
};
//Spaces out evenly an item. If the width is set to 50, the entire string
//will have 50 characters, as long as the item is less than 50.
String.prototype.width = function (space, spaceFirst = false, spacingChar = " ")
{
var arrL = space - this.length + 1;
if (arrL < 1) arrL = 1;
if (spaceFirst) return Array(arrL).join(spacingChar) + this;
else return this + Array(arrL).join(spacingChar);
};
//``` ``` is the markup that Discord uses to send messages as a code box,
//this is just a short-hand way of doing that
String.prototype.toBox = function()
{
if (this !== "" && this != null && this.length && /\S+/.test(this))
{
return "```" + this + "```";
}
else return this;
};