-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.js
154 lines (135 loc) · 4.15 KB
/
String.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
function sortObject(o) {
var sorted = {},
key, a = [];
for (key in o) {
if (o.hasOwnProperty(key)) {
a.push(key);
}
}
a.sort();
for (key = 0; key < a.length; key++) {
sorted[a[key]] = o[a[key]];
}
return sorted;
}
function prs(Input) {
return JSON.parse(Input);
};
function str(Input) {
return JSON.stringify(Input);
};
// "Stringifies" or "Parses" any char that could mean something in a RegExp string.
// Params.regexConvert("String");
// Params.regexConvert("Parse");
String.prototype.regexConvert = function(Type) {
var rep = this;
if (Type.toLowerCase() === "string") {
rep = rep.replace(/(\[)/g, "\\\[");
rep = rep.replace(/(\])/g, "\\\]");
rep = rep.replace(/(\{)/g, "\\\{");
rep = rep.replace(/(\})/g, "\\\}");
rep = rep.replace(/(\.)/g, "\\\.");
rep = rep.replace(/(\*)/g, "\\\*");
rep = rep.replace(/(\+)/g, "\\\+");
rep = rep.replace(/(\?)/g, "\\\?");
rep = rep.replace(/(\/)/g, "\\\/");
} else if (Type.toLowerCase() === "parse") {
rep = rep.replace(/(\\\[)/g, "\[");
rep = rep.replace(/(\\\])/g, "\]");
rep = rep.replace(/(\\\{)/g, "\{");
rep = rep.replace(/(\\\})/g, "\}");
rep = rep.replace(/(\\\.)/g, "\.");
rep = rep.replace(/(\\\*)/g, "\*");
rep = rep.replace(/(\\\+)/g, "\+");
rep = rep.replace(/(\\\?)/g, "\?");
rep = rep.replace(/(\\\/)/g, "\/");
}
return rep;
}
String.prototype.noSpace = function() {
return this.replace(/\s/g, "");
}
//All prototypes Echo's package used for JS does not have.
if (!String.prototype.includes) {
String.prototype.includes = function(search, start) {
"use strict";
if (typeof start !== "number") {
start = 0;
}
if (start + search.length > this.length) {
return false;
} else {
return this.indexOf(search, start) !== -1;
}
};
}
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(search, this_len) {
if (this_len === undefined || this_len > this.length) {
this_len = this.length;
}
return this.substring(this_len - search.length, this_len) === search;
};
}
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(search, pos) {
return this.substr(!pos || pos < 0 ? 0 : +pos, search.length) === search;
};
}
if (!String.prototype.repeat) {
String.prototype.repeat = function(count) {
"use strict";
if (this == null) {
throw new TypeError("can\"t convert " + this + " to object");
}
var str = "" + this;
count = +count;
if (count != count) {
count = 0;
}
if (count < 0) {
throw new RangeError("repeat count must be non-negative");
}
if (count == Infinity) {
throw new RangeError("repeat count must be less than infinity");
}
count = Math.floor(count);
if (str.length == 0 || count == 0) {
return "";
}
// Ensuring count is a 31-bit integer allows us to heavily optimize the
// main part. But anyway, most current (August 2014) browsers can"t handle
// strings 1 << 28 chars or longer, so:
if (str.length * count >= 1 << 28) {
throw new RangeError("repeat count must not overflow maximum string size");
}
var rpt = "";
for (var i = 0; i < count; i++) {
rpt += str;
}
return rpt;
}
}
// Following prototypes are used for markdown in Discord™.
String.prototype.bold = function() {
return ("**" + this + "**")
}
String.prototype.italic = function() {
return ("*" + this + "*")
}
String.prototype.underline = function() {
return ("__" + this + "__")
}
String.prototype.strikethrough = function() {
return ("~~" + this + "~~")
}
String.prototype.smallCodeblock = function() {
return ("`" + this + "`")
}
String.prototype.bigCodeblock = function(Language) {
if (Object.keys(arguments).length === 1) {
return ("```" + Language + "\n" + this + "```");
} else {
return ("```\n" + this + "```");
}
}