Jump to content

User:Anomie/util.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Anomie (talk | contribs) at 12:26, 8 May 2009 (more). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* Various random utility functions I find useful. If for some reason you want to
 * use these in your code, the following line will import them for you:

importScript('User:Anomie/util.js'); // Linkback: [[User:Anomie/util.js]]
 
 * (Please keep the comment so I can see how many people use this).
 */

function jsondecode(v){
    try {
        return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u\n\r\t]/.test(v.replace(/"(\\.|[^\x22\\])*"/g,''))) && eval('('+v+')');
    } catch(e){
        return null;
    }
}

if(!Number.prototype.toHex) Number.prototype.toHex = function(p){
        var s=this.toString(16);
        var i=s.indexOf('.');
        if(i<0) i=s.length;
        if(typeof(p)!='undefined') for(p-=i; p>0; p--){
                s='0'+s;
        }
        return s;
}
if(!Number.toHex) Number.toHex = function(n,p){
	return Number(n).toHex(p);
}

if(!RegExp.quote){
    RegExp.quote = function(s){
        return s.replace(/[^\t !\x22#%&\x27,:;<=>@_`~a-z0-9]/ig, RegExp.quote.$replacer);
    }
    RegExp.quote.$replacer = function(c){
        c=c.charCodeAt(0);
        return '\\u'+c.toHex(4);
    }
}

function api(p, cb){
    var uri=wgServer+wgScriptPath+'/api.php';
    var data='format=json';
    for(var k in p){
        var v=p[k];
        if(typeof(v)=='object' && (v instanceof Array))
            v=v.map(encodeURIComponent).join('|');
        else
            v=encodeURIComponent(v);
        data+='&'+encodeURIComponent(k)+'='+v;
    }
    
    var x = sajax_init_object();
    if(!x) return false;

    if(typeof(cb)=='function'){
        x.open('POST', uri, true);
        x.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        x.onreadystatechange=function(){
            if(x.readyState!=4) return;
            var r=jsondecode(x.responseText);
            if(!r) throw new Error("Could not parse response");
            cb(r, p);
        };
        x.send(data);
        return true;
    } else {
        x.open('POST', uri, false);
        x.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        x.send(data);
        var r=jsondecode(x.responseText);
        if(!r) throw new Error("Could not parse response");
        return r;
    }
}

function rawpage(pg, cb){
    var uri=wgServer+wgScriptPath+'/index.php?action=raw&title='+encodeURIComponent(pg);
    
    var x = sajax_init_object();
    if(!x) return false;

    if(typeof(cb)=='function'){
        x.open('GET', uri, true);
        x.onreadystatechange=function(){
            if(x.readyState!=4) return;
            cb(x.responseText);
        };
        x.send(null);
        return true;
    } else {
        x.open('GET', uri, false);
        x.send(null);
        return x.responseText;
    }
}

function isClass(n, c){
	return n.className.match(new RegExp('(^|\\s)'+c+'(\\s|$)'));
}
function addClass(n, c){
	if(!isClass(n,c)) n.className=(n.className=='')?c:(n.className+' '+c);
}
function delClass(n, c){
	var cl=n.className;
	cl=cl.replace(new RegExp('(^|\\s)'+c+'(\\s|$)', 'g'), ' ');
	cl=cl.replace(/\s\s+/g, ' ').replace(/^\s+|\s+$/g, '');
	if(n.className!=cl) n.className=cl;
}

if(!Array.prototype.map) Array.prototype.map=function(callback, thisObject){
        var l=this.length;
        var a=[];
        if(typeof(thisObject)=='undefined' || thisObject===null){
                for(var i=0;i<l;i++){
                        a[i]=callback(this[i],i,this);
                }
        } else {
                for(var i=0;i<l;i++){
                        a[i]=callback.call(thisObject,this[i],i,this);
                }
        }
        return a;
}
if(!Array.map) Array.map=function(obj, callback, thisObject){
        return Array.prototype.map.call(obj, callback, thisObject);
}

if(!Array.prototype.forEach) Array.prototype.forEach=function(callback, thisObject){
	var l=this.length;
	if(typeof(thisObject)=='undefined' || thisObject===null){
		for(var i=0;i<l;i++){
			callback(this[i],i,this);
		}
	} else {
		for(var i=0;i<l;i++){
			callback.call(thisObject,this[i],i,this);
		}
	}
}
if(!Array.forEach) Array.forEach=function(obj, callback, thisObject){
	return Array.prototype.forEach.call(obj, callback, thisObject);
}

if(!Array.prototype.filter) Array.prototype.filter=function(callback, thisObject){
	var l=this.length;
	var a=[];
	if(typeof(thisObject)=='undefined' || thisObject===null){
		for(var i=0;i<l;i++){
			if(callback(this[i],i,this)) a.push(this[i]);
		}
	} else {
		for(var i=0;i<l;i++){
			if(callback.call(thisObject,this[i],i,this)) a.push(this[i]);
		}
	}
	return a;
}
if(!Array.filter) Array.filter=function(obj, callback, thisObject){
	return Array.prototype.filter.call(obj, callback, thisObject);
}