biguint-format
Advanced tools
Comparing version 0.1.1 to 0.2.0
51
index.js
@@ -20,3 +20,3 @@ /** | ||
function format (buffer, base, options) { | ||
module.exports = function (buffer, base, options) { | ||
var buffer = _toBuffer(buffer), formatter = FORMATS[base]; | ||
@@ -62,3 +62,5 @@ | ||
return _split(digits.toString('ascii', d), options.groupsize, options.delimiter); | ||
return _pad( | ||
_split(digits.toString('ascii', d), options.groupsize, options.delimiter), | ||
'', options.padstr, options.size); | ||
} | ||
@@ -68,3 +70,4 @@ | ||
var options = options || {}, digits = new Array(buffer.length) | ||
, size = options.groupsize || -1, num, result; | ||
, size = options.groupsize || -1, num, result | ||
, prefix = options.prefix || '', output; | ||
@@ -75,12 +78,16 @@ if((options.format || 'BE') !== 'BE') _reverseBuffer(buffer); | ||
num = buffer[i].toString(2); | ||
digits[i] = '00000000'.slice(0, 8 - num.length) + buffer[i].toString(2) | ||
digits[i] = '00000000'.slice(0, 8 - num.length) + buffer[i].toString(2); | ||
}; | ||
if(size <= 0) { | ||
result = digits.join(''); | ||
} else { | ||
result = _split(digits.join(''), size, options.delimiter) | ||
output = digits.join(''); | ||
if(options.trim) { | ||
output = output.substr(output.indexOf('1')); | ||
} | ||
return (options.prefix || '') + result; | ||
if(size > 0) { | ||
output = _split(output, size, options.delimiter) | ||
} | ||
return prefix + _pad(output, prefix, options.padstr, options.size); | ||
} | ||
@@ -93,3 +100,3 @@ | ||
function toHexString (buffer, options) { | ||
var options = options || {}, digits, idx; | ||
var options = options || {}, prefix = options.prefix || '', digits, idx; | ||
@@ -104,4 +111,4 @@ if((options.format || 'BE') !== 'BE') _reverseBuffer(buffer); | ||
return (options.prefix || '') | ||
+ _split(digits.slice(idx), options.groupsize, options.delimiter); | ||
return prefix + _pad( | ||
_split(digits.slice(idx), options.groupsize, options.delimiter), prefix, options.padstr, options.size); | ||
} | ||
@@ -114,3 +121,3 @@ | ||
, digits = new Buffer(shifts) | ||
, idx = -1; | ||
, idx = -1, prefix = options.prefix || '' | ||
@@ -136,4 +143,4 @@ digits.fill(0); // reset digits buffer | ||
return (options.prefix || '') | ||
+ _split(digits.toString('ascii', idx), options.groupsize, options.delimiter) | ||
return prefix + _pad( | ||
_split(digits.toString('ascii', idx), options.groupsize, options.delimiter), prefix, options.padstr, options.size); | ||
} | ||
@@ -149,2 +156,10 @@ | ||
function _pad (str, prefix, pad, size) { | ||
if ('undefined' === typeof pad || 'undefined' === typeof size || pad.length === 0 || str.length + prefix.length >= size) { | ||
return str; | ||
} | ||
var padlen = size - str.length - prefix.length; | ||
return new Array(Math.ceil(padlen / pad.length) + 1).join(pad).substr(0, padlen) + str; | ||
} | ||
function _toAsciiDigits (buffer, offset) { | ||
@@ -227,8 +242,2 @@ for (var i = offset; i < buffer.length; i++) { | ||
}; | ||
} | ||
if (typeof module !== "undefined") { | ||
module.exports = { | ||
'format': format | ||
} | ||
} |
{ | ||
"name": "biguint-format", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "An arbitrary length unsigned integer formatter library for Node.js", | ||
@@ -26,4 +26,11 @@ "main": "index.js", | ||
], | ||
"author": "Tom Pawlak", | ||
"license": "MIT", | ||
"author": { | ||
"name": "Tom Pawlak", | ||
"url": "http://blog.tompawlak.org/", | ||
"email": "tompwk@gmail.com" | ||
}, | ||
"licenses": [{ | ||
"type": "MIT", | ||
"url": "http://blog.tompawlak.org/p/mit.html" | ||
}], | ||
"bugs": { | ||
@@ -30,0 +37,0 @@ "url": "https://github.com/T-PWK/biguint-format/issues" |
@@ -1,7 +0,7 @@ | ||
Big Unsigned Integer Formatter | ||
============================== | ||
Big UInteger Formatter | ||
==== | ||
[![Build Status](https://travis-ci.org/T-PWK/biguint-format.png?branch=master)](https://travis-ci.org/T-PWK/biguint-format) [![NPM version](https://badge.fury.io/js/biguint-format.png)](http://badge.fury.io/js/biguint-format) [![Total views](https://sourcegraph.com/api/repos/github.com/T-PWK/biguint-format/counters/views.png)](https://sourcegraph.com/github.com/T-PWK/biguint-format) | ||
An arbitrary length unsigned integer formatter library for Node.js. | ||
[![NPM](https://nodei.co/npm/biguint-format.png)](https://nodei.co/npm/biguint-format/) | ||
JavaScript uses [IEEE 754 double-precision floats](http://en.wikipedia.org/wiki/IEEE_floating_point) to represents numbers. That works perfectly fine for small numbers, however, it is an issue for big integers. This means they lose integer precision for values beyond `+/- 2 pow 53` | ||
@@ -18,3 +18,3 @@ | ||
// output is '1.5123660750094533e+36' - incorrect - lose integer precision | ||
```` | ||
``` | ||
@@ -34,6 +34,10 @@ ### Solution ### | ||
``` | ||
## Instalation ## | ||
``` | ||
$ npm install biguint-format | ||
``` | ||
## Usage ## | ||
## API ## | ||
The `biguint-format` module has `format(number, format [, options])` function which performs number conversion to the required string format. | ||
The `biguint-format` module is a function (`fn(number, format [, options])`) which performs number conversion to the required string format. | ||
@@ -56,2 +60,5 @@ The `number` argument represents an arbitrary lenght unsigned integer number to be converted to string. It can be provided in one of the following formats: | ||
* `delimiter` - specifes delimiter string to be inserted in between character groups. Default value is space. It is quite handy option when dealing with large numbers. | ||
* `trim` - (works only with `bin` formatting) specifies if the leading 0's should be trimmed. | ||
* `padstr` - string used for right-padding of the fomratted string if its length (inlucing prefix and grouping) is less than value of `size` parameter. | ||
* `size` - determines formatted string size. That opiton has effect only with `padstr` option. Note that the formatted string is not trimmed if its length is longer than value of `size` parameter. | ||
@@ -61,3 +68,3 @@ ### Examples ### | ||
```js | ||
var biguint = require('biguint-format'); | ||
var format = require('biguint-format'); | ||
@@ -67,21 +74,23 @@ var buffer1 = new Buffer([0x63, 0xA7, 0x27]); | ||
biguint.format(buffer1, 'dec', {format:'LE'}) // returns '2598755' | ||
biguint.format(buffer2, 'dec', {format:'BE'}) // returns '2598755' | ||
biguint.format(buffer2, 'dec') // returns '2598755' | ||
format(buffer1, 'dec', {format:'LE'}) // returns '2598755' | ||
format(buffer2, 'dec', {format:'BE'}) // returns '2598755' | ||
format(buffer2, 'dec') // returns '2598755' | ||
biguint.format(buffer1, 'hex', {format:'LE'}) // returns '27a763' | ||
biguint.format(buffer2, 'hex', {format:'BE'}) // returns '27a763' | ||
biguint.format(buffer2, 'hex', {prefix:'0x'}) // returns '0x27a763' | ||
format(buffer1, 'hex', {format:'LE'}) // returns '27a763' | ||
format(buffer2, 'hex', {format:'BE'}) // returns '27a763' | ||
format(buffer2, 'hex', {prefix:'0x'}) // returns '0x27a763' | ||
biguint.format(buffer2, 'bin') // 001001111010011101100011 | ||
biguint.format(buffer2, 'bin', {groupsize:8}) // 00100111 10100111 01100011 | ||
biguint.format(buffer2, 'oct') // 11723543 | ||
biguint.format(buffer2, 'oct', {prefix:'0'}) // 011723543 | ||
format(buffer2, 'bin') // 001001111010011101100011 | ||
format(buffer2, 'bin', {groupsize:8}) // 00100111 10100111 01100011 | ||
format(buffer2, 'oct') // 11723543 | ||
format(buffer2, 'oct', {prefix:'0'}) // 011723543 | ||
``` | ||
Usage of `delimiter` option which helps with large numbers e.g. | ||
Use of `delimiter` option which helps with large numbers e.g. | ||
```js | ||
biguint.format([0x2A, 0xFF, 0x1E, 0x22, 0x11, 0x30, 0x12, 0x2F], 'bin') | ||
biguint.format([0x2A, 0xFF, 0x1E, 0x22, 0x11, 0x30, 0x12, 0x2F], 'bin', {groupsize:8}) | ||
var format = require('biguint-format'); | ||
format([0x2A, 0xFF, 0x1E, 0x22, 0x11, 0x30, 0x12, 0x2F], 'bin') | ||
format([0x2A, 0xFF, 0x1E, 0x22, 0x11, 0x30, 0x12, 0x2F], 'bin', {groupsize:8}) | ||
// returned values | ||
@@ -91,4 +100,28 @@ 0010101011111111000111100010001000010001001100000001001000101111 // no delimiter | ||
``` | ||
Example of `trim` option which works only with binary formatter | ||
```js | ||
var format = require('biguint-format'); | ||
var buffer = new Buffer([0x1, 0xA7, 0x63]); | ||
format(buffer, 'bin'); // returns 000000011010011101100011 | ||
format(buffer, 'bin', {trim:true}); // returns 11010011101100011 | ||
``` | ||
Example of `padstr` and `size` options | ||
```js | ||
var format = require('biguint-format'); | ||
var buffer = new Buffer([0x1, 0xA7, 0x63]); | ||
format(buffer, 'dec'); // returns 108387 | ||
format(buffer, 'oct'); // returns 323543 | ||
format(buffer, 'hex'); // returns 1a763 | ||
format(buffer, 'dec', {padstr:'0', size:6}); // returns 108387 - no padding effect | ||
format(buffer, 'oct', {padstr:'0', size:10}); // returns 0000323543 | ||
format(buffer, 'hex', {padstr:'0', size:6}); // returns 01a763 | ||
``` | ||
## Author ## | ||
Writen by Tom Pawlak - [Blog](http://tompawlak.blogspot.co.uk) | ||
Writen by Tom Pawlak - [Blog](http://blog.tompawlak.org) | ||
@@ -99,2 +132,2 @@ ## License ## | ||
MIT License : http://tompawlak.blogspot.com/p/mit.html | ||
MIT License : http://blog.tompawlak.org/p/mit.html |
24
test.js
@@ -11,2 +11,4 @@ var assert = require('assert') | ||
['0x27A763', 'dec'], '2598755', | ||
['0x27A763', 'dec', {padstr:'0', size:10}], '0002598755', | ||
['0x27A763', 'dec', {padstr:'0000', size:10}], '0002598755', | ||
['0x100', 'dec'], '256', | ||
@@ -31,4 +33,10 @@ ['0x0000100', 'dec'], '256', | ||
[[0x00, 0x00, 0x00, 0x01], 'hex', {prefix:'0x'}], '0x1', | ||
[[0x00, 0x00, 0x00, 0x01], 'hex', {padstr:'0', size:12}], '000000000001', | ||
[[0x00, 0x00, 0x00, 0x01], 'hex', {prefix:'0x', padstr:'0', size:12}], '0x0000000001', | ||
['0x0000100', 'hex', {prefix:'0x'}], '0x100', | ||
['0x1FF', 'oct'], '777', | ||
['0x1FF', 'oct', {padstr:'0', size:1}], '777', | ||
['0x1FF', 'oct', {padstr:'0', size:4, prefix:'0'}], '0777', | ||
['0x1FF', 'oct', {padstr:'0', size:6}], '000777', | ||
['0x1FF', 'oct', {padstr:'0', size:6, prefix:'0'}], '000777', | ||
[[0xFF, 0x1], 'oct', {format:'LE'}], '777', | ||
@@ -41,6 +49,14 @@ ['0x123456789A', 'oct', {prefix:'0'}], '01106425474232', | ||
[[0x1, 0xFF], 'bin', {delimiter:' ', groupsize:1}], '0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1', | ||
[[0x1, 0xFF], 'bin', {groupsize:1}], '0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1', | ||
['0x1FF', 'bin', {delimiter:' '}], '0000000111111111', | ||
[[0x1, 0xFF], 'bin', {groupsize:1}], '0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1', // default delimiter is ' ' | ||
['0x1FF', 'bin', {delimiter:' '}], '0000000111111111', // group size needs to be provided | ||
['0x1FF', 'bin', {delimiter:' ', groupsize:8}], '00000001 11111111', | ||
['0x1FF', 'bin', {delimiter:'|', prefix:'B', groupsize:8}], 'B00000001|11111111' | ||
['0x1FF', 'bin', {delimiter:'|', prefix:'B', groupsize:8}], 'B00000001|11111111', | ||
['0x1FF', 'bin', {trim:true}], '111111111', | ||
['0x1FF', 'bin', {trim:true, prefix:'B'}], 'B111111111', | ||
['0x1FF', 'bin', {trim:true, prefix:'B', padstr:'0', size:12}], 'B00111111111', | ||
['0x1FF', 'bin', {trim:true, padstr:'0', size:12}], '000111111111', | ||
['0x1FF', 'bin', {padstr:'0', size:12}], '0000000111111111', | ||
['0x0', 'bin', {trim:true}], '0', | ||
['0x1', 'bin', {trim:true}], '1', | ||
['0x1', 'bin', {trim:true, padstr:'0', size:8}], '00000001' | ||
] | ||
@@ -50,3 +66,3 @@ | ||
for (var i = 0; i < tests.length; i += 2) { | ||
assert.equal (biguint.format.apply(biguint, tests[i]), tests[i+1]); | ||
assert.equal (biguint.apply(biguint, tests[i]), tests[i+1]); | ||
}; |
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
18102
6
249
127