-
Notifications
You must be signed in to change notification settings - Fork 0
/
extensions.js
44 lines (36 loc) · 1.04 KB
/
extensions.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
String.prototype.truncate = function(n, useWordBoundary = true) {
if (this.length <= n) {
return this;
}
let subString = this.substr(0, n - 1);
return (useWordBoundary ? subString.substr(0, subString.lastIndexOf(' ')) : subString) + "...";
};
String.prototype.ucfirst = function() {
return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase();
};
String.prototype.trimChar = function(char) {
let newString = this;
while (this.charAt(0) === char) {
newString = newString.substring(1);
}
while (this.charAt(newString.length - 1) === char) {
newString = newString.substring(0, newString.length - 1);
}
return newString;
};
Array.prototype.contains = function(...items) {
for (let item of items) {
if (this.indexOf(item) >= 0) {
return true;
}
}
return false;
};
String.prototype.contains = function(...items) {
for (let item of items) {
if (this.indexOf(item) >= 0) {
return true;
}
}
return false;
};