Browse Source

version bump, use node-units over half baked self made thing

master
Brett Langdon 13 years ago
parent
commit
80d3cc4ff1
3 changed files with 32 additions and 140 deletions
  1. +2
    -1
      lib/index.js
  2. +26
    -136
      lib/units/index.js
  3. +4
    -3
      package.json

+ 2
- 1
lib/index.js View File

@ -25,7 +25,8 @@ server.get('/', function(req, res){
', example (/bjcp?id=28A, /bjcp?mouthfeel=tannin)',
};
answer.units = {
'/units': 'get the currently supported list of conversions',
'/units': 'get the currently supported list of units and unit their groups',
'/units/type/:unit': 'try to determine which unit group(s) :unit belongs to',
'/units/:value/:unit_from/to/:unit_to': 'convert :value from :unit_from to :unit_to, if it is supported',
};


+ 26
- 136
lib/units/index.js View File

@ -1,143 +1,33 @@
var conversions = {
// Tempurature
'fahrenheit': {
'celsius': function(value){
value -= 32;
value *= 5
value /= 9;
return value;
},
},
'celsius': {
'fahrenheit': function(value){
value *= 9;
value /= 5;
value += 32;
return value;
},
},
// Liquid Volume
'gallon': {
'cup': function(value){
return value * 16;
},
'liter': function(value){
return value * 3.78541178;
},
'pint': function(value){
return value * 8;
},
'milliliter': function(value){
return value * 3785.41178;
}
},
'cup': {
'gallon': function(value){
return value / 16;
},
'liter': function(value){
return (value / 16) * 3.78541178;
},
'pint': function(value){
return value / 2;
},
'milliliter': function(value){
return value * 236.588237;
},
},
'liter': {
'gallon': function(value){
return value / 3.78541178;
},
'cup': function(value){
return (value / 3.78541178) * 16;
},
'pint': function(value){
return (value / 3.78541178) * 8;
},
'milliliter': function(value){
return value * 1000;
},
},
'milliliter': {
'gallon': function(value){
return value / 3785.41178;
},
'cup': function(value){
return value / 236.588;
},
'pint': function(value){
return value / 473.176;
},
'liter': function(value){
return value / 1000;
},
},
'pint': {
'gallon': function(value){
return value * 0.125;
},
'cup': function(value){
return value * 2;
},
'liter': function(value){
return value * 0.473176;
},
'milliliter': function(value){
return value * 473.176;
},
},
// Weight
'pound': {
'ounce': function(value){
return value * 16;
},
'gram': function(value){
return value * 453.592;
},
},
'ounce': {
'pound': function(value){
return value / 16;
},
'gram': function(value){
return value * 28.3495;
},
},
'gram': {
'ounce': function(value){
return value / 28.3495;
},
'pound': function(value){
return value / 453.592;
},
},
};
var units = require('node-units');
// might as well cache this so we dont have to compute every time
// the list of available units will only change once we restart
var available = {};
var db = units.getDB();
for(var group in db){
available[group] = Object.keys(db[group]);
}
return module.exports = function(server){
server.get('/units', function(req, res){
var can_convert = {};
for(var from in conversions){
can_convert[from] = [];
for(var to in conversions[from]){
can_convert[from].push(to);
}
}
res.writeHead(200, {
'Content-Type': 'application/javascript'
});
res.end(JSON.stringify(can_convert));
return res.end(JSON.stringify(available));
});
server.get(/units\/type\/([a-zA-Z]+)/i, function(req, res){
var result = units.getUnitType(req.params[0]);
res.writeHead(200, {
'Content-Type': 'application/javascript',
});
return res.end(JSON.stringify(result));
});
server.get(/\/units\/([0-9]+(\.[0-9]+)?)\/([a-zA-Z]+)\/to\/([a-zA-Z]+)/i, function(req, res){
var value = parseFloat(req.params[0]);
var from = req.params[2].toLowerCase();
var to = req.params[3].toLowerCase();
server.get(/\/units\/(.*?)\/([a-zA-Z]+)\/to\/([a-zA-Z]+)/i, function(req, res){
var value = decodeURIComponent(req.params[0]);
var from = req.params[1];
var to = req.params[2];
var answer = {
'convert': value,
@ -146,15 +36,15 @@ return module.exports = function(server){
'result': undefined,
'error': undefined
};
if(from in conversions && to in conversions[from]){
answer['result'] = conversions[from][to](value);
} else{
answer['error'] = 'There is no conversion from ' + from + ' to ' + to;
try{
answer['result'] = units.convert(value + ' ' + from + ' to ' + to);
} catch(e){
answer['error'] = e;
}
res.writeHead(200, {
'Content-Type': 'application/javascript'
});
res.end(JSON.stringify(answer));
return res.end(JSON.stringify(answer));
});
};

+ 4
- 3
package.json View File

@ -1,6 +1,6 @@
{
"name": "brett-is-api",
"version": "0.1.1",
"version": "0.1.2",
"description": "",
"main": "lib/index.js",
"scripts": {
@ -10,10 +10,11 @@
"author": "Brett Langdon <brett@blangdon.com> (www.brett.is)",
"license": "MIT",
"dependencies": {
"restify": "~1.4.4"
"restify": "~1.4.4",
"node-units": "~0.1.0"
},
"bin": {
"brett-is-api": "bin/brett-is-api"
"brett-is-api": "bin/brett-is-api"
},
"private": true
}

Loading…
Cancel
Save