From 082631984b8b159c1d9378c2a304690bdef0b271 Mon Sep 17 00:00:00 2001 From: Brett Langdon Date: Tue, 19 Feb 2013 19:08:54 -0500 Subject: [PATCH] version bump, expanded default unit database also fixed a unit case issue, make sure to be consistent everywhere, also made sure that if the same abbreviation is used across unit groups that the conversion can find them --- lib/default.units | 32 ++++++++++++++++++++++++-------- lib/index.js | 44 ++++++++++++++++++++++++++------------------ package.json | 2 +- 3 files changed, 51 insertions(+), 27 deletions(-) diff --git a/lib/default.units b/lib/default.units index 5237f84..432884d 100644 --- a/lib/default.units +++ b/lib/default.units @@ -5,15 +5,31 @@ time: day 86400s week 7day +distance: + meter,m 1m + feet,ft 0.3048m + inch,in 0.0254m + yard,yd 0.9144m + mile,mi 1609.34m + +mass: + ton,t 1000000gm + gram,gm 1gm + pound,lb 16gm + ounce,oz 28.3495gm + stone,st 6350.29gm + volume: - liter,L 1L - gallon,gal 8pint - pint 0.125gal - quart,qt 2pint - tablespoon,tbl 0.03125pint - teaspoon,tsp 0.33333333333tbl - cup 16tbl - gill,gi 0.1182941L + liter,L 202.884tsp + gallon,gal 768tsp + pint 96tsp + quart,qt 192tsp + tablespoon,tbsp 3tsp + teaspoon,tsp 1tsp + cup 48tsp + ounce,oz 6tsp + gill,gi 24tsp + barrel,bbl 24192tsp storage: bit,b 1bit diff --git a/lib/index.js b/lib/index.js index b7f226c..bc3a728 100644 --- a/lib/index.js +++ b/lib/index.js @@ -114,7 +114,7 @@ var parseSection = function(section){ var names = next[0].replace(/[\s\t]/g, '').split(','); var value = next[1].replace(/[\s\t]/g, '').match(/([0-9.]+)|([a-zA-Z]+)/g); - var unit = value[1].toLowerCase(); + var unit = value[1]; value = parseFloat(value[0]); names.forEach(function(name){ @@ -204,18 +204,20 @@ var get_modifier = function(unit){ }; var determine_type = function(variations){ + var possible = []; for(var i in types){ for(var k in variations){ if(unit_database[types[i]][variations[k]] !== undefined){ - return {'type': types[i], 'unit': variations[k], 'modifier': 1}; + possible.push({'type': types[i], 'unit': variations[k], 'modifier': 1}); } else{ var modified = get_modifier(variations[k]); if(unit_database[types[i]][modified.unit] !== undefined){ - return {'type': types[i], 'unit': modified.unit, 'modifier': modified.modifier}; + posible.push({'type': types[i], 'unit': modified.unit, 'modifier': modified.modifier}); } } } } + return possible; }; var default_db = path.join(path.dirname(module.filename), 'default.units'); @@ -237,7 +239,7 @@ return module.exports = { return type; }, convert: function(str){ - var type, value, from, to; + var value, from, to; var max_tries = 5; var parts = conversion_regex.exec(str); @@ -250,33 +252,39 @@ return module.exports = { var from_variations = get_variations(parts[2]); var to_variations = get_variations(parts[3]); - var from_type = determine_type(from_variations); - var to_type = determine_type(to_variations); + var from_types = determine_type(from_variations); + var to_types = determine_type(to_variations); - if(!from_type){ + if(!from_types.length){ throw 'Unknown unit: "' + parts[2] + '"'; return; } - if(!to_type){ + if(!to_types.length){ throw 'Unknown unit: "' + parts[3] + '"'; return; } - if(from_type.type !== to_type.type){ - throw 'Units "' + from_type.unit + '" and "' + to_type.unit + '" do not belong in the same unit group'; - return; + for(var i in from_types){ + for(var k in to_types){ + if(from_types[i].type === to_types[k].type){ + from = from_types[i]; + to = to_types[i]; + break; + } + } } - value = value * from_type.modifier; - type = from_type.type; - from = from_type.unit; - to = to_type.unit; + if(!from || !to){ + throw 'Units "' + parts[2] + '" and "' + parts[3] + '" do not belong in the same unit group'; + return; + } - var result = convert(type, value, from, to, max_tries); + value = value * from.modifier; + var result = convert(from.type, value, from.unit, to.unit, max_tries); if(result == undefined){ - throw 'Conversion of "' + from + '" to "' + to + '" was not possible'; + throw 'Conversion of "' + from.unit + '" to "' + to.unit + '" was not possible'; } else{ - return result / to_type.modifier; + return result / to.modifier; } }, }; diff --git a/package.json b/package.json index 3ba7718..01b4da1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "node-units", - "version": "0.1.2", + "version": "0.1.3", "description": "Node.JS unit conversion library with customizable units", "main": "lib/index.js", "directories": {