Browse Source

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
pull/3/head v0.1.3
Brett Langdon 13 years ago
parent
commit
082631984b
3 changed files with 51 additions and 27 deletions
  1. +24
    -8
      lib/default.units
  2. +26
    -18
      lib/index.js
  3. +1
    -1
      package.json

+ 24
- 8
lib/default.units View File

@ -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


+ 26
- 18
lib/index.js View File

@ -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;
}
},
};

+ 1
- 1
package.json View File

@ -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": {


Loading…
Cancel
Save