Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Javascript prototypes.
- */
- /**
- * Array.remove() prototype.
- */
- if (!Array.prototype.remove)
- {
- Array.prototype.remove = function(value)
- {
- if (value == undefined) return false;
- var j = this.length;
- for (var i = 0; i < j; i++)
- {
- if (this[i] == value)
- {
- this.splice(i, 1);
- return true;
- }
- }
- return false;
- }
- }
- /**
- * Array.indexOf() prototype.
- */
- if (!Array.prototype.indexOf)
- {
- Array.prototype.indexOf = function(elm, i)
- {
- var j = this.length;
- if (!i) i = 0;
- if (i >= 0)
- {
- while(i < j)
- {
- if (this[i++] === elm)
- {
- i = i - 1 + j;
- j = i - j;
- }
- }
- }
- else
- j = this.indexOf(elm, j + i);
- return j !== this.length ? j : -1;
- }
- }
- /**
- * Array.lastIndexOf() prototype.
- */
- if (!Array.prototype.lastIndexOf)
- {
- Array.prototype.lastIndexOf = function(elm, i)
- {
- var j = -1;
- if (!i) i = this.length;
- if (i >= 0)
- {
- do
- {
- if (this[i--] === elm)
- {
- j = i + 1;
- i = 0;
- }
- }
- while (i > 0)
- }
- else
- {
- if (i >= this.length) j = this.lastIndexOf(elm, this.length + i);
- }
- return j;
- }
- }
- /**
- * Array.map() prototype.
- */
- if (!Array.prototype.map)
- {
- Array.prototype.map = function(func /*, thisp*/)
- {
- if (typeof func != "function") throw new TypeError();
- var len = this.length;
- var res = new Array(len);
- var thisp = arguments[1];
- for (var i = 0; i < len; i++) if (i in this) res[i] = func.call(thisp, this[i], i, this);
- return res;
- }
- }
- /**
- * Array.forEach() prototype.
- */
- if (!Array.prototype.forEach)
- {
- Array.prototype.forEach = function(func /*, thisp*/)
- {
- if (typeof func != "function") throw new TypeError();
- var len = this.length;
- var thisp = arguments[1];
- for (var i = 0; i < len; i++) if (i in this) func.call(thisp, this[i], i, this);
- return true;
- }
- }
- /**
- * Array.every() prototype.
- */
- if (!Array.prototype.every)
- {
- Array.prototype.every = function(fun /*, thisp*/)
- {
- var len = this.length;
- if (typeof fun != "function") throw new TypeError();
- var thisp = arguments[1];
- for (var i = 0; i < len; i++)
- {
- if (i in this && !fun.call(thisp, this[i], i, this)) return false;
- }
- return true;
- }
- }
- /**
- * Array.filter() prototype.
- */
- if (!Array.prototype.filter)
- {
- Array.prototype.filter = function(fun /*, thisp*/)
- {
- if (typeof fun != "function") throw new TypeError();
- var j = this.length;
- var res = new Array();
- var thisp = arguments[1];
- for (var i = 0; i < j; i++)
- {
- if (i in this)
- {
- var val = this[i];
- if (fun.call(thisp, val, i, this)) res.push(val);
- }
- }
- return res;
- }
- }
- /**
- * Array.reduce() prototype.
- */
- if (!Array.prototype.reduce)
- {
- Array.prototype.reduce = function(fun /*, initial*/)
- {
- if (typeof fun != "function") throw new TypeError();
- var j = this.length;
- if (j == 0 && arguments.length == 1) throw new TypeError();
- var i = 0;
- if (arguments.length >= 2)
- {
- var rv = arguments[1];
- }
- else
- {
- do
- {
- if (i in this)
- {
- rv = this[i++];
- break;
- }
- if (++i >= j) throw new TypeError();
- }
- while (true);
- }
- for (; i < j; i++)
- {
- if (i in this) rv = fun.call(null, rv, this[i], i, this);
- }
- return rv;
- }
- }
- /**
- * Array.reduceRight() prototype.
- */
- if (!Array.prototype.reduceRight)
- {
- Array.prototype.reduceRight = function(func /*, initial*/)
- {
- if (typeof func != "function") throw new TypeError();
- var j = this.length;
- if (j == 0 && arguments.length == 1) throw new TypeError();
- var i = j - 1;
- if (arguments.length >= 2)
- {
- var rv = arguments[1];
- }
- else
- {
- do
- {
- if (i in this)
- {
- rv = this[i--];
- break;
- }
- if (--i < 0) throw new TypeError();
- }
- while (true);
- }
- for (; i >= 0; i--)
- {
- if (i in this) rv = func.call(null, rv, this[i], i, this);
- }
- return rv;
- }
- }
- /**
- * Array.some() prototype.
- */
- if (!Array.prototype.some)
- {
- Array.prototype.some = function(func /*, thisp*/)
- {
- var len = this.length;
- if (typeof func != "function") throw new TypeError();
- var thisp = arguments[1];
- for (var i = 0; i < len; i++)
- {
- if (i in this && func.call(thisp, this[i], i, this)) return true;
- }
- return false;
- }
- }
- /**
- * Object.toSource() prototype.
- */
- if (!Object.prototype.toSource)
- {
- Object.prototype.toSource = function()
- {
- if (typeof this == 'string' || this instanceof String) return '"' + this + '"';
- if (typeof this == 'number' || this instanceof Number) return '"' + this + '"';
- if (typeof this == 'boolean' || this instanceof Boolean) return '"' + this + '"';
- if (typeof this == 'function' || this instanceof Function)
- {
- var r = this.toString();
- r = r.replace(/function\(/, 'function \(').replace(/\)\{/, ') {');
- return '(' + r + ')';
- }
- if (this instanceof Array)
- {
- var r = [];
- for (var i = 0, len = this.length; i < len; i++)
- {
- if (typeof this[i] == 'undefined') r.push('undefined');
- else if (this[i] == null) r.push('null');
- else r.push(this[i].toSource());
- }
- return '[' + r.join(', ') + ']';
- }
- if (this instanceof Object)
- {
- var r = [];
- for (var i in this)
- {
- if (i == 'toSource') continue;
- if (typeof this[i] == 'undefined') r.push(i + ':undefined');
- else if (this[i] == null) r.push(i + ':null');
- else r.push(i + ':' + this[i].toSource());
- }
- return '{' + r.join(', ') + '}';
- }
- return 'unknown';
- }
- }
- /**
- * Date.format() prototype.
- */
- if (!Date.prototype.format)
- {
- Date.prototype.format = function(format)
- {
- var YYYY, YY, MMMM, MMM, MM, M, DDDD, DDD, DD, D, hhh, hh, h, mm, m, ss, s;
- YY = ((YYYY = this.getFullYear()) + '').substr(2, 2);
- MM = (M = this.getMonth() + 1) < 10 ? ('0' + M) : M;
- MMM = (MMMM =
- [
- "January", "February", "March", "April", "May", "June",
- "July", "August", "September", "October", "November", "December"
- ][M - 1]).substr(0, 3);
- DD = (D = this.getDate()) < 10 ? ('0' + D) : D;
- DDD = (DDDD =
- [
- "Sunday", "Monday", "Tuesday", "Wednesday",
- "Thursday", "Friday", "Saturday"
- ][this.getDay()]).substr(0, 3);
- h = (hhh = this.getHours());
- if (h == 0) h = 24;
- if (h > 12) h -= 12;
- hh = h < 10 ? ('0' + h) : h;
- mm = (m = this.getMinutes()) < 10 ? ('0' + m) : m;
- ss = (s = this.getSeconds()) < 10 ? ('0' + s) : s;
- return format.replace("#YYYY#", YYYY).replace("#YY#", YY).replace("#MMMM#", MMMM)
- .replace("#MMM#", MMM).replace("#MM#", MM).replace("#M#", M).replace("#DDDD#", DDDD)
- .replace("#DDD#", DDD).replace("#DD#", DD).replace("#D#", D).replace("#hhh#", hhh)
- .replace("#hh#", hh).replace("#h#", h).replace("#mm#", mm).replace("#m#", m).
- replace("#ss#", ss).replace("#s#", s);
- }
- }
- /**
- * Date.timestamp() prototype.
- */
- if (!Date.prototype.timestamp)
- {
- Date.prototype.timestamp = function()
- {
- return Math.round(this.getTime() / 1000);
- }
- }
Add Comment
Please, Sign In to add comment