Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

biguint-format

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

biguint-format - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

33

index.js

@@ -61,7 +61,8 @@ /**

return digits.toString('ascii', d);
return _split(digits.toString('ascii', d), options.groupsize, options.delimiter);
}
function toBinaryString (buffer, options) {
var options = options || {}, digits = new Array(buffer.length), num;
var options = options || {}, digits = new Array(buffer.length)
, size = options.groupsize || -1, num, result;

@@ -75,3 +76,9 @@ if((options.format || 'BE') !== 'BE') _reverseBuffer(buffer);

return (options.prefix || '') + digits.join(options.delimiter || '');
if(size <= 0) {
result = digits.join('');
} else {
result = _split(digits.join(''), size, options.delimiter)
}
return (options.prefix || '') + result;
}

@@ -94,3 +101,4 @@

return (options.prefix || '') + digits.slice(idx);
return (options.prefix || '')
+ _split(digits.slice(idx), options.groupsize, options.delimiter);
}

@@ -124,5 +132,14 @@

return (options.prefix || '') + digits.toString('ascii', idx)
return (options.prefix || '')
+ _split(digits.toString('ascii', idx), options.groupsize, options.delimiter)
}
function _split (string, size, delim) {
if(typeof delim === 'undefined')
delim = ' ';
return (typeof string !== 'undefined' && +size > 0)
? string.replace(new RegExp('(.)(?=(.{' + +size + '})+(?!.))', 'g'), "$1" + delim)
: string;
}
function _toAsciiDigits (buffer, offset) {

@@ -189,4 +206,4 @@ for (var i = offset; i < buffer.length; i++) {

carry = (buffer[i] & 0x80) != 0;
buffer[i] = (buffer[i] * 2) & 0xFF;
if(carry && i >= 0) buffer[i+1] |= 0x01;
buffer[i] = (buffer[i] << 1) & 0xFF;
if(carry && i >= 0) buffer[i+1] |= 0x01;
};

@@ -203,3 +220,3 @@ }

prevcarry = (buffer[i] & 0x1) != 0;
buffer[i] = Math.floor(buffer[i] / 2) & 0xFF;
buffer[i] >>= 1;
if(carry && i > 0) buffer[i] |= 0x80;

@@ -206,0 +223,0 @@ };

{
"name": "biguint-format",
"version": "0.1.0",
"version": "0.1.1",
"description": "An arbitrary length unsigned integer formatter library for Node.js",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -0,4 +1,10 @@

Big Unsigned Integer Formatter
==============================
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`
### Problem
### Problem ###

@@ -14,3 +20,3 @@ Presentation of *small* integer in decimal format works fine (e.g. `0x1FF`). However, we can see an issue when we try to convert big integers like `0x1234567890abcdeffedcba908765421` to string decimal.

### Solution
### Solution ###

@@ -29,3 +35,3 @@ Node.js `biguint-format` module has been built in order to help display very large (arbitrary lengh) unsigned integers without any integer precision lose.

### `biguint-format` module
## Usage ##

@@ -35,18 +41,19 @@ The `biguint-format` module has `format(number, format [, options])` function which performs number conversion to the required string format.

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:
- Node.js Buffer e.g. `new Buffer([0x1, 0xFF])`
- An array of bytes (values from `0x00` to `0xFF`) e.g. `[0x1, 0xFF]`.
- A string with a number in a hexadecimal format e.g. `0x1FF0A` or `1FF0A`
* Node.js [Buffer](http://nodejs.org/api/buffer.html) e.g. `new Buffer([0x1, 0xFF])`
* An array of bytes (values from `0x00` to `0xFF`) e.g. `[0x1, 0xFF]`.
* A string with a number in a hexadecimal format e.g. `0x1FF0A` or `1FF0A`
The `format` argument represents output string format and it can be one of the following options:
- `dec` - convertion to decimal format e.g. `123324884`
- `bin` - conversion to binary format e.g. `1100101010`
- `hex` - conversion to hexadecimal format e.g. `0xADFFAA11`
- `oct` - conversion to octet format e.g. `07771`
* `dec` - convertion to decimal format e.g. `123324884`
* `bin` - conversion to binary format e.g. `1100101010`
* `hex` - conversion to hexadecimal format e.g. `0xADFFAA11`
* `oct` - conversion to octet format e.g. `07771`
The `options` argument (optional) is an object which provides some additional conversion details:
- `format` - specifies format of the input number. It can be either `BE` for Big Endian or `LE` for Little Endian. `BE` is a default value. Check [wikipedia](http://en.wikipedia.org/wiki/Endianness) for more details.
- `prefix` - output string prefix. It is not supported by `dec` conversion.
- `delimiter` - used by `bin` conversion only; specifes delimiter between bytes. It is quite handy option when dealing with large numbers.
* `format` - specifies format of the input number. It can be either `BE` for Big Endian or `LE` for Little Endian. `BE` is a default value. Check [wikipedia](http://en.wikipedia.org/wiki/Endianness) for more details.
* `prefix` - output string prefix. Note that this option is not supported by `dec` conversion.
* `groupsize` - splits output string into groups of `groupsize` lenght characters.
* `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.
#### Examples
### Examples ###

@@ -68,3 +75,3 @@ ```js

biguint.format(buffer2, 'bin') // 001001111010011101100011
biguint.format(buffer2, 'bin', {delimiter:' '}) // 00100111 10100111 01100011
biguint.format(buffer2, 'bin', {groupsize:8}) // 00100111 10100111 01100011
biguint.format(buffer2, 'oct') // 11723543

@@ -77,3 +84,3 @@ biguint.format(buffer2, 'oct', {prefix:'0'}) // 011723543

biguint.format([0x2A, 0xFF, 0x1E, 0x22, 0x11, 0x30, 0x12, 0x2F], 'bin')
biguint.format([0x2A, 0xFF, 0x1E, 0x22, 0x11, 0x30, 0x12, 0x2F], 'bin', {delimiter:' '})
biguint.format([0x2A, 0xFF, 0x1E, 0x22, 0x11, 0x30, 0x12, 0x2F], 'bin', {groupsize:8})

@@ -84,1 +91,9 @@ // returned values

```
## Author ##
Writen by Tom Pawlak - [Blog](http://tompawlak.blogspot.co.uk)
## License ##
Copyright (c) 2014 Tom Pawlak
MIT License : http://tompawlak.blogspot.com/p/mit.html

@@ -13,3 +13,5 @@ var assert = require('assert')

['0x0000100', 'dec'], '256',
['0x0000100', 'dec', {groupsize:1}], '2 5 6',
[[0, 0, 0x10, 0], 'dec', {format:'LE'}], '1048576',
[[0, 0, 0x10, 0], 'dec', {format:'LE', groupsize:'3',delimiter:','}], '1,048,576',
[[0x63, 0xA7, 0x27], 'dec', {format:'LE'}], '2598755',

@@ -22,3 +24,6 @@ [[0x27, 0xA7, 0x63], 'dec', {format:'BE'}], '2598755',

['1234567890abcdeffedcba908765421', 'dec'], '1512366075009453296626403467035300897',
['1234567890abcdeffedcba908765421', 'dec', {delimiter:',', groupsize:3}], '1,512,366,075,009,453,296,626,403,467,035,300,897',
[[0x1, 0xFF, 0xFF], 'hex'], '1ffff',
[[0x1, 0xFF, 0xFF], 'hex', {groupsize:2}], '1 ff ff',
[[0x1, 0xFF, 0xFF], 'hex', {groupsize:2, delimiter:'::'}], '1::ff::ff',
[[0x1, 0xFF, 0xFF], 'hex', {prefix:'0x'}], '0x1ffff',

@@ -32,6 +37,10 @@ [[0xFF, 0xFF, 0x01], 'hex', {format:'LE', prefix:'0x'}], '0x1ffff',

['0x123456789ABCDEFF', 'oct', {prefix:'0'}], '0110642547423257157377',
[[0xFF, 0x1], 'bin', {format:'LE'}], '111111111',
[[0x1, 0xFF], 'bin'], '111111111',
['0x1FF', 'bin', {delimiter:' '}], '1 11111111',
['0x1FF', 'bin', {delimiter:'|', prefix:'B'}], 'B1|11111111'
['0x123456789ABCDEFF', 'oct', {groupsize:3, delimiter:' '}], '110 642 547 423 257 157 377',
[[0xFF, 0x1], 'bin', {format:'LE'}], '0000000111111111',
[[0x1, 0xFF], 'bin'], '0000000111111111',
[[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',
['0x1FF', 'bin', {delimiter:' ', groupsize:8}], '00000001 11111111',
['0x1FF', 'bin', {delimiter:'|', prefix:'B', groupsize:8}], 'B00000001|11111111'
]

@@ -38,0 +47,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc