Browse Source

version bump to 0.1.1

units.convert now accepts a string for conversion rather than parameters,
the unit types are now determined rather than needed to be given,
the plurarl forms of units are now accepted,
numerizer was added to accept values such as 'forty two',
fixed some incorrect units in default unit db,
update README, added some simple examples
pull/3/head v0.1.1
Brett Langdon 13 years ago
parent
commit
beaa28c234
7 changed files with 113 additions and 27 deletions
  1. +8
    -8
      README.md
  2. +10
    -0
      examples/custom/index.js
  3. +9
    -0
      examples/custom/my_custom.units
  4. +13
    -0
      examples/simple/index.js
  5. +3
    -3
      lib/default.units
  6. +62
    -14
      lib/index.js
  7. +8
    -2
      package.json

+ 8
- 8
README.md View File

@ -15,7 +15,7 @@ npm install node-units
* `importDB(file_name, cb)` - imports a custom unit database, `cb` takes a single argument `err` * `importDB(file_name, cb)` - imports a custom unit database, `cb` takes a single argument `err`
* `importDBSync(file_name)` - the sync version of `importDB` * `importDBSync(file_name)` - the sync version of `importDB`
* `getDB()` - returns he currently used database of units as an object * `getDB()` - returns he currently used database of units as an object
* `convert(type, value, from, to)` - converts `value` of `from` to `to`, returns the result or raises exception if conversion cannot be made
* `convert(conversion_string)` - where `conversion_string` is of the form `<value> <from_unit> to <to_unit>`
### Properties ### Properties
* `types` - `types` is an object containing constants for each unit group. * `types` - `types` is an object containing constants for each unit group.
@ -31,10 +31,10 @@ npm install node-units
```javascript ```javascript
var units = require('node-units'); var units = require('node-units');
var result = units.convert('volume', 5, 'gill', 'mL');
var result = units.convert('5 gills to mL');
// result == 591.4705 // result == 591.4705
units.convert(units.types.TIME, 5, 'day', 'second');
units.convert('five days to seconds');
``` ```
### Custom Units ### Custom Units
@ -42,9 +42,9 @@ units.convert(units.types.TIME, 5, 'day', 'second');
With `node-units` you can import custom unit definitions from files defined like the following: With `node-units` you can import custom unit definitions from files defined like the following:
``` ```
group: group:
long_name,longname,ln 1ln
another_unit,au 5ln
one_more,om 2au
longname,longname,ln 1ln
anotherunit,au 5ln
onemore,om 2au
``` ```
```javascript ```javascript
@ -52,7 +52,7 @@ var units = require('node-units');
units.importDBSync('my_custom.units'); units.importDBSync('my_custom.units');
var result = units.convert(units.types.GROUP, 5, 'au', 'one_more');
var result = units.convert('five au to onemores');
// result == 10 // result == 10
``` ```
@ -68,7 +68,7 @@ var units = require('node-units');
units.importDBSync('my_custom.units'); units.importDBSync('my_custom.units');
var result = units.convert('time', 5, 'minute', 's');
var result = units.convert('5 minutes to s');
// result == 250 // result == 250
``` ```


+ 10
- 0
examples/custom/index.js View File

@ -0,0 +1,10 @@
var path = require('path');
var units = require('../../');
var db = path.join(path.dirname(module.filename), 'my_custom.units');
units.importDBSync(db);
console.log(units.convert('5 buttons to widgets'));
console.log(units.convert('30 jings to jong'));

+ 9
- 0
examples/custom/my_custom.units View File

@ -0,0 +1,9 @@
elements:
button,btn 5wg
widget,wg 2tbl
table,tbl 1tbl
things:
jing 2jabber
jabber 30jong
jong 1jong

+ 13
- 0
examples/simple/index.js View File

@ -0,0 +1,13 @@
var units = require('../../');
var result = units.convert('20 quarts to gallons');
console.log(result === 5);
try{
var value = units.convert('5 days to gallon');
console.log(value);
} catch(e){
console.log(e);
}

+ 3
- 3
lib/default.units View File

@ -7,9 +7,9 @@ time:
volume: volume:
liter,L 1L liter,L 1L
milliliter,mL 0.001L milliliter,mL 0.001L
gallon,gal 3.78541L
pint 0.473176L
quart,qt 0.946353L
gallon,gal 8pint
pint 0.125gal
quart,qt 2pint
tablespoon,tbl 0.03125pint tablespoon,tbl 0.03125pint
teaspoon,tsp 0.33333333333tbl teaspoon,tsp 0.33333333333tbl
cup 16tbl cup 16tbl


+ 62
- 14
lib/index.js View File

@ -1,11 +1,16 @@
var path = require('path'); var path = require('path');
var fs = require('fs'); var fs = require('fs');
var numerizer = require('numerizer');
var types = {}; 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 db_regex = /[a-zA-Z]+:\n([\s\t]+([a-zA-Z]+,?)+[\s\t]+[0-9.]+[a-zA-Z]+\n)+/g;
var unit_database = {}; var unit_database = {};
var forms = [/s$/i, /es$/i, /ies$/i];
var parseSection = function(section){ var parseSection = function(section){
var lines = section.split('\n'); var lines = section.split('\n');
var section_name = lines.shift(); var section_name = lines.shift();
@ -23,12 +28,12 @@ var parseSection = function(section){
var names = next[0].replace(/[\s\t]/g, '').split(','); 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 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]); value = parseFloat(value[0]);
names.forEach(function(name){ 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'); var default_db = path.join(path.dirname(module.filename), 'default.units');
importDBSync(default_db); importDBSync(default_db);
@ -92,20 +117,43 @@ return module.exports = {
getDB: function(){ getDB: function(){
return unit_database; 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){ if(result == undefined){
throw 'Conversion of ' + from + ' to ' + to + ' was not possible';
throw 'Conversion of "' + from + '" to "' + to + '" was not possible';
} else{ } else{
return result; return result;
} }


+ 8
- 2
package.json View File

@ -1,6 +1,6 @@
{ {
"name": "node-units", "name": "node-units",
"version": "0.1.0",
"version": "0.1.1",
"description": "Node.JS unit conversion library with customizable units", "description": "Node.JS unit conversion library with customizable units",
"main": "lib/index.js", "main": "lib/index.js",
"directories": { "directories": {
@ -21,5 +21,11 @@
"conversion" "conversion"
], ],
"author": "Brett Langdon <brett@blangdon.com> (www.brett.is)", "author": "Brett Langdon <brett@blangdon.com> (www.brett.is)",
"license": "MIT"
"license": "MIT",
"dependencies": {
"numerizer": "0.0.2"
},
"devDependencies": {
"mocha": "*"
}
} }

Loading…
Cancel
Save