SHOW:
|
|
- or go back to the newest paste.
| 1 | (() => {
| |
| 2 | ||
| 3 | String.prototype.ensureStart = function(str = '') {
| |
| 4 | if (!this.startsWith(str)) {
| |
| 5 | return str + this; | |
| 6 | } | |
| 7 | ||
| 8 | return this.toString(); | |
| 9 | } | |
| 10 | ||
| 11 | String.prototype.ensureEnd = function(str = '') {
| |
| 12 | if (!this.endsWith(str)) {
| |
| 13 | return this + str; | |
| 14 | } | |
| 15 | ||
| 16 | return this.toString(); | |
| 17 | } | |
| 18 | ||
| 19 | String.prototype.isEmpty = function() {
| |
| 20 | return this.length == 0; | |
| 21 | } | |
| 22 | ||
| 23 | ||
| 24 | String.prototype.truncate = function(n) {
| |
| 25 | if (n < 4) {
| |
| 26 | return '.'.repeat(n); | |
| 27 | } | |
| 28 | ||
| 29 | if (this.length < n) {
| |
| 30 | return this.toString; | |
| 31 | } | |
| 32 | ||
| 33 | let foundedSpace = this.lastIndexOf(' ');
| |
| 34 | ||
| 35 | if (foundedSpace != -1) {
| |
| 36 | return str.substring(0,foundedSpace) + '...'; | |
| 37 | } else {
| |
| 38 | return str.substring(0, n - 3 ) + '...'; | |
| 39 | } | |
| 40 | } | |
| 41 | ||
| 42 | String.format = function(str, ...params) {
| |
| 43 | params.forEach((key, index) => {
| |
| 44 | str = str.replace(`{${index}}`, key);
| |
| 45 | }) | |
| 46 | } | |
| 47 | ||
| 48 | })(); |