Skip to content

Instantly share code, notes, and snippets.

@kevadsett
kevadsett / ValueMapper
Last active December 23, 2015 08:39
ValueMapper - normalise and transform - great for converting a number from one range to a different range
var ValueMapper = {
normalise: function(value, low, high) {
var range = high - low;
return (value - low) / range;
},
transform: function(value, inputLow, inputHigh, outputLow, outputHigh) {
var normal = this.normalise(value, inputLow, inputHigh);
return normal * (outputHigh - outputLow) + outputLow;
}
@kevadsett
kevadsett / function cloneObject
Created September 8, 2013 08:32
Simple plain javascript object clone (not fully tested but it does as much as I need!).
function cloneObject(objectToClone) {
var key, i, clonedArray = [];
var clone = {};
if(typeof objectToClone == "object") {
if(objectToClone.length == undefined) {
for(key in objectToClone){
clone[key] = cloneObject(objectToClone[key]);
}
} else { // we're an array or string
clone = objectToClone.slice(0)
@kevadsett
kevadsett / Localhost mode
Last active December 22, 2015 03:38
Change local mode (file://c:/whatever) to localhost mode (http://localhost/whatever) – requires symbolic link in server's htdocs folder.
var url = document.URL,
currentLocation = url.split("//"),
newURL = "",
path = "";
if(currentLocation[0] == "file:"){
path = url.substring(11, url.length);
newURL = "http://localhost/" + path;
}
document.location = newURL;