xrb-prettify
Advanced tools
Comparing version 1.0.0 to 2.0.1
@@ -1,1 +0,1 @@ | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:!0}),exports.xrbPrettify=void 0;var _bignumber=require('bignumber.js'),_bignumber2=_interopRequireDefault(_bignumber),_numeral=require('numeral'),_numeral2=_interopRequireDefault(_numeral);function _interopRequireDefault(a){return a&&a.__esModule?a:{default:a}}function formatXrbValueWithDecimalPoint(a){return new _bignumber2.default(a||0).dividedBy(new _bignumber2.default(10).pow(6)).toFixed()}var formatXrbValueWithCommas=function(a){return(0,_numeral2.default)(a).format('0,0.[00000000000]')};function xrbPrettify_(a){var b=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},c=b.commas,d=b.decimals,e=formatXrbValueWithDecimalPoint(a);return void 0===d||d||(e=Math.floor(e)),void 0===c||c?formatXrbValueWithCommas(e):parseFloat(e)}exports.default=xrbPrettify_;var xrbPrettify=exports.xrbPrettify=xrbPrettify_; | ||
'use strict';var _units;Object.defineProperty(exports,'__esModule',{value:!0});function _defineProperty(a,b,c){return b in a?Object.defineProperty(a,b,{value:c,enumerable:!0,configurable:!0,writable:!0}):a[b]=c,a}var UNIT_RAW='raw',UNIT_UXRB='uxrb',UNIT_MXRB='mxrb',UNIT_XRB='xrb',UNIT_KXRB='kxrb',UNIT_MXRB_='Mxrb',UNIT_GXRB='Gxrb',units=(_units={},_defineProperty(_units,UNIT_RAW,{places:0}),_defineProperty(_units,UNIT_UXRB,{places:18}),_defineProperty(_units,UNIT_MXRB,{places:21}),_defineProperty(_units,UNIT_XRB,{places:24}),_defineProperty(_units,UNIT_KXRB,{places:27}),_defineProperty(_units,UNIT_MXRB_,{places:30}),_defineProperty(_units,UNIT_GXRB,{places:33}),_units);function formatXrbValue(a,b){var c=b.commas,d=b.decimals,e=b.decimalPlaces,f=b.unit,g=units[f].places,h=a.substring(a.length-g),j=a.substring(0,a.length-h.length),k=[];if(c){for(var l=0,m=j.length-1;0<=m;m--)3==l&&0<=m&&(k.unshift(','),l=0),k.unshift(j[m]),l++;j=k.join('')}return f===UNIT_RAW?j:d?j+'.'+(0===e?h:h.substring(0,e)):j}function xrbPrettify_(a){var b=1<arguments.length&&arguments[1]!==void 0?arguments[1]:{},c=b.unit,d=c===void 0?'Mxrb':c,e=b.commas,f=b.decimals,g=b.decimalPlaces,h=g===void 0?6:g;if('string'!=typeof a)return console.error('xrb-prettify must be supplied with a string. Please check your types.'),'XRB_PRETTIFY_DISPLAY_ERROR';return formatXrbValue(a,{commas:!(e!==void 0)||e,decimals:!(f!==void 0)||f,decimalPlaces:h,unit:d})}exports.default=xrbPrettify_;var xrbPrettify=exports.xrbPrettify=xrbPrettify_; |
{ | ||
"name": "xrb-prettify", | ||
"version": "1.0.0", | ||
"version": "2.0.1", | ||
"description": "Format RaiBlocks raw amounts (xrb) to be more human-readable. Adds decimal point and commas where necessary.", | ||
@@ -27,6 +27,3 @@ "main": "dist/index.js", | ||
"homepage": "https://github.com/kilkelly/xrb-prettify#readme", | ||
"dependencies": { | ||
"bignumber.js": "^4.1.0", | ||
"numeral": "^2.0.6" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
@@ -33,0 +30,0 @@ "babel-cli": "^6.26.0", |
# xrb-prettify | ||
Format RaiBlocks raw amounts (xrb) to be more human-readable. Adds decimal point and commas where necessary. | ||
Prettify / Format RaiBlocks (XRB) Raw amounts to be more human-readable. Adds decimal point and commas where necessary. Raw amounts are commonly returned when interacting with a RaiBlocks node (rai_node) | ||
e.g. 1002513383 becomes 1,002.513383 | ||
e.g. "2100176034320859259343606608761791" becomes "2,100.176034" | ||
@@ -27,43 +27,75 @@ ## Install | ||
```sh | ||
xrbPrettify(1002513383) | ||
// "1,002.513383" | ||
xrbPrettify("2100176034320859259343606608761791") | ||
// "2,100.176034" | ||
``` | ||
## Note | ||
**xrb-prettify** works with strings only. If you need to deal with numbers you can try converting returned results to a numeric format using parseInt or parseFloat where applicable. | ||
## API | ||
### xrbPrettify(xrbValue, config <optional>) | ||
### xrbPrettify(raw, config <optional>) | ||
**Parameters** | ||
*xrbValue* (number) - The raw XRB amount, usually returned when performing API calls to a RaiBlocks node. | ||
**raw** *(number)* - The Raw amount, returned when performing API calls to a RaiBlocks node (rai_node). | ||
*config* (object) <optional> - Configuration for the returned result. The configurations are: | ||
* commas (boolean) - Choose whether or not to display commas where necessary e.g. 1,000,000.000000. Default: true | ||
* decimals (boolean) - Choose whether or not to display decimal places. Default: true | ||
**config** *(object)* <optional> - Configuration for the returned result. The configurations are: | ||
* **unit** *(Possible values: 'raw', 'uxrb', 'mxrb', 'xrb', 'kxrb', 'Mxrb', 'Gxrb')* - Specify which RaiBlocks unit is used to prettify the Raw amount. Default unit is 'Mxrb'. | ||
* **commas** *(boolean)* - Choose whether or not to display commas where necessary e.g. "1,000,000.000000". Default: true | ||
* **decimals** *(boolean)* - Choose whether or not to display decimal places. Default: true | ||
* **decimalPlaces** *(number)* - Number of decimal places to display. Default is 6. Enter 0 to show all decimal places. | ||
#### Examples | ||
Prettify an XRB amount | ||
Prettify a Raw amount | ||
```sh | ||
xrbPrettify(1002513383) | ||
// Result: "1,002.513383" | ||
// returns a string | ||
xrbPrettify("2100176034320859259343606608761791") | ||
// Result: "2,100.176034" | ||
``` | ||
Prettify an XRB amount without commas | ||
Prettify a Raw amount using the 'xrb' unit (overridiing the default unit 'Mxrb') | ||
```sh | ||
xrbPrettify(1002513383, { commas: false }) | ||
// Result: 1002.513383 | ||
// returns a number | ||
xrbPrettify("2100176034320859259343606608761791", { unit: 'xrb' }) | ||
// Result: "2,100,176,034.320859" | ||
``` | ||
Prettify an XRB amount without decimal places | ||
Prettify a Raw amount without commas | ||
```sh | ||
xrbPrettify(1002513383, { decimals: false }) | ||
// Result: "1,002" | ||
xrbPrettify("2100176034320859259343606608761791", { commas: false }) | ||
// Result: "2100.176034" | ||
``` | ||
Prettify a Raw amount without decimal places | ||
```sh | ||
xrbPrettify("2100176034320859259343606608761791", { decimals: false }) | ||
// Result: "2,100" | ||
``` | ||
Prettify a Raw amount with 10 decimal places (overridiing the default of 6 decimal places) | ||
```sh | ||
xrbPrettify("2100176034320859259343606608761791", { decimalPlaces: 10 }) | ||
// Result: "2,100.1760343208" | ||
``` | ||
Prettify a Raw amount using a combination of configurations | ||
```sh | ||
xrbPrettify("2100176034320859259343606608761791", | ||
{ | ||
commas: false, | ||
decimals: true, | ||
unit: 'xrb', | ||
decimalPlaces: 10 | ||
} | ||
) | ||
// Result: "2100176034.3208592593" | ||
``` | ||
## Tests | ||
@@ -70,0 +102,0 @@ |
107
src/index.js
'use strict' | ||
import BigNumber from 'bignumber.js' | ||
import numeral from 'numeral' | ||
const UNIT_RAW = 'raw' | ||
const UNIT_UXRB = 'uxrb' | ||
const UNIT_MXRB = 'mxrb' | ||
const UNIT_XRB = 'xrb' | ||
const UNIT_KXRB = 'kxrb' | ||
const UNIT_MXRB_ = 'Mxrb' | ||
const UNIT_GXRB = 'Gxrb' | ||
// --------------------------------------------------- | ||
function formatXrbValueWithDecimalPoint (xrbValue) { | ||
return new BigNumber(xrbValue || 0).dividedBy(new BigNumber(10).pow(6)).toFixed() | ||
const units = { | ||
[UNIT_RAW]: { | ||
places: 0 | ||
}, | ||
[UNIT_UXRB]: { | ||
places: 18 | ||
}, | ||
[UNIT_MXRB]: { | ||
places: 21 | ||
}, | ||
[UNIT_XRB]: { | ||
places: 24 | ||
}, | ||
[UNIT_KXRB]: { | ||
places: 27 | ||
}, | ||
[UNIT_MXRB_]: { | ||
places: 30 | ||
}, | ||
[UNIT_GXRB]: { | ||
places: 33 | ||
} | ||
} | ||
@@ -14,4 +37,40 @@ | ||
const formatXrbValueWithCommas = function (xrbValue) { | ||
return numeral(xrbValue).format('0,0.[00000000000]') | ||
function formatXrbValue (xrbValue, | ||
{ | ||
commas, | ||
decimals, | ||
decimalPlaces, | ||
unit | ||
}) { | ||
let unitPlaces = units[unit].places | ||
let fractionalPartOfXrbValue = xrbValue.substring(xrbValue.length - unitPlaces) | ||
let integerPartOfXrbValue = xrbValue.substring(0, xrbValue.length - fractionalPartOfXrbValue.length) | ||
let integerPartOfXrbValueWithCommas = [] | ||
if (commas) { | ||
let commasInsertTrigger = 0 // when equal to 3 then this is the trigger to insert a comma | ||
for (let i = integerPartOfXrbValue.length - 1; i >= 0; i--) { | ||
// Comma insert has been triggered and we are not at the beginning of the integer part | ||
if (commasInsertTrigger === 3 && i >= 0) { | ||
integerPartOfXrbValueWithCommas.unshift(',') | ||
commasInsertTrigger = 0 | ||
} | ||
integerPartOfXrbValueWithCommas.unshift(integerPartOfXrbValue[i]) | ||
commasInsertTrigger++ | ||
} | ||
integerPartOfXrbValue = integerPartOfXrbValueWithCommas.join('') | ||
} | ||
if (unit === UNIT_RAW) { | ||
return integerPartOfXrbValue | ||
} else { | ||
if (decimals) { | ||
return integerPartOfXrbValue + '.' + (decimalPlaces === 0 ? fractionalPartOfXrbValue : fractionalPartOfXrbValue.substring(0, decimalPlaces)) | ||
} else { | ||
return integerPartOfXrbValue | ||
} | ||
} | ||
} | ||
@@ -22,21 +81,27 @@ | ||
function xrbPrettify_ ( | ||
rawXrbValue, | ||
raw, | ||
{ | ||
unit = 'Mxrb', | ||
commas = true, // include commas? e.g. '4,002.513383' | ||
decimals = true // include decimal places? e.g. 4002.513383 | ||
decimals = true, // include decimal places? e.g. 4002.513383 | ||
decimalPlaces = 6 | ||
} = {} | ||
) { | ||
let rawXrbValueInProcess = formatXrbValueWithDecimalPoint(rawXrbValue) | ||
// Don't show decimal places | ||
if (!decimals) { | ||
rawXrbValueInProcess = Math.floor(rawXrbValueInProcess) | ||
// Check that the raw value we are working with is a string | ||
if (typeof raw !== 'string') { | ||
console.error('xrb-prettify must be supplied with a string. Please check your types.') | ||
return 'XRB_PRETTIFY_DISPLAY_ERROR' | ||
} | ||
// Include commas in the value (outputs a string) | ||
if (commas) { | ||
return formatXrbValueWithCommas(rawXrbValueInProcess) | ||
} else { // No commas in the value | ||
return parseFloat(rawXrbValueInProcess) | ||
} | ||
// let importantRawDigits = extractImportantDigitsFromRaw(raw) | ||
let importantRawDigits = raw | ||
// let rawXrbValueInProcess = formatXrbValueWithDecimalPoint(importantRawDigits, unit) | ||
let rawXrbValueInProcess = importantRawDigits | ||
return formatXrbValue(rawXrbValueInProcess, { | ||
commas, | ||
decimals, | ||
decimalPlaces, | ||
unit | ||
}) | ||
} | ||
@@ -43,0 +108,0 @@ |
68
test.js
var test = require('tape') | ||
var xrbPrettify = require('./dist/index').xrbPrettify | ||
test('Prettify amount', function (t) { | ||
test('Handle incorrect type', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify(4002513383), '4,002.513383') | ||
t.equal(xrbPrettify(235580100176034320859259343606608761791), 'XRB_PRETTIFY_DISPLAY_ERROR') | ||
}) | ||
test('Prettify amount, no commas', function (t) { | ||
test('Prettify amount (default unit: Mxrb)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify(4002513383, { commas: false }), 4002.513383) | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791'), '235,580,100.176034') | ||
}) | ||
test('Prettify small amount', function (t) { | ||
test('Prettify amount, no commas (default unit: Mxrb)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify(123), '0.000123') | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791', { commas: false }), '235580100.176034') | ||
}) | ||
test('Prettify small amount, no commas', function (t) { | ||
test('Prettify amount, no decimal places (default unit: Mxrb)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify(123, { commas: false }), 0.000123) | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791', { decimals: false }), '235,580,100') | ||
}) | ||
test('Prettify amount, no decimal places', function (t) { | ||
test('Prettify amount, no commas, no decimal places (default unit: Mxrb)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify(4002513383, { decimals: false }), '4,002') | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791', { commas: false, decimals: false }), '235580100') | ||
}) | ||
test('Prettify small amount, no decimal places', function (t) { | ||
test('Prettify amount, 10 decimal places (default unit: Mxrb)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify(123, { decimals: false }), '0') | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791', { decimalPlaces: 10 }), '235,580,100.1760343208') | ||
}) | ||
test('Prettify amount, no commas, no decimal places', function (t) { | ||
test('Prettify amount, all decimal places (default unit: Mxrb)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify(4002513383, { commas: false, decimals: false }), 4002) | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791', { decimalPlaces: 0 }), '235,580,100.176034320859259343606608761791') | ||
}) | ||
test('Prettify small amount, no commas, no decimal places', function (t) { | ||
test('Prettify amount, no commas, all decimal places (default unit: Mxrb)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify(123, { commas: false, decimals: false }), 0) | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791', { commas: false, decimalPlaces: 0 }), '235580100.176034320859259343606608761791') | ||
}) | ||
test('Prettify amount (unit: raw)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791', { unit: 'raw' }), '235,580,100,176,034,320,859,259,343,606,608,761,791') | ||
}) | ||
test('Prettify amount, no commas (unit: raw)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791', { commas: false, unit: 'raw' }), '235580100176034320859259343606608761791') | ||
}) | ||
test('Prettify amount (unit: uxrb)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791', { unit: 'uxrb' }), '235,580,100,176,034,320,859.259343') | ||
}) | ||
test('Prettify amount (unit: mxrb)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791', { unit: 'mxrb' }), '235,580,100,176,034,320.859259') | ||
}) | ||
test('Prettify amount (unit: xrb)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791', { unit: 'xrb' }), '235,580,100,176,034.320859') | ||
}) | ||
test('Prettify amount (unit: kxrb)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791', { unit: 'kxrb' }), '235,580,100,176.034320') | ||
}) | ||
test('Prettify amount (unit: Gxrb)', function (t) { | ||
t.plan(1) | ||
t.equal(xrbPrettify('235580100176034320859259343606608761791', { unit: 'Gxrb' }), '235,580.100176') | ||
}) | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
12605
0
157
0
117
- Removedbignumber.js@^4.1.0
- Removednumeral@^2.0.6
- Removedbignumber.js@4.1.0(transitive)
- Removednumeral@2.0.6(transitive)