Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function trim(s) {
- return typeof s.trim != 'undefined' ? s.trim() : s.replace(/^\s+|\s+$/g, '');
- }
- function ltrim(s) {
- return typeof s.ltrim != 'undefined' ? s.ltrim() : s.replace(/^\s+/, '');
- }
- function rtrim(s) {
- return typeof s.rtrim != 'undefined' ? s.rtrim() : s.replace(/\s+$/, '');
- }
- function wrap(s, w, br) {
- w = w || 80;
- if (s.length <= w) {
- return s;
- }
- br = br || '\n';
- // /(.{1,80})[\t\r\x20]+|(.{1,80})/g
- var re = new RegExp('(.{1,' + w + '})[\\t\\r\\x20]+|(.{1,' + w + '})', 'g');
- s = s.split('\n');
- for (var i = 0; i < s.length; ++i) {
- if (s[i].length > w) {
- s[i] = s[i].match(re).map(function (v) {
- return rtrim(v);
- }).join(br);
- }
- }
- return s.join('\n');
- }
- function startsWith(haystack, needle) {
- return needle == haystack.substr(0, needle.length);
- }
- function endsWith(haystack, needle) {
- return needle == haystack.substr(-needle.length);
- }
- function containsStr(haystack, needle) {
- return haystack.indexOf(needle) >= 0;
- }
- function randStr(len, chars) {
- var ret = '';
- chars = chars || '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM';
- while (ret.length < len) {
- ret += chars.charAt(Math.random() * chars.length);
- }
- return ret;
- }
- function ucfirst(s) {
- return s.charAt(0).toUpperCase() + s.substr(1);
- }
- function capitalize(s) {
- return s.replace(/\b[a-z]/g, function (c) {
- return c.toUpperCase();
- });
- }
- function reverseStr(s) {
- return s.split('').reverse().join('');
- }
- function repeatStr(s, n) {
- return Array(++n).join(s);
- }
- function lpad(s, l, c) {
- if (s.length >= l) return s;
- c = c || ' ';
- while (s.length < l) {
- s = c + s;
- }
- return s.length > l ? s.substr(0, l) : s;
- }
- function rpad(s, l, c) {
- if (s.length >= l) return s;
- c = c || ' ';
- while (s.length < l) {
- s += c;
- }
- return s.length > l ? s.substr(0, l) : s;
- }
- function center(s, l, c) {
- if (s.length >= l) return s;
- c = c || ' ';
- l -= s.length;
- l /= 2;
- var repeater = repeatStr(c, Math.ceil(l / c.length));
- return repeater.substr(0, Math.floor(l)) + s + repeater.substr(0, Math.ceil(l))
- }
- function escapeRe(s) {
- return s.replace(/([-.\\+*?[^\]$(){}=!<>|:])/g, '\\$1');
- }
- function addSlashes(s) {
- return s.replace(/([\\'"])/g, '\\$1');
- }
- function stripSlashes(s) {
- return s.replace(/\\([\\'"])/g, '$1');
- }
- function encodeHtml(s) {
- return s.replace(/[<>&"']/g, function (c) {
- return {
- '<': '<',
- '>': '>',
- '&': '&',
- '"': '"',
- "'": '''
- }[c];
- });
- }
- function decodeHtml(s) {
- var el = document.createElement('div');
- s = s.replace('<', '<');
- s = s.replace('>', '>');
- el.innerHTML = s;
- return el.textContent;
- }
- function encodeEntities(s) {
- var r = '';
- for (var i = 0; i < s.length; ++i) {
- r += '&#' + s.charCodeAt(i) + ';';
- }
- return r;
- }
- function stripTags(s) {
- return s.replace(/<[\s\S]+?>/g, '');
- }
- function template(s, o) {
- return s.replace(/\{(.*?)\}/g, function (m, v) {
- var ks = v.split('.'), r = o;
- for (var i = 0; i < ks.length; ++i) {
- r = r[ks[i]];
- }
- return r;
- });
- }
- function format(s) {
- var arg = toArray(arguments).slice(1);
- return s.replace(/\{(\d+)\}/g, function (match, index) {
- if (index >= arg.length) {
- throw new Error('Index out of range');
- }
- return arg[index];
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement