@ -1,11 +1,16 @@
var path = require ( 'path' ) ;
var fs = require ( 'fs' ) ;
var numerizer = require ( 'numerizer' ) ;
var types = { } ;
var conversion_regex = /^(.*?)\s([a-zA-Z]+)\sto\s([a-zA-Z]+)$/i ;
var db_regex = /[a-zA-Z]+:\n([\s\t]+([a-zA-Z]+,?)+[\s\t]+[0-9.]+[a-zA-Z]+\n)+/g ;
var unit_database = { } ;
var forms = [ /s$/i , /es$/i , /ies$/i ] ;
var parseSection = function ( section ) {
var lines = section . split ( '\n' ) ;
var section_name = lines . shift ( ) ;
@ -23,12 +28,12 @@ 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 ] ;
var unit = value [ 1 ] . toLowerCase ( ) ;
value = parseFloat ( value [ 0 ] ) ;
names . forEach ( function ( name ) {
unit_database [ section_name ] [ name ] = { 'unit' : unit ,
'value' : value } ;
unit_database [ section_name ] [ name . toLowerCase ( ) ] = { 'unit' : unit ,
'value' : value } ;
} ) ;
}
} ;
@ -81,6 +86,26 @@ var convert = function(type, value, from, to, max_calls){
} ;
var get_variations = function ( unit ) {
var variations = [ unit ] ;
forms . forEach ( function ( form ) {
if ( unit . match ( form ) ) {
variations . push ( unit . replace ( form , '' ) ) ;
}
} ) ;
return variations ;
} ;
var determine_type = function ( variations ) {
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 ] } ;
}
}
}
} ;
var default_db = path . join ( path . dirname ( module . filename ) , 'default.units' ) ;
importDBSync ( default_db ) ;
@ -92,20 +117,43 @@ return module.exports = {
getDB : function ( ) {
return unit_database ;
} ,
convert : function ( type , value , from , to ) {
type = type . toLowerCase ( ) ;
if ( unit_database [ type ] == undefined ) {
throw 'Unit group ' + type + ' does not exist' ;
} else if ( unit_database [ type ] [ from ] == undefined ) {
throw 'Unit ' + from + ' does not belong to unit group ' + type ;
} else if ( unit_database [ type ] [ to ] == undefined ) {
throw 'Unit ' + to + ' does not belong to unit group ' + type ;
convert : function ( str ) {
var type , value , from , to , max_tries ;
var parts = conversion_regex . exec ( str ) ;
if ( parts == null ) {
throw 'Invalid conversion string: "' + str + '", expected "<value> <from_unit> to <to_unit>"' ;
return ;
}
value = parseFloat ( numerizer ( parts [ 1 ] ) ) ;
var from_variations = get_variations ( parts [ 2 ] . toLowerCase ( ) ) ;
var to_variations = get_variations ( parts [ 3 ] . toLowerCase ( ) ) ;
var from_type = determine_type ( from_variations ) ;
var to_type = determine_type ( to_variations ) ;
if ( ! from_type ) {
throw 'Unknown unit: "' + parts [ 2 ] + '"' ;
return ;
}
if ( ! to_type ) {
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 ;
}
type = from_type . type ;
from = from_type . unit ;
to = to_type . unit ;
var result = convert ( type , value , from , to , 5 ) ;
var result = convert ( type , value , from , to , max_tries ) ;
if ( result == undefined ) {
throw 'Conversion of ' + from + ' to ' + to + ' was not possible' ;
throw 'Conversion of "' + from + '" to "' + to + '" was not possible';
} else {
return result ;
}