Skip to content

Instantly share code, notes, and snippets.

@ne-sachirou
Created June 10, 2011 14:36
Show Gist options
  • Save ne-sachirou/1018954 to your computer and use it in GitHub Desktop.
Save ne-sachirou/1018954 to your computer and use it in GitHub Desktop.
Define ES5 extention by ES3 #js
// @description Define ES5 extention by ES3
// @author ne_Sachirou http://c4se.tk/profile/ne.html
// @site https://gist.github.com/1018954
// @date 2011
// @license Public Domain
//java -jar compiler.jar --compilation_level SIMPLE_OPTIMIZATIONS --js_output_file es5.g.js --js es5.js
(function () {
'use strict';
var Array_prototype = Array.prototype;
if (!Array.isArray) {
Array.isArray = function (obj) { // @param Object:
// @return Boolean: obj is an Array or not
return Object.prototype.toString.call(obj) === '[object Array]';
};
}
if (!Array_prototype.every) {
Array_prototype.every = function (fun, // @param Function:
obj) { // @param Object: this in fun
// @return Boolean:
var i = this.length - 1;
for (; i >= 0; i -= 1) {
if (typeof this[i] !== 'undefined' &&
!fun.call(obj, this[i], i, this)) {
return false;
}
}
return true;
};
}
if (!Array_prototype.filter) {
Array_prototype.filter = function (fun, // @param Function:
obj) { // @param Object: this in fun
// @return Array:
var arr = [],
i = 0,
len = this.length;
for (; i < len; i += 1) {
if (typeof this[i] !== 'undefined' &&
fun.call(obj, this[i], i, this)) {
arr.push(this[i]);
}
}
return arr;
};
}
if (!Array_prototype.forEach) {
Array_prototype.forEach = function (fun, // @param Function:
obj) { // @param Object: this in fun
// @return Array: this
var i = this.length - 1;
for (; i >= 0; i -= 1) {
if (typeof this[i] !== 'undefined') {
fun.call(obj, this[i], i, this);
}
}
return this;
};
}
if (!Array_prototype.indexOf) {
Array_prototype.indexOf = function (val, // @param Object:
num) { // @param Number=0:
// @return Number: not found = -1
var i,
len = this.length;
num = num || 0;
while (num < 0) {
num += len - 1;
}
for (i = num; i < len; i += 1) {
if (this[i] === val) {
return i;
}
}
return -1;
};
}
if (!Array_prototype.lastIndexOf) {
Array_prototype.lastIndexOf = function (val, // @param Object:
num) { // @param Number=(this.length-1):
// @return Number: not found = -1
var i,
len = this.length;
if (typeof num === 'undefined') {
num = len - 1;
}
while (num < 0) {
num += len - 1;
}
i = num;
for (; i >= 0; i -= 1) {
if (this[i] === val) {
return i;
}
}
return -1;
};
}
if (!Array_prototype.map) {
Array_prototype.map = function (fun, // @param Function:
obj) { // @param Object: this in fun
// @return Array:
var i = this.length,
arr = new Array(i);
for (; i >= 0; i -= 1) {
if (typeof this[i] !== 'undefined') {
arr[i] = fun.call(obj, this[i], i, this);
}
}
return arr;
};
}
if (!Array_prototype.reduce) {
Array_prototype.reduce = function (fun, // @param Function:
val) { // @param Object:
// @return Object:
var i = 0,
len = this.length;
if (typeof val === 'undefined') {
val = this[0];
i = 1;
}
for (; i < len; i += 1) {
if (typeof this[i] !== 'undefined') {
val = fun.call(null, val, this[i], i, this);
}
}
return val;
};
}
if (!Array_prototype.reduceRight) {
Array_prototype.reduceRight = function (fun, // @param Function:
val) { // @param Object:
// @return Object:
var i = this.length - 1;
if (typeof val !== 'undefined') {
val = this[i];
i -= 1;
}
for (; i >= 0; i -= 1) {
if (typeof this[i] !== 'undefined') {
val = fun.call(null, val, this[i], i, this);
}
}
return val;
};
}
if (!Array_prototype.some) {
Array_prototype.some = function (fun, // @param Function:
obj) { // @param Object:
// @return Boolean:
var i = this.length - 1;
for (; i >= 0; i -= 1) {
if (typeof this[i] !== 'undefined' &&
fun.call(obj, this[i], i, this)) {
return true;
}
}
return false;
};
}
// http://blog.stevenlevithan.com/archives/faster-trim-javascript
if (!String.prototype.trim) {
String.prototype.trim = function () { // @return String:
var str = this.replace(/^\s\s*/, ''),
ws = /\s/,
i = str.length;
while (ws.test(str.charAt(i -= 1))) {
}
return str.slice(0, i + 1);
};
}
if (!Object.keys) {
Object.keys = function (obj) { // @param Object:
// @return Array[String]:
var key, result = [];
for (key in obj) {
if (obj.hasOwnProperty(key)) {
result.push(key);
}
}
return result;
};
}
if (!Date.now) {
Date.now = function () {
return new Date().getTime();
};
}
if (!Date.prototype.toISOString) {
Date.prototype.toISOString = function () {
return (this.getUTCFullYear() < 1000 ?
this.getUTCFullYear() < 100 ?
this.getUTCFullYear() < 10 ?
'000' :
'00' :
'0' :
'') + this.getUTCFullYear() + '-' +
(this.getUTCMonth() + 1 < 10 ? '0' : '') + (this.getUTCMonth() + 1) + '-' +
(this.getUTCDate() < 10 ? '0' : '') + this.getUTCDate() + 'T' +
(this.getUTCHours() < 10 ? '0' : '') + this.getUTCHours() + ':' +
(this.getUTCMinutes() < 10 ? '0' : '') + this.getUTCMinutes() + ':' +
(this.getUTCSeconds() < 10 ? '0' : '') + this.getUTCSeconds() + 'Z';
};
}
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
if (!Function.prototype.bind) {
Function.prototype.bind = function (oThis) {
var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function () {};
function fBound() {
return fToBind.apply(this instanceof fNOP ? this : oThis || window,
aArgs.concat(Array.prototype.slice.call(arguments)));
};
// closest thing possible to the ECMAScript 5 internal IsCallable function
if (typeof this !== "function") {
throw new TypeError("Function.prototype.bind - what is trying to be fBound is not callable");
}
fNOP.prototype = this.prototype;
fBound.prototype = new fNOP();
return fBound;
};
}
}());
/* (Public Domain)2011 ne_Sachirou https://gist.github.com/1018954 */
(function(){var d=Array.prototype;if(!Array.isArray)Array.isArray=function(c){return Object.prototype.toString.call(c)==="[object Array]"};if(!d.every)d.every=function(c,b){for(var a=this.length-1;a>=0;a-=1)if(typeof this[a]!=="undefined"&&!c.call(b,this[a],a,this))return false;return true};if(!d.filter)d.filter=function(c,b){for(var a=[],e=0,f=this.length;e<f;e+=1)typeof this[e]!=="undefined"&&c.call(b,this[e],e,this)&&a.push(this[e]);return a};if(!d.forEach)d.forEach=function(c,b){for(var a=this.length-
1;a>=0;a-=1)typeof this[a]!=="undefined"&&c.call(b,this[a],a,this);return this};if(!d.indexOf)d.indexOf=function(c,b){var a,e=this.length;for(b=b||0;b<0;)b+=e-1;for(a=b;a<e;a+=1)if(this[a]===c)return a;return-1};if(!d.lastIndexOf)d.lastIndexOf=function(c,b){var a;a=this.length;if(typeof b==="undefined")b=a-1;for(;b<0;)b+=a-1;for(a=b;a>=0;a-=1)if(this[a]===c)return a;return-1};if(!d.map)d.map=function(c,b){for(var a=this.length,e=Array(a);a>=0;a-=1)if(typeof this[a]!=="undefined")e[a]=c.call(b,this[a],
a,this);return e};if(!d.reduce)d.reduce=function(c,b){var a=0,e=this.length;if(typeof b==="undefined"){b=this[0];a=1}for(;a<e;a+=1)if(typeof this[a]!=="undefined")b=c.call(null,b,this[a],a,this);return b};if(!d.reduceRight)d.reduceRight=function(c,b){var a=this.length-1;if(typeof b!=="undefined"){b=this[a];a-=1}for(;a>=0;a-=1)if(typeof this[a]!=="undefined")b=c.call(null,b,this[a],a,this);return b};if(!d.some)d.some=function(c,b){for(var a=this.length-1;a>=0;a-=1)if(typeof this[a]!=="undefined"&&
c.call(b,this[a],a,this))return true;return false};if(!String.prototype.trim)String.prototype.trim=function(){for(var c=this.replace(/^\s\s*/,""),b=/\s/,a=c.length;b.test(c.charAt(a-=1)););return c.slice(0,a+1)};if(!Object.keys)Object.keys=function(c){var b,a=[];for(b in c)c.hasOwnProperty(b)&&a.push(b);return a};if(!Date.now)Date.now=function(){return(new Date).getTime()};if(!Date.prototype.toISOString)Date.prototype.toISOString=function(){return(this.getUTCFullYear()<1E3?this.getUTCFullYear()<100?
this.getUTCFullYear()<10?"000":"00":"0":"")+this.getUTCFullYear()+"-"+(this.getUTCMonth()+1<10?"0":"")+(this.getUTCMonth()+1)+"-"+(this.getUTCDate()<10?"0":"")+this.getUTCDate()+"T"+(this.getUTCHours()<10?"0":"")+this.getUTCHours()+":"+(this.getUTCMinutes()<10?"0":"")+this.getUTCMinutes()+":"+(this.getUTCSeconds()<10?"0":"")+this.getUTCSeconds()+"Z"};if(!Function.prototype.bind)Function.prototype.bind=function(c){function b(){return e.apply(this instanceof f?this:c||window,a.concat(Array.prototype.slice.call(arguments)))}
var a=Array.prototype.slice.call(arguments,1),e=this,f=function(){};if(typeof this!=="function")throw new TypeError("Function.prototype.bind - what is trying to be fBound is not callable");f.prototype=this.prototype;b.prototype=new f;return b}})();
/*
http://www.JSON.org/json2.js
2011-02-23
Public Domain.
NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
See http://www.JSON.org/js.html
This code should be minified before deployment.
See http://javascript.crockford.com/jsmin.html
USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO
NOT CONTROL.
*/
var JSON;JSON||(JSON={}),function(){function f(a){return a<10?"0"+a:a}function quote(a){return escapable.lastIndex=0,escapable.test(a)?'"'+a.replace(escapable,function(a){var b=meta[a];return typeof b=="string"?b:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function str(a,b){var c,d,e,f,g=gap,h,i=b[a];i&&typeof i=="object"&&typeof i.toJSON=="function"&&(i=i.toJSON(a)),typeof rep=="function"&&(i=rep.call(b,a,i));switch(typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i)return"null";gap+=indent,h=[];if(Object.prototype.toString.apply(i)==="[object Array]"){f=i.length;for(c=0;c<f;c+=1)h[c]=str(c,i)||"null";return e=h.length===0?"[]":gap?"[\n"+gap+h.join(",\n"+gap)+"\n"+g+"]":"["+h.join(",")+"]",gap=g,e}if(rep&&typeof rep=="object"){f=rep.length;for(c=0;c<f;c+=1)typeof rep[c]=="string"&&(d=rep[c],e=str(d,i),e&&h.push(quote(d)+(gap?": ":":")+e))}else for(d in i)Object.prototype.hasOwnProperty.call(i,d)&&(e=str(d,i),e&&h.push(quote(d)+(gap?": ":":")+e));return e=h.length===0?"{}":gap?"{\n"+gap+h.join(",\n"+gap)+"\n"+g+"}":"{"+h.join(",")+"}",gap=g,e}}"use strict",typeof Date.prototype.toJSON!="function"&&(Date.prototype.toJSON=function(a){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(a){return this.valueOf()});var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},rep;typeof JSON.stringify!="function"&&(JSON.stringify=function(a,b,c){var d;gap="",indent="";if(typeof c=="number")for(d=0;d<c;d+=1)indent+=" ";else typeof c=="string"&&(indent=c);rep=b;if(!b||typeof b=="function"||typeof b=="object"&&typeof b.length=="number")return str("",{"":a});throw new Error("JSON.stringify")}),typeof JSON.parse!="function"&&(JSON.parse=function(text,reviver){function walk(a,b){var c,d,e=a[b];if(e&&typeof e=="object")for(c in e)Object.prototype.hasOwnProperty.call(e,c)&&(d=walk(e,c),d!==undefined?e[c]=d:delete e[c]);return reviver.call(a,b,e)}var j;text=String(text),cx.lastIndex=0,cx.test(text)&&(text=text.replace(cx,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)}));if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return j=eval("("+text+")"),typeof reviver=="function"?walk({"":j},""):j;throw new SyntaxError("JSON.parse")})}()
@ne-sachirou
Copy link
Author

@ne-sachirou
Copy link
Author

https://gist.github.com/1018954/d0832ef7c7e364118aabfff077eb054d47dd0bd5
Rename es5.g.js to es5.min.js.
Add json2.js into es5.min.js for JSON support.

@ne-sachirou
Copy link
Author

@ne-sachirou
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment