From a0586d8dde8c0f2903d156d3dcf6b352ec504357 Mon Sep 17 00:00:00 2001 From: Michal Czapracki Date: Thu, 30 Aug 2012 23:33:49 +0200 Subject: [PATCH] Initial commit --- LICENSE | 22 ++++++++++++++++ README.md | 71 +++++++++++++++++++++++++++++++++++++++++++++++--- main.js | 44 +++++++++++++++++++++++++++++++ package.json | 30 +++++++++++++++++++++ tests/basic.js | 6 +++++ 5 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 LICENSE create mode 100644 main.js create mode 100644 package.json create mode 100644 tests/basic.js diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c599140 --- /dev/null +++ b/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. \ No newline at end of file diff --git a/README.md b/README.md index 9a76143..bb777e2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,69 @@ -node-si -======= +Node Si +========= -SI and IEC compatible large number parser/formatter module for node.js \ No newline at end of file +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. diff --git a/main.js b/main.js new file mode 100644 index 0000000..8d7ab9c --- /dev/null +++ b/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); + })); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..d960d39 --- /dev/null +++ b/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" +} \ No newline at end of file diff --git a/tests/basic.js b/tests/basic.js new file mode 100644 index 0000000..38103bc --- /dev/null +++ b/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)));