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