Advertisement
Mirineo

Untitled

Jul 3rd, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function Extend() {
  2.  
  3.     String.prototype.ensureStart = function (str) {
  4.         let startsWith = true;
  5.         for (let i = 0; i < str.length; i++) {
  6.             startsWith = str[i] === this[i];
  7.         }
  8.  
  9.         return startsWith ? '' + this : str + this;
  10.     };
  11.  
  12.     String.prototype.ensureEnd = function (str) {
  13.         let endsWith = true;
  14.         for (let i = 0; i < str.length; i++) {
  15.             endsWith = str[str.length - 1 - i] === this[this.length - 1 - i];
  16.         }
  17.  
  18.         return endsWith ? '' + this : this + str;
  19.     };
  20.    
  21.     String.prototype.isEmpty = function () {
  22.         return str === '';
  23.     };
  24.  
  25.     String.prototype.truncate = function (n) {
  26.         if (this.length <= n){
  27.             return this + "";
  28.         }else {
  29.             if (!/\s+/.test(this)){
  30.                 if (n < 4){
  31.                     return ".".repeat(n);
  32.                 }
  33.                 return this.slice(0, n - 3) + "...";
  34.             }else {
  35.                 if (n < 4) {
  36.                     return ".".repeat(n);
  37.                 }
  38.                 return this.slice(0, n - 3) + "...";
  39.             }
  40.         }
  41.     };
  42.  
  43.     String.prototype.format = function (str1, str2, str3) {
  44.         if (str2 !== '' || str2 !== undefined){
  45.             str1.replace('{0}', str2);
  46.         }
  47.  
  48.         if (str3 !== '' || str3 !== undefined){
  49.             str1.replace('{1}', str3);
  50.         }
  51.  
  52.         return str1;
  53.     }
  54. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement