PHP var_dump like¶
function var_dump(obj, alertTrue, consoleLog) {
var out = '';
if(typeof(obj) === "object"){
for (var i in obj) {
out += i + ": " + obj[i] + "\n";
}
}
else{
out = obj;
}
if(alertTrue == true)
alert(out);
else if(consoleLog == true)
console.log(out);
else{
//or, if you wanted to avoid alerts...
var pre = document.createElement('pre');
pre.innerHTML = out;
document.body.appendChild(pre)
}
}
YYYYMMDDHHMMSS to date¶
function stringYYYYMMDDHHMMSSToDate(string) {
string = string.split('');
date = string[6] + string[7] + "/" + string[4] + string[5] + "/" + string[3] + string[1] + string[2] + string[0] + " à " + string[8] + string[9] + ":" + string[10] + string[11] + ":" + string[12] + string[13];
return date;
}
RGB to BGR and vice versa¶
Used in Google Earth for example
function RGBtoBGR(RGB)
{
BGR = RGB.split('');
BGR = BGR[4] + "" + BGR[5] + "" + BGR[2] + "" + BGR[3] + "" + BGR[0] + "" + BGR[1];
return BGR;
};
function BGRtoRGB(BGR)
{
RGB = BGR.split('');
RGB = RGB[4] + "" + RGB[5] + "" + RGB[2] + "" + RGB[3] + "" + RGB[0] + "" + RGB[1];
return RGB;
};
Distance between two coordinates¶
function distance2Points(latA, longA, latB, longB) {
var kEarthRadiusKms = 6378.164; //rayon equatorial de la Terre
var dLat1InRad = latA * (Math.PI / 180.0);
var dLong1InRad = longA * (Math.PI / 180.0);
var dLat2InRad = latB * (Math.PI / 180.0);
var dLong2InRad = longB * (Math.PI / 180.0);
var dLongitude = dLong2InRad - dLong1InRad;
var dLatitude = dLat2InRad - dLat1InRad;
var a = Math.pow(Math.sin(dLatitude / 2.0), 2.0) +
Math.cos(dLat1InRad) * Math.cos(dLat2InRad) *
Math.pow(Math.sin(dLongitude / 2.0), 2.0);
// Intermediate result c (great circle distance in Radians).
var c = 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a));
// Distance.
dDistance = kEarthRadiusKms * c;
// resultat en kilomètres
return Math.abs(dDistance);
}
Convert degree to radian¶
if (typeof (Number.prototype.toRad) === "undefined") {
Number.prototype.toRad = function () {
return this * Math.PI / 180;
}
}
Convert radian to degree¶
if (typeof (Number.prototype.toDeg) === "undefined") {
Number.prototype.toDeg = function () {
return this * 180 / Math.PI;
}
}