Advertisement
stuppid_bot

Extends String

Sep 1st, 2014
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function (win) {
  2.     'using strict';
  3.     String.PAD_LEFT = 1;
  4.     String.PAD_RIGHT = 2;
  5.     String.PAD_BOTH = 3;
  6.  
  7.     var proto = String.prototype;
  8.  
  9.     proto.template = function (o) {
  10.         return this.replace(/\{(.*?)\}/g, function (s, m) {
  11.             var ks = m.split('.'),
  12.                 ret = o;
  13.             for (var i = 0; i < ks.length; ++i)
  14.                 ret = ret[ks[i]];
  15.             return ret;
  16.         });
  17.     };
  18.  
  19.     proto.startsWith = function (s) {
  20.         return s == this.substr(0, s.length);
  21.     };
  22.  
  23.     proto.endsWith = function (s) {
  24.         return s == this.substr(-s.length);
  25.     };
  26.  
  27.     proto.contains = function (s) {
  28.         return this.indexOf(s) >= 0;
  29.     };
  30.  
  31.     proto.reverse = function () {
  32.         return this.split('').reverse().join('');
  33.     };
  34.  
  35.     proto.repeat = function (n) {
  36.         return Array(++n).join(this);
  37.     };
  38.  
  39.     proto.pad = function (l, s, t) {
  40.         var r = this;
  41.         s = null == s ? ' ' : (s += '');
  42.         t = (t & String.PAD_BOTH) || String.PAD_RIGHT;
  43.         if (s.length && r.length < l) {
  44.             l -= r.length;
  45.             if (t == String.PAD_BOTH)
  46.                 l /= 2;
  47.             s = s.repeat(Math.ceil(l / s.length));
  48.             if (t & String.PAD_LEFT)
  49.                 r = s.substr(0, (t == String.PAD_BOTH) ? Math.floor(l) : l) + r;
  50.             if (t & String.PAD_RIGHT)
  51.                 r += s.substr(0, (t == String.PAD_BOTH) ? Math.ceil(l) : l);
  52.         }
  53.         return r;
  54.     };
  55.  
  56.     proto.padLeft = function (l, s) {
  57.         return this.pad(l, s, String.PAD_LEFT);
  58.     };
  59.  
  60.     proto.padRight = function (l, s) {
  61.         return this.pad(l, s, String.PAD_RIGHT);
  62.     };
  63.  
  64.     proto.padBoth = function (l, s) {
  65.         return this.pad(l, s, String.PAD_BOTH);
  66.     };
  67.  
  68.     proto.wrap = function (l, br) {
  69.         var lines, i, j, match, indent, words;
  70.         l = null == l ? 78 : l;
  71.         br = null == br ? '\n' : br;
  72.         lines = this.split(/\n/);
  73.         for (i = 0; i < lines.length; ++i) {
  74.             match = lines[i].match(/^[ \t]+/);
  75.             indent = null == match ? '' : match[0];
  76.             words = lines[i].words();
  77.             for (j = 0; j < words.length; ++j) {
  78.                 //
  79.             }
  80.             lines[i] = indent + $.join(br);
  81.         }
  82.         return lines.join('\n');
  83.     };
  84.  
  85.     proto.words = function () {
  86.         var s = this;
  87.         return (s = s.trim().replace(/\s+/g, ' ')) ? s.split(' ') : [];
  88.     };
  89.    
  90.     proto.addSlashes = function () {
  91.         return this.replace(/([\\'"])/g, '\\$1');
  92.     };
  93.  
  94.     proto.stripSlashes = function () {
  95.         return this.replace(/\\([\\'"])/g, '$1');
  96.     };
  97.  
  98.     proto.encodeHTML = function () {
  99.         return this.replace(/[<>&"']/g, function (u) {
  100.             return  {
  101.                 '<': '&lt;',
  102.                 '>': '&gt;',
  103.                 '&': '&amp;',
  104.                 '"': '&quot;'
  105.             }[u];
  106.         });
  107.     };
  108.  
  109.     proto.decodeHTML = function () {
  110.         var el = document.createElement('div');
  111.         el.innerHTML = this.replace('<', '&lt;').replace('>', '&gt;');
  112.         return el.textContent;
  113.     };
  114. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement