├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.js ├── package.json ├── test └── index.js └── units.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 18 | .grunt 19 | 20 | # node-waf configuration 21 | .lock-wscript 22 | 23 | # Compiled binary addons (http://nodejs.org/api/addons.html) 24 | build/Release 25 | 26 | # Dependency directory 27 | # https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git 28 | node_modules 29 | 30 | # Optional npm cache directory 31 | .npm 32 | 33 | # Optional REPL history 34 | .node_repl_history 35 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.11" 4 | - "0.12" 5 | - "4" 6 | - "5" 7 | addons: 8 | apt: 9 | sources: 10 | - ubuntu-toolchain-r-test 11 | env: 12 | matrix: 13 | - TEST_SUITE=test 14 | matrix: 15 | fast_finish: true 16 | include: 17 | - os: linux 18 | node_js: "4" 19 | env: TEST_SUITE=coveralls 20 | - os: linux 21 | node_js: "4" 22 | env: TEST_SUITE=lint 23 | script: npm run $TEST_SUITE 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Alex Beregszaszi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ethereumjs-units 2 | 3 | [![NPM Package](https://img.shields.io/npm/v/ethereumjs-units.svg?style=flat-square)](https://www.npmjs.org/package/ethereumjs-units) 4 | [![Build Status](https://img.shields.io/travis/ethereumjs/ethereumjs-units.svg?branch=master&style=flat-square)](https://travis-ci.org/ethereumjs/ethereumjs-units) 5 | [![Coverage Status](https://img.shields.io/coveralls/ethereumjs/ethereumjs-units.svg?style=flat-square)](https://coveralls.io/r/ethereumjs/ethereumjs-units) 6 | [![Gitter](https://img.shields.io/gitter/room/ethereum/ethereumjs-lib.svg?style=flat-square)](https://gitter.im/ethereum/ethereumjs-lib) or #ethereumjs on freenode 7 | 8 | Unit conversion utility. 9 | 10 | There are two methods: 11 | 12 | - `convert(value, unitFrom, unitTo)` - convert a value between two units 13 | - `lazyConvert(value, unitTo)` - include unit type in the input and the output 14 | 15 | The `value` and the output in all cases is a string. 16 | 17 | ## Examples 18 | 19 | ```js 20 | Units.convert('1', 'eth', 'wei') // '1000000000000000000' 21 | Units.convert('1', 'wei', 'eth') // '0.000000000000000001' 22 | Units.convert('1', 'finney', 'eth') // '0.001' 23 | 24 | Units.lazyConvert('1 eth', 'wei') // '1000000000000000000 wei' 25 | Units.lazyConvert('1 wei', 'eth') // '0.000000000000000001 eth' 26 | Units.lazyConvert('1 finney', 'eth') // '0.001 eth' 27 | ``` 28 | 29 | ## Units 30 | 31 | Units are defined in `units.json`. It is compatible with [web3.js](https://github.com/ethereum/web3.js) and additionally includes `ETH`. 32 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var BigNumber = require('bignumber.js') 2 | 3 | var Units = {} 4 | 5 | var rawUnits = require('./units.json') 6 | var units = {} 7 | 8 | Object.keys(rawUnits).map(function (unit) { 9 | units[unit] = new BigNumber(rawUnits[unit], 10) 10 | }) 11 | 12 | Units.units = rawUnits 13 | 14 | var re = RegExp(/^[0-9]+\.?[0-9]*$/) 15 | 16 | Units.convert = function (value, from, to) { 17 | if (!re.test(value)) { 18 | throw new Error('Unsupported value') 19 | } 20 | 21 | from = from.toLowerCase() 22 | if (!units[from]) { 23 | throw new Error('Unsupported input unit') 24 | } 25 | 26 | to = to.toLowerCase() 27 | if (!units[to]) { 28 | throw new Error('Unsupported output unit') 29 | } 30 | 31 | return new BigNumber(value, 10).mul(units[from]).round(0, BigNumber.ROUND_DOWN).div(units[to]).toString(10) 32 | } 33 | 34 | Units.lazyConvert = function (value, to) { 35 | var tmp = value.split(' ') 36 | if (tmp.length !== 2) { 37 | throw new Error('Invalid input') 38 | } 39 | return Units.convert(tmp[0], tmp[1], to) + ' ' + to 40 | } 41 | 42 | module.exports = Units 43 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ethereumjs-units", 3 | "version": "0.2.0", 4 | "description": "Ethereum unit conversion.", 5 | "main": "index.js", 6 | "scripts": { 7 | "coverage": "istanbul cover _mocha", 8 | "coveralls": "npm run coverage && coveralls ", 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/ethereumjs/ethereumjs-units/issues" 26 | }, 27 | "homepage": "https://github.com/ethereumjs/ethereumjs-units#readme", 28 | "dependencies": { 29 | "bignumber.js": "^2.3.0" 30 | }, 31 | "devDependencies": { 32 | "coveralls": "^2.11.4", 33 | "istanbul": "^0.4.1", 34 | "mocha": "^2.3.4", 35 | "standard": "^5.4.1" 36 | }, 37 | "standard": { 38 | "globals": [ 39 | "describe", 40 | "it" 41 | ] 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert') 2 | var Units = require('../') 3 | 4 | describe('convert', function () { 5 | it('should work for big unit to small unit', function () { 6 | assert.equal(Units.convert('1', 'eth', 'wei'), '1000000000000000000') 7 | assert.equal(Units.convert('20', 'gwei', 'wei'), '20000000000') 8 | assert.equal(Units.convert('20.05', 'gwei', 'wei'), '20050000000') 9 | assert.equal(Units.convert('20.005', 'kwei', 'wei'), '20005') 10 | assert.equal(Units.convert('20.0005', 'kwei', 'wei'), '20000') 11 | assert.equal(Units.convert('1', 'tether', 'eth'), '1000000000000') 12 | assert.equal(Units.convert('1', 'tether', 'wei'), '1000000000000000000000000000000') 13 | }) 14 | it('should work for small unit to big unit', function () { 15 | assert.equal(Units.convert('1', 'wei', 'eth'), '0.000000000000000001') 16 | assert.equal(Units.convert('0.5', 'wei', 'eth'), '0') 17 | assert.equal(Units.convert('0.0005', 'kwei', 'eth'), '0') 18 | assert.equal(Units.convert('1', 'finney', 'eth'), '0.001') 19 | assert.equal(Units.convert('20', 'gwei', 'eth'), '0.00000002') 20 | assert.equal(Units.convert('1', 'eth', 'tether'), '0.000000000001') 21 | // XXX: precision loss 22 | assert.equal(Units.convert('1', 'wei', 'tether'), '0') 23 | }) 24 | it('should fail on invalid input units', function () { 25 | assert.throws(function () { 26 | Units.convert('1', 'random', 'wei') 27 | }, /^Error: Unsupported input unit$/) 28 | }) 29 | it('should fail on invalid output units', function () { 30 | assert.throws(function () { 31 | Units.convert('1', 'wei', 'random') 32 | }, /^Error: Unsupported output unit$/) 33 | }) 34 | it('should fail on non-decimal input', function () { 35 | assert.throws(function () { 36 | Units.convert('1,00', 'wei', 'random') 37 | }, /^Error: Unsupported value$/) 38 | 39 | assert.throws(function () { 40 | Units.convert('test', 'wei', 'random') 41 | }, /^Error: Unsupported value$/) 42 | }) 43 | }) 44 | 45 | describe('lazyConvert', function () { 46 | it('should work for big unit to small unit', function () { 47 | assert.equal(Units.lazyConvert('1 eth', 'wei'), '1000000000000000000 wei') 48 | assert.equal(Units.lazyConvert('20 gwei', 'wei'), '20000000000 wei') 49 | }) 50 | it('should work for small unit to big unit', function () { 51 | assert.equal(Units.lazyConvert('1 wei', 'eth'), '0.000000000000000001 eth') 52 | assert.equal(Units.lazyConvert('1 finney', 'eth'), '0.001 eth') 53 | assert.equal(Units.lazyConvert('20 gwei', 'eth'), '0.00000002 eth') 54 | }) 55 | it('should fail on invalid input', function () { 56 | assert.throws(function () { 57 | Units.lazyConvert('1') 58 | }, /^Error: Invalid input$/) 59 | 60 | assert.throws(function () { 61 | Units.lazyConvert('1 eth wei') 62 | }, /^Error: Invalid input$/) 63 | }) 64 | it('should fail on non-decimal input', function () { 65 | assert.throws(function () { 66 | Units.convert('1,00', 'wei', 'random') 67 | }, /^Error: Unsupported value$/) 68 | 69 | assert.throws(function () { 70 | Units.convert('test', 'wei', 'random') 71 | }, /^Error: Unsupported value$/) 72 | }) 73 | }) 74 | 75 | describe('units export', function () { 76 | it('should be available', function () { 77 | assert.equal(typeof Units.units, 'object') 78 | }) 79 | it('should contain strings', function () { 80 | assert.equal(typeof Units.units['wei'], 'string') 81 | }) 82 | }) 83 | -------------------------------------------------------------------------------- /units.json: -------------------------------------------------------------------------------- 1 | { 2 | "wei": "1", 3 | "kwei": "1000", 4 | "Kwei": "1000", 5 | "babbage": "1000", 6 | "femtoether": "1000", 7 | "mwei": "1000000", 8 | "Mwei": "1000000", 9 | "lovelace": "1000000", 10 | "picoether": "1000000", 11 | "gwei": "1000000000", 12 | "Gwei": "1000000000", 13 | "shannon": "1000000000", 14 | "nanoether": "1000000000", 15 | "nano": "1000000000", 16 | "szabo": "1000000000000", 17 | "microether": "1000000000000", 18 | "micro": "1000000000000", 19 | "finney": "1000000000000000", 20 | "milliether": "1000000000000000", 21 | "milli": "1000000000000000", 22 | "ether": "1000000000000000000", 23 | "eth": "1000000000000000000", 24 | "kether": "1000000000000000000000", 25 | "grand": "1000000000000000000000", 26 | "mether": "1000000000000000000000000", 27 | "gether": "1000000000000000000000000000", 28 | "tether": "1000000000000000000000000000000" 29 | } 30 | --------------------------------------------------------------------------------