Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalCz committed Aug 30, 2012
1 parent 5eb31ea commit a0586d8
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 3 deletions.
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2011 Michał Czapracki, budleigh.salterton@gmail.com

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
71 changes: 68 additions & 3 deletions README.md
@@ -1,4 +1,69 @@
node-si
=======
Node Si
=========

SI and IEC compatible large number parser/formatter module for node.js
The goal of this project is to provide a simple module that parses and formats
numbers to be human readable - like "10gb" -> 1e10.

This might be especially useful in command line arguments and configuration
directives where you don't want the user to pass the whole number or use the
scientific notation.

Usage
-------

Write a simple code, like:

var si = require('si'),
number = si.parse(process.argv[2]);
console.log('Your number is: ',
number);
console.log('The square of it is: ',
si.format(Math.pow(number, 2)));
console.log('The square root on the other hand is: ',
si.format(Math.pow(number, 0.5)));

Now run it:

$ node tests/basic.js 1G
Your number is: 1000000000
The square of it is: 1E
The square root on the other hand is: 31.62k

Fairly straightforward I hope.

Future
--------

I will be bringing some more parsers/formatters in later releases, like:

* IEC compliant binary multipliers like "MiB"
* Case sensitive Mbytes/mbytes (decimal vs. binary)
* Fractional SI multipliers like centi/mili etc.

Feel free to post feature requests in issues. :)

License
=========

(The MIT License)

Copyright (c) 2012 Michał Czapracki budleigh.salterton@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
44 changes: 44 additions & 0 deletions main.js
@@ -0,0 +1,44 @@
// Copyright Michał Czapracki, budleigh.salterton@gmail.com
// License: https://github.com/MichalCz/node-si-numbers/blob/master/LICENSE

/**
* Human readable number multipliers (2^(10*pos))
*/
var siMultiples = "\0kMGTPEZY",
siMulStr = siMultiples.toLowerCase(),
siRegexp = /^([\+\-]?[\d\.]+)\s?([kmgtpezy]?).*?$/;

exports.si = {};

/**
* Returns human readable number representation (up to nY - 1e24).
* @param {Number} number
* @returns {String}
*/
exports.format = exports.si.format = function(number) {
var div = 1,
res = 0.1;

for (var i = 1; i < siMultiples.length; i++) {
div *= 1e3;
res = number / div;
if (res <= 800 || i == siMultiples.length)
return ("" + res).substr(0, 5) + siMultiples[i];
}
return number+'';
};

/**
* Parses human readable numbers to Number.
* @param {String} string number representation
* @returns {Number}
*/
exports.parse = exports.si.parse = function(string) {
return +(
string
.toLowerCase()
.replace(siRegexp,
function(a, b, c) {
return (+b) * ('1e' + siMulStr.indexOf(c)*3);
}));
};
30 changes: 30 additions & 0 deletions package.json
@@ -0,0 +1,30 @@
{
"name": "sinumbers",
"version": "0.1.0",
"description": "SI and IEC compatible large number parser/formatter module for node.js",
"author": "Michal Czapracki",
"contributors": [],
"homepage": "https://github.com/MichalCz/node-si-numbers",
"licenses":[{
"type":"MIT",
"url":"https://github.com/MichalCz/node-si-numbers/blob/master/LICENSE"
}],
"keywords" : [
"kilo", "mega", "giga", "tera", "peta", "exa",
"IEC", "SI", "human readable", "large numbers"
],
"engines": {
"node": ">= 0.2.0"
},
"bugs": {
"url": "http://github.com/MichalCz/node-agenda/issues"
},
"directories": {
"lib": "./lib"
},
"repository": {
"type": "git",
"url" : "git://github.com/MichalCz/node-agenda.git"
},
"main": "./main.js"
}
6 changes: 6 additions & 0 deletions tests/basic.js
@@ -0,0 +1,6 @@
var si = require('../'),
number = si.parse(process.argv[2]);

console.log('Your number is: ', number);
console.log('The square of it is: ', si.format(Math.pow(number, 2)));
console.log('The square root on the other hand is: ', si.format(Math.pow(number, 0.5)));

0 comments on commit a0586d8

Please sign in to comment.