Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function (win) {
- 'using strict';
- String.PAD_LEFT = 1;
- String.PAD_RIGHT = 2;
- String.PAD_BOTH = 3;
- var proto = String.prototype;
- proto.template = function (o) {
- return this.replace(/\{(.*?)\}/g, function (s, m) {
- var ks = m.split('.'),
- ret = o;
- for (var i = 0; i < ks.length; ++i)
- ret = ret[ks[i]];
- return ret;
- });
- };
- proto.startsWith = function (s) {
- return s == this.substr(0, s.length);
- };
- proto.endsWith = function (s) {
- return s == this.substr(-s.length);
- };
- proto.contains = function (s) {
- return this.indexOf(s) >= 0;
- };
- proto.reverse = function () {
- return this.split('').reverse().join('');
- };
- proto.repeat = function (n) {
- return Array(++n).join(this);
- };
- proto.pad = function (l, s, t) {
- var r = this;
- s = null == s ? ' ' : (s += '');
- t = (t & String.PAD_BOTH) || String.PAD_RIGHT;
- if (s.length && r.length < l) {
- l -= r.length;
- if (t == String.PAD_BOTH)
- l /= 2;
- s = s.repeat(Math.ceil(l / s.length));
- if (t & String.PAD_LEFT)
- r = s.substr(0, (t == String.PAD_BOTH) ? Math.floor(l) : l) + r;
- if (t & String.PAD_RIGHT)
- r += s.substr(0, (t == String.PAD_BOTH) ? Math.ceil(l) : l);
- }
- return r;
- };
- proto.padLeft = function (l, s) {
- return this.pad(l, s, String.PAD_LEFT);
- };
- proto.padRight = function (l, s) {
- return this.pad(l, s, String.PAD_RIGHT);
- };
- proto.padBoth = function (l, s) {
- return this.pad(l, s, String.PAD_BOTH);
- };
- proto.wrap = function (l, br) {
- var lines, i, j, match, indent, words;
- l = null == l ? 78 : l;
- br = null == br ? '\n' : br;
- lines = this.split(/\n/);
- for (i = 0; i < lines.length; ++i) {
- match = lines[i].match(/^[ \t]+/);
- indent = null == match ? '' : match[0];
- words = lines[i].words();
- for (j = 0; j < words.length; ++j) {
- //
- }
- lines[i] = indent + $.join(br);
- }
- return lines.join('\n');
- };
- proto.words = function () {
- var s = this;
- return (s = s.trim().replace(/\s+/g, ' ')) ? s.split(' ') : [];
- };
- proto.addSlashes = function () {
- return this.replace(/([\\'"])/g, '\\$1');
- };
- proto.stripSlashes = function () {
- return this.replace(/\\([\\'"])/g, '$1');
- };
- proto.encodeHTML = function () {
- return this.replace(/[<>&"']/g, function (u) {
- return {
- '<': '<',
- '>': '>',
- '&': '&',
- '"': '"'
- }[u];
- });
- };
- proto.decodeHTML = function () {
- var el = document.createElement('div');
- el.innerHTML = this.replace('<', '<').replace('>', '>');
- return el.textContent;
- };
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement