Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String.prototype.trim = function(){ this.replace( /^s+|s+$/g, '' ); }
- function sign(x) {
- return (x > 0) - (x < 0);
- }
- function isIE() {
- return 'v' == 'v';
- }
- function chain(fn) {
- return function() {
- return fn.apply(this, arguments) || this;
- }
- }
- CanvasRenderingContext2D.prototype.fillRect =
- chain(CanvasRenderingContext2D.prototype.fillRect);
- //...
- ctx.fillRect(0, 0, 10, 10).fillRect(90, 90, 10, 10).fillRect(0, 10, 100, 10);
- readableNumberLimiter = function (number,limiter)
- {
- return number.toString().replace(
- new RegExp(
- "(^\d{"+(number.toString().length%3||-1)+"})(?=\d{3})"),
- "$1"+limiter
- ).replace(/(d{3})(?=d)/g,"$1"+limiter);
- }
- readableNumberLimiter(12345678,",");
- "12,345,678"
- readableNumberLimiter(12345678,".");
- "12.345.678"
- readableNumberLimiter(12345678," ");
- "12 345 678"
- function clone(obj) {
- return typeof obj === 'undefined' ?
- this : (clone.prototype = Object(obj), new clone);
- }
- function addCallback(oldFunc, newFunc) {
- oldFunc = oldFunc || function () {};
- return function () {oldFunc(); newFunc();};
- }
- document.onload = addCallback(window.onLoad, function () {alert("me too!"); });
- document.onload = addCallback(window.onLoad, function () {alert("me three!"); });
- function chain(f1, f2) {
- return typeof f1 !== 'function' ? f2 : function() {
- var r1 = f1.apply(this, arguments),
- r2 = f2.apply(this, arguments);
- return typeof r1 === 'undefined' ? r2 : (r1 && r2);
- };
- }
- Date.prototype.daysInMonth = function ()
- {
- var x = 31;
- switch (this.getMonth()) //zero-index
- {
- case 1: x = (this.isLeapYear()? 29 : 28);break;
- case 3:
- case 5:
- case 8:
- case 10: x = 30;
- }
- return x;
- };
- Date.prototype.isLeapYear = function ()
- {
- var y = this.getFullYear();
- return (y%400==0 || (y%100!=0 && y%4==0));
- };
- Date.prototype.unbox = function ()
- {
- return new Date(this);
- }
- String.prototype.reverse = function ()
- {
- return this.split('').reverse().join('');
- };
- function coalesce() {
- for (var i=0; i<arguments.length; i++) {
- if (arguments[i] !== null) {
- return arguments[i];
- }
- }
- return null;
- }
- function IsDefined(variable)
- {
- return !(typeof(window[variable]) == 'undefined');
- }
- /**
- * Left-pad a number with zeros so that it's at least the given
- * number of characters long
- * @param n The number to pad
- * @param len The desired length
- */
- function leftPad(n, len) {
- return (new Array(len - String(n).length + 1)).join("0").concat(n);
- }
- alert(leftPad(42, 6)); // Gives "000042"
- //+ Carlos R. L. Rodrigues
- //@ http://jsfromhell.com/array/chunk [rev. #1]
- function chunk(a, s){
- for(var x, i = 0, c = -1, l = a.length, n = []; i < l; i++)
- (x = i % s) ? n[c][x] = a[i] : n[++c] = [a[i]];
- return n;
- }
- function formatDate(date, fmt) {
- return fmt.replace(
- /{([^}:]+)(?::(d+))?}/g,
- function (s, comp, pad) {
- var fn = date["get" + comp];
- if (fn) {
- var v = (fn.call(date) +
- (/Month$/.test(comp) ? 1 : 0)).toString();
- return pad && (pad = pad - v.length)
- ? new Array(pad + 1).join("0") + v
- : v;
- } else {
- return s;
- }
- });
- };
- var now = new Date();
- alert(formatDate(now, "This is the year {FullYear}"));
- // Output: This is the year 2008
- alert(formatDate(now, "{FullYear}-{Month:2}-{Date:2}"));
- // Output: 2008-09-02
- alert(formatDate(now, "{Hours:2}:{Minutes:2}:{Seconds:2}.{Milliseconds:3}"));
- // Output: 15:47:32.156
- alert(formatDate(now, "{Hourz} hours {Minutes} minutes"));
- // Output: {Hourz} hours 51 minutes
- alert(formatDate(now, "{FullYear:}));
- // Output: {FullYear:}
- function typeOf(value) {
- var type = typeof value;
- return type === 'object' ? (value === null ? 'null' :
- Object.prototype.toString.call(value).match(/^[object (.*)]$/)[1]) :
- type === 'function' ? 'Function' : type;
- }
- function secondstominutes(secs)
- {
- return ((arguments[1]=(Math.floor(secs/60)))<10?"0":"")+arguments[1]+":"+((arguments[2]=secs%60)<10?"0":"") + arguments[2];
- }
- function sigFigs(n, sig) {
- var mult = Math.pow(10,
- sig - Math.floor(Math.log(n) / Math.LN10) - 1);
- return Math.round(n * mult) / mult;
- }
- alert(sigFigs(1234567, 3)); // Gives 1230000
- alert(sigFigs(0.06805, 3)); // Gives 0.0681
- alert(sigFigs(5, 3)); // Gives 5
- String.prototype.replaceAll = function(target, replacement) {
- return this.replace(new RegExp(target, "g"), replacement);
- };
- String.prototype.capitalize = function() {
- return this.charAt(0).toUpperCase() + this.substring(1).toLowerCase();
- };
- Array.prototype.contains = function(element) {
- var i = this.length;
- while (i--) {
- if (this[i] === element) {
- return true;
- }
- }
- return false;
- };
- Array.prototype.clear = function() {
- this.length = 0;
- };
- //+ Jonas Raoni Soares Silva
- //@ http://jsfromhell.com/array/rotate [rev. #2]
- rotate = function(a, p){
- for(var l = a.length, p = (Math.abs(p) >= l && (p %= l), p < 0 && (p += l), p), i, x; p; p = (Math.ceil(l / p) - 1) * p - l + (l = p))
- for(i = l; i > p; x = a[--i], a[i] = a[i - p], a[i - p] = x);
- return a;
- };
- function htmlEscape(s) {
- return s.replace(/([&<>])/g,
- function (c) {
- return "&" + {
- "&": "amp",
- "<": "lt",
- ">": "gt"
- }[c] + ";";
- });
- }
- function removeQueryStringParam(queryString, param) {
- return queryString
- .replace(
- new RegExp("(&?)(?:"
- + (param.join ? param.join("|") : param)
- + ")=[^&]*&?", "g"),
- "$1")
- .replace(/([^&?]*)(?:&|?)$/, "$1");
- }
- removeQueryStringParam("foo=1¶m2=2&bar=3¶m4=4", [ "foo", "bar" ]));
- // Results in "param2=2¶m4=4"
- var is_array = function (value) {
- return value &&
- typeof value === 'object' &&
- typeof value.length === 'number' &&
- typeof value.splice === 'function' &&
- !(value.propertyIsEnumerable('length'));
- };
- Object.prototype.toString.call(obj) === '[object Array]'
- typeof obj.length === 'number'
- String.prototype.strip_tags = function() {
- return this.replace(/<[^>]+>/gi,"");
- }
- function getElementsByClassName(name, parent){
- for(var o = [], n = new RegExp("\b" + name.replace(/([(){}|*+?.,^$[]\])/g, "\$1") + "\b"), l = (parent || document).getElementsByTagName("*"), i = l.length; i--;)
- n.test(l[i].className) && (o[o.length] = l[i]);
- return o;
- }
- Array.prototype.lpush = function(item) {
- this.unshift(item); return this;
- }
- while (cycle)
- var foo = arr.lpush(foo).pop();
- for(var offsetLeft, offsetRight = start % 8, curByte = this.buffer.length - (start >> 3) - 1, lastByte = this.buffer.length + (-(start + length) >> 3), diff = curByte - lastByte, sum = ((this.buffer[ curByte ] >> offsetRight) & ((1 << (diff ? 8 - offsetRight : length)) - 1)) + (diff && (offsetLeft = (start + length) % 8) ? (this.buffer[ lastByte++ ] & ((1 << offsetLeft) - 1)) << (diff-- << 3) - offsetRight : 0); diff; sum += shl(this.buffer[lastByte++ ], (diff-- << 3) - offsetRight);
- (!k || k == 8 ? 1 : (r = /^(.)^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ? r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0] : (l = (f.value += m.substr(l, (r = /[A|9|C|*]/i.exec(m.substr(l))) ? r.index : l)).length) < m.length && accept(c, m.charAt(l))) || e.preventDefault();
- return function(x, y, x0, y0){return Math.sqrt((x -= x0) * x + (y -= y0) * y);}(px, py, x, y) > ray ? {x: Math.cos(tg = Math.atan2(py - y, px - x)) * ray + x, y: Math.sin(tg) * ray + y} : {x: px, y: py};
- // Javascript modulus operator always returning a positive number
- Number.prototype.mod = function(n) {
- return ((this%n)+n)%n;
- }
- var num = -2;
- alert(num % 10); // will output -2
- var num = -2;
- alert(num.mod(10)); // will output 8
- function dateOK(Y, M, D) {
- return D > 0 &&
- (D <= [, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][M] ||
- D == 29 && M == 2 && Y % 4 == 0 && (Y % 100 > 0 || Y % 400 == 0));
- }
- String.prototype.repeat = function(n){
- n=parseInt(n,10)+1;
- return n ? Array(n).join(this) : this.toString();
- };
- String.prototype.repeat = function(n){
- return n && n.constructor === Number ? Array(n+1).join(this) : this;
- }
- //Example:
- alert(('-').repeat(10)); //=> '----------'
- Number.prototype.between = function()
- { return this>arguments[0] && this<arguments[1]; };
- //example
- alert((23).between(10,30)); //=> true
- alert((23).between(33,40)); //=> false
- function extractFloat() {
- var x = arguments[0].split(/,|./),
- x2 = x.join('').replace(new RegExp(x[x.length-1]+'$'),'.'+x[x.length-1]);
- return parseFloat(x2);
- }
- //example:
- extractFloat('1,239.4'); //=> 1239.4
- extractFloat('1.239,4'); //=> 1239.4
- extractFloat('a1.239,b4'); //=> NaN
- String.prototype.ellipses = function(len) {
- return (this.length > len) ? this.substr(0, len) + "..." : this;
- }
- var isMSIE = /*@cc_on!@*/false;
Add Comment
Please, Sign In to add comment