Last active
August 29, 2015 14:27
-
-
Save pixelgrid/a6ea00263a6df79e8cc4 to your computer and use it in GitHub Desktop.
Small converter utility for converting between provided values. Its parameters must be the type names and the type values association
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
function Converter(types, values) { | |
var associations = {}, | |
values = values, | |
types = types; | |
return function(baseValue){ | |
types.forEach(function(type){ | |
associations[type] = { | |
to : types.reduce(function(current, previous){ | |
if(type == previous){ | |
return current; | |
} | |
var index1 = types.indexOf(type) + 1, | |
index2 = types.indexOf(previous) + 1; | |
if(index1 > index2){ | |
var reversed = true; | |
index2 = [index1, index1 = index2][0]; | |
} | |
current[previous] = values | |
.slice(index1, index2) | |
.reduce(function(current, previous){ | |
return current * previous; | |
}, 1) * baseValue; | |
if(reversed){ | |
current[previous] = (1 / current[previous]) * baseValue; | |
} | |
return current; | |
}, {}) | |
} | |
}); | |
return associations; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage