Created
August 16, 2013 18:29
-
-
Save r3b/6252304 to your computer and use it in GitHub Desktop.
A collection of random polyfills. YMMV.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Array | |
([].indexOf)||(Array.prototype.indexOf=[].indexOf||function(a,b,c,r){for(c=this,b=b|0,r=-1;b++<c.length&&!~r;r=c[b]===a?b:r);return r}); | |
([].map)||(Array.prototype.map=function(a){for(var b=this,c=b.length,d=[],e=0,f;e<b;)d[e]=e in b?a.call(arguments[1],b[e],e++,b):f;return d}); | |
([].filter)||(Array.prototype.filter=[].filter||function(a,b,c,d,e){for(b=this,d=0,c=[];d<b.length;d++)if(a.call(b,e=b[d]))c.push(e);return c}); | |
([].isArray)||(Array.isArray||(Array.isArray=function(a){return{}.toString.call(a)=='[object Array]'})); | |
([].every)||(Array.prototype.every=[].every||function(a,b,c,d){for(c=0,d=this;c<d.length;c++)if(!a.call(b,d[c],c,d))return!1;return!0}); | |
([].some)||(Array.prototype.some=[].some||function(a,b,c,d){for(c=0,d=this;c<d.length;c++)if(a.call(b,d[c],c,d))return!0;return!1}); | |
([].unique)||(Array.prototype.unique=function(a){return function(){return this.filter(a)}}(function(a,b,c){return c.indexOf(a,b+1)<0})); | |
//Function | |
Function.prototype.bind=(function(){}).bind||function(a,b){b=this;return function(){b.apply(a,arguments)}} | |
Function.prototype.partial=function(){ | |
var fn=this,b=[].slice.call(arguments); | |
return function(){return fn.apply(this,b.concat([].slice.call(arguments)))} | |
} | |
Function.prototype.time=function(n){var fn=this;(!n)&&(n=String.random(10));return function(){console.time(n);var val=fn.apply(this,arguments);console.timeEnd(n);return val;}} | |
Function.prototype.avgtime=function(count, name){ | |
var fn=this; | |
return function(){ | |
var args=arguments,val=""; | |
function timer(){ | |
//var n=String.random(10); | |
var start=Date.now(); | |
//console.time(n); | |
val=fn.apply(this,args); | |
//console.timeEnd(n); | |
return Date.now()-start; | |
} | |
var tests=0,i=count; | |
do{ | |
var time=timer(); | |
tests+=time; | |
}while(--i>0); | |
return [name,tests/count,val]; | |
} | |
} | |
Function.prototype.extend = function(superClass) { | |
var subClass=this,F = function() {}; | |
F.prototype = superClass.prototype; | |
subClass.prototype = new F(); | |
subClass.prototype.constructor = subClass; | |
subClass.superclass = superClass.prototype; | |
if(superClass.prototype.constructor == Object.prototype.constructor) { | |
superClass.prototype.constructor = superClass; | |
} | |
return subClass; | |
} | |
Function.prototype.debounce = function(delay, maxCalls) { | |
var func = this, timeout, calls=[]; | |
if(!maxCalls)maxCalls=10; | |
function callback(){ | |
while(calls.length>0){ | |
var call=calls.pop() | |
func.apply(call[0], call[1]); | |
} | |
} | |
return function debounced() { | |
var pointer = this, args = arguments; | |
calls.push([pointer,args]); | |
if(calls.length<maxCalls){ | |
clearTimeout(timeout); | |
timeout = setTimeout(callback, delay); | |
} else { | |
callback(); | |
} | |
}; | |
} | |
Function.prototype.throttle = function(delay) { | |
var func = this, timeout; | |
return function throttled() { | |
var pointer = this, args = arguments; | |
clearTimeout(timeout); | |
timeout = setTimeout(function(){func.apply(pointer, args);}, delay); | |
}; | |
} | |
Function.prototype.wrapped = function (before, after) { | |
var f="function";func = this; | |
return function() { | |
var obj = this, args = arguments; | |
if(before && typeof before === f){before.apply(obj, args);} | |
func.apply(obj, args); | |
if(after && typeof after === f){after.apply(obj, args);} | |
}; | |
} | |
Function.prototype.limit = function (limit) { | |
var func = this; | |
return function() { | |
var obj = this, args = arguments; | |
func.apply(obj, args); | |
if(--limit === 0){ | |
func=function(){ | |
if(console && console.warn){ | |
console.warn("This method has reached its execution limit."); | |
} | |
} | |
} | |
}; | |
} | |
/* logging */ | |
function Logger(){ | |
function tr(m){return {'E':'End','%':'group','#':'profile','!':'time'}[m];} | |
var logEnabled = true, | |
methods="log error warn info debug assert clear count dir dirxml exception % %Collapsed %E # #E table ! !E trace".replace(/([E%#!])/g,tr).split(' '), | |
con=window.console; | |
function createLogMethod(method) { | |
return function() { | |
logEnabled && con && con[ method ] && con[ method ].apply( con, arguments ); | |
} | |
} | |
for ( var i=0;i <methods.length;i++ ) { | |
var method=methods[i]; | |
this[ method ] = createLogMethod(method); | |
} | |
} | |
('extend' in Object)||(Object.prototype.extend=function(a,b,c,d){b=b||this;d={};for(c in a){d[c]=b[c];b[c]=a[c]};return{undo:function(){Object.prototype.extend(d,b)}}}); | |
Object.prototype.keys=Object.prototype.keys||function(){var o=this,k,r=[];for(k in o)r.hasOwnProperty.call(o,k)&&r.push(k);return r} | |
Object.prototype.clone=function(a,b){with(Object)return getOwnPropertyNames(a).map(b=function(c){b[c]=getOwnPropertyDescriptor(a,c)}),create(getPrototypeOf(a),b)} | |
Date.now=Date.now||function(){return+new Date} | |
Date.prototype.ago=function (){var b,c,d=this;for(b=[1e3,60,60,24],d=new Date-d,c=0;d>2*b[c];d/=b[c++]);return~~d+" "+"m0second0minute0hour0day".split(0)[c]+"s ago"} | |
Number.prototype.suffix=function(){ var a=this;return a+["th","st","nd","rd"][(a=~~(a<0?-a:a)%100)>10&&a<14||(a%=10)>3?0:a]} | |
Number.prototype.pad =function(l){return(1e15+this+"").slice(-l)} | |
//??? Number.prototype.stylize=function(a,b,c){a=a||this;for(a in c="I??O?GEUCC".charCodeAt(a).toString(2))b=[b]+" |_"[1^c[a]&&a%2+1]+["\n"[~-a%4]];return b} | |
String.prototype.trim=''.trim||function(){return this.replace(/^\s+|\s+$/g,'')} | |
String.prototype.diff=function(a,b){if(!b){b=a;a=this}for(var c=0,d=a.length,e=b.length;a[c]&&a[c]==b[c];c++);for(;d>c&&e>c&&a[d-1]==b[e-1];d--)e--;return[c,d-c,e-c]} | |
/* length, radix */String.random=function(a,b){b=b||16;return Array(a||32).join(0).replace(/0/g,function(){return(0|Math.random()*b).toString(b)})} | |
function getParam(a){return RegExp("[&?]"+a+"=([^&]+)").exec(location)[1]} | |
function isSameOrigin(a,b,c){b=document.createElement("a");b.href=a;return(a=location)[c="protocol"]==b[c]&&a[c="hostname"]==b[c]&&+a.port||80==+b.port||80} | |
function cssPrefix(s){return eval(0+"O-Moz-Webkit-Ms-".replace(/.*?-|$/g,"||(s='$&"+s+"')in new Image().style&&s").replace(/-(.)/g,"'+'$1'.toUpperCase()+'"))} | |
/* array of URLs, asyc true/false */function loadScript(a,b,c){with(document)for(;(c=createElement("script")).src=a.shift();body.appendChild(c))c.async=b} | |
//window.XMLHttpRequest=(function(a){for(a=3;a--;)try{return (this.XMLHttpRequest||ActiveXObject)(["Msxml2","Msxml3","Microsoft"][a]+".XMLHTTP")}catch(e){}}()); | |
window.XMLHttpRequest = window.XMLHttpRequest||function (){for(a = 3;a--;) try {return new(ActiveXObject)(["Msxml2","Msxml3","Microsoft"][a] + ".XMLHTTP")}catch(e){}}; | |
function Promise() { | |
this.complete = false; | |
this.error = null; | |
this.result = null; | |
this.callbacks = []; | |
} | |
Promise.prototype={ | |
create:function(){return new Promise()}, | |
then:function(callback, context) { | |
var f = function() {return callback.apply(context, arguments)}; | |
if (this.complete) { | |
f(this.error, this.result); | |
} else { | |
this.callbacks.push(f); | |
} | |
}, | |
done:function(error, result) { | |
this.complete = true; | |
this.error = error; | |
this.result = result; | |
for (var i = 0; i < this.callbacks.length; i++) this.callbacks[i](error, result); | |
this.callbacks.length=0; | |
}, | |
join:function(promises) { | |
var p = new Promise(), | |
total = promises.length, | |
completed = 0, | |
errors = [], | |
results = []; | |
function notifier(i) { | |
return function(error, result) { | |
completed += 1; | |
errors[i] = error; | |
results[i] = result; | |
if (completed === total) { | |
p.done(errors, results); | |
} | |
}; | |
} | |
for (var i = 0; i < total; i++) { | |
promises[i]().then(notifier(i)); | |
} | |
return p; | |
}, | |
chain:function(promises, error, result) { | |
var p = new Promise(); | |
if (promises.length === 0) { | |
p.done(error, result); | |
} else { | |
promises[0](error, result).then(function(res, err) { | |
promises.splice(0, 1); | |
chain(promises, res, err).then(function(r, e) { | |
p.done(r, e); | |
}); | |
}); | |
} | |
return p; | |
}, | |
createTag:function(tagName, attributes){ | |
var p=new Promise(), | |
element=document.createElement(tagName); | |
if(typeof attributes ==='object'){ | |
Object.keys(attributes).forEach(function (f) { | |
element[f] = attributes[f]; | |
}); | |
} | |
function onload(e){ | |
if(e.type=='readystatechange'){ | |
if (!(e.readyState && e.readyState !== "complete" && e.readyState !== "loaded")) return; | |
} | |
p.done(null, element); | |
} | |
if(element.onafterscriptexecute){ | |
element.addEventListener('afterscriptexecute', onload); | |
} | |
element.addEventListener('load', onload); | |
document.body.appendChild(element); | |
return p; | |
} | |
} | |
function Ajax(){ | |
function encode(data) { | |
var result = ""; | |
if (typeof data === "string") { | |
result = data; | |
} else { | |
var e = encodeURIComponent; | |
for (var k in data) { | |
if (data.hasOwnProperty(k)) { | |
result += '&' + e(k) + '=' + e(data[k]); | |
} | |
} | |
} | |
return result; | |
} | |
var request=function(m,u,d){ | |
var p=new Promise(); | |
with(new XMLHttpRequest()){onreadystatechange=function(){this.readyState^4||p.done(null, this)};open(m,u);send(encode(d));} | |
return p; | |
}.memoize(); | |
this.get=request.partial('GET'); | |
this.post=request.partial('POST'); | |
this.jsonp=function(a,b,c,d){with(document)body.appendChild(createElement(d="script")).src=a+(c=d+(jsonp.a=-~jsonp.a));(a=window)[c]=function(d){a[c]=b(d)}}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment