Created
March 2, 2019 12:55
-
-
Save ske2004/da64632704f758f246a3ddf36e19eaed to your computer and use it in GitHub Desktop.
Lightweight AJAX and URL library
This file contains hidden or 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
/* | |
Author: ishidex2 | |
License: MIT | |
*/ | |
class URLUtils | |
{ | |
static isObj(a):boolean | |
{ | |
if ((!!a) && (a.constructor === Object)) | |
{ | |
return true; | |
} | |
return false; | |
} | |
static _st(z,g):string | |
{ | |
return `${g != "" ? "[" : ""}${z}${g != "" ? "]" : ""}`; | |
} | |
static fp(params, skipobjects=false, prefix=""):string | |
{ | |
var result = ""; | |
if (typeof (params) != "object") | |
{ | |
return `${prefix}=${encodeURIComponent(params)}` + `&`; | |
} | |
for (var param in params) | |
{ | |
var c = `${prefix}${URLUtils._st(param, prefix)}`; | |
if (URLUtils.isObj(params[param]) && !skipobjects) | |
{ | |
result += URLUtils.fp(params[param], false, `${c}`); | |
} | |
else if (Array.isArray(params[param]) && !skipobjects) | |
{ | |
params[param].forEach(function (item, ind) | |
{ | |
result += URLUtils.fp(item, false, `${c}[${ind}]`); | |
}); | |
} | |
else | |
{ | |
result += `${c}=${encodeURIComponent(params[param])}` + `&`; | |
} | |
} | |
return result; | |
} | |
static objAddArray(robj, arr, val, i = 0) | |
{ | |
if (!isNaN(arr[i]) && !robj) robj = []; | |
robj = robj || {}; | |
// var obj = {}; | |
i = i || 0; | |
if (arr[i] !== undefined) | |
{ | |
robj[arr[i]] = URLUtils.objAddArray(robj[arr[i]], arr, val, i+1); | |
} | |
else | |
{ | |
return decodeURIComponent(val); | |
} | |
return robj; | |
}; | |
static objectify(e):object | |
{ | |
var obj = {}; | |
e.forEach(function(item, i){ | |
if (!item) return; | |
console.log(item[0]); | |
obj = URLUtils.objAddArray(obj, item[0], item[1]) | |
}) | |
return obj; | |
}; | |
static toJSON(url, useRaw = false):string | |
{ | |
return JSON.stringify(URLUtils.toObject(url, useRaw)); | |
} | |
static fromJSON(params, skipobjects=false):string | |
{ | |
return this.fromObject(JSON.parse(params), skipobjects); | |
} | |
static toObject(url, useRaw = false):object | |
{ | |
var result = []; | |
var obj; | |
result = url.split("&"); | |
result.forEach(function(i, n){ | |
if (i === "") return; | |
result[n] = i.split("="); | |
if (!useRaw) | |
{ | |
result[n][0] = result[n][0].split(/[\[\]]/g).filter(function (el) { | |
return el !== ""; | |
}); | |
} | |
}); | |
return URLUtils.objectify(result); | |
} | |
static fromObject( params, skipobjects=false):string | |
{ | |
return URLUtils.fp(params, skipobjects, ""); | |
} | |
} | |
class AjaxUtils | |
{ | |
static send(params): void { | |
var xhttp = new XMLHttpRequest(); | |
var isGet:boolean = params.method.toUpperCase()=="GET"; | |
var formattedParams; | |
if (typeof(params.data)==="string") | |
{ | |
formattedParams = params.data; | |
} | |
else | |
{ | |
formattedParams = URLUtils.fromObject(params.data); | |
} | |
params.error = params.error || function(){} | |
params.callback = params.callback || function () { | |
if (this.readyState == 4 && this.status == 200) { | |
params.success(this); | |
} | |
else if (this.readyState == 4) | |
{ | |
params.error(this); | |
} | |
} | |
xhttp.onreadystatechange = params.callback; | |
xhttp.open(params.method, params.url+"?"+(isGet?formattedParams:""), params.async || true); | |
for (var header in params.headers) { | |
xhttp.setRequestHeader(header, params.headers[header]); | |
} | |
xhttp.setRequestHeader("Content-type", params.contentType || "application/x-www-form-urlencoded"); | |
xhttp.send(!isGet?formattedParams:""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment