Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

[WIP] Rewrite in typescript, add more methods #8

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
node_modules
package-lock.json
dist
.nyc_output
coverage
3 changes: 3 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@ethereumjs/config-nyc"
}
6 changes: 6 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
.vscode
package.json
dist
.nyc_output
docs
47 changes: 26 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# SYNOPSIS
# SYNOPSIS

[![NPM Package](https://img.shields.io/npm/v/fixed-bn.js.svg?style=flat-square)](https://www.npmjs.org/package/fixed-bn.js)
[![Build Status](https://img.shields.io/travis/ewasm/fixed-bn.js.svg?branch=master&style=flat-square)](https://travis-ci.org/ewasm/fixed-bn.js)
[![Coverage Status](https://img.shields.io/coveralls/ewasm/fixed-bn.js.svg?style=flat-square)](https://coveralls.io/r/ewasm/fixed-bn.js)

[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

a bn.js factory wrapper that constrains numbers to a fixed width

# USAGE

```javascript
const FixedBN = require('fixed-bn.js')

Expand All @@ -22,85 +24,88 @@ bnNum.toBuffer()

// you can also create an arbitary fixed lenght bn
// max bit lenght is 199 bits and min length is 2 bits
const I199 = FixedBN(199, 2)
const I199 = FixedBN(199, 2)
const newBnNum = new I199(390248)
```

# API
Since this module extends [BN.js](https://github.com/indutny/bn.js/) it has the methods as it does plus a few extras.

Since this module extends [BN.js](https://github.com/indutny/bn.js/) it has the methods as it does plus a few extras.

## factory

[./index.js:11-87](https://github.com/ewasm/fixedBN/blob/814e88711940f48efc341ed0c1296f7fa6cdd111/./index.js#L11-L87 "Source code on GitHub")
[./index.js:11-87](https://github.com/ewasm/fixedBN/blob/814e88711940f48efc341ed0c1296f7fa6cdd111/./index.js#L11-L87 'Source code on GitHub')

A factory that produces BN.js constructors for a given width

**Parameters**

- `maxWidth` **Integer** the max length in bits that the bn.js instance can handle
- `minWidth` **Integer** the min length in bits that the bn.js instance can handle
- `maxWidth` **Integer** the max length in bits that the bn.js instance can handle
- `minWidth` **Integer** the min length in bits that the bn.js instance can handle

Returns **bn.js** returns a bn.js constuctor that that is constained to `maxWidth` and `minWidth`

## builtin length

the factory has the following builtins

- `FixedBN.U64`
- `FixedBN.U128`
- `FixedBN.U160`
- `FixedBN.U256`

## bn.js instance

Each instance has the following additional methods

## maxWidth

[./index.js:35-37](https://github.com/ewasm/fixedBN/blob/814e88711940f48efc341ed0c1296f7fa6cdd111/./index.js#L35-L37 "Source code on GitHub")
[./index.js:35-37](https://github.com/ewasm/fixedBN/blob/814e88711940f48efc341ed0c1296f7fa6cdd111/./index.js#L35-L37 'Source code on GitHub')

retuns Max Width

Returns **integer**
Returns **integer**

## minWidth

[./index.js:43-45](https://github.com/ewasm/fixedBN/blob/814e88711940f48efc341ed0c1296f7fa6cdd111/./index.js#L43-L45 "Source code on GitHub")
[./index.js:43-45](https://github.com/ewasm/fixedBN/blob/814e88711940f48efc341ed0c1296f7fa6cdd111/./index.js#L43-L45 'Source code on GitHub')

retuns Min Width

Returns **integer**
Returns **integer**

## fromBuffer

[./index.js:66-68](https://github.com/ewasm/fixedBN/blob/814e88711940f48efc341ed0c1296f7fa6cdd111/./index.js#L66-L68 "Source code on GitHub")
[./index.js:66-68](https://github.com/ewasm/fixedBN/blob/814e88711940f48efc341ed0c1296f7fa6cdd111/./index.js#L66-L68 'Source code on GitHub')

converts a buffer to a fixed-bn.js

**Parameters**

- `value` **([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) | integer)**
- `endain` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
- `endian` (optional, default `'be'`)
- `value` **([string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) | integer)**
- `endain` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
- `endian` (optional, default `'be'`)

## isFixBN

[./index.js:74-76](https://github.com/ewasm/fixedBN/blob/814e88711940f48efc341ed0c1296f7fa6cdd111/./index.js#L74-L76 "Source code on GitHub")
[./index.js:74-76](https://github.com/ewasm/fixedBN/blob/814e88711940f48efc341ed0c1296f7fa6cdd111/./index.js#L74-L76 'Source code on GitHub')

checks if a BN instance is a fixed BN instance

**Parameters**

- `bn` **bn.js**
- `bn` **bn.js**

## isSameWidth

[./index.js:82-84](https://github.com/ewasm/fixedBN/blob/814e88711940f48efc341ed0c1296f7fa6cdd111/./index.js#L82-L84 "Source code on GitHub")
[./index.js:82-84](https://github.com/ewasm/fixedBN/blob/814e88711940f48efc341ed0c1296f7fa6cdd111/./index.js#L82-L84 'Source code on GitHub')

checks if a fixed-bn instance is the same width as the contructor

**Parameters**

- `fixBN` **bn.js**

- `fixBN` **bn.js**

# LICENSE
[MPL-2.0](https://tldrlegal.com/license/mozilla-public-license-2.0-(mpl-2))

[MPL-2.0](<https://tldrlegal.com/license/mozilla-public-license-2.0-(mpl-2)>)
74 changes: 74 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@

# fixed-bn.js

## Index

### Classes

* [Factory](classes/factory.md)
* [FixedWidthBN](classes/fixedwidthbn.md)

### Type aliases

* [Endianness](#endianness)

### Variables

* [U128](#u128)
* [U160](#u160)
* [U256](#u256)
* [U64](#u64)

---

## Type aliases

<a id="endianness"></a>

### Endianness

**Ƭ Endianness**: *"le" \| "be"*

*Defined in [fixed-width.ts:6](https://github.com/ewasm/fixed-bn.js/blob/master/src/fixed-width.ts#L6)*

___

## Variables

<a id="u128"></a>

### `<Const>` U128

**● U128**: *[Factory](classes/factory.md)* = new Factory(128)

*Defined in [index.ts:7](https://github.com/ewasm/fixed-bn.js/blob/master/src/index.ts#L7)*

___
<a id="u160"></a>

### `<Const>` U160

**● U160**: *[Factory](classes/factory.md)* = new Factory(160)

*Defined in [index.ts:8](https://github.com/ewasm/fixed-bn.js/blob/master/src/index.ts#L8)*

___
<a id="u256"></a>

### `<Const>` U256

**● U256**: *[Factory](classes/factory.md)* = new Factory(256)

*Defined in [index.ts:6](https://github.com/ewasm/fixed-bn.js/blob/master/src/index.ts#L6)*

___
<a id="u64"></a>

### `<Const>` U64

**● U64**: *[Factory](classes/factory.md)* = new Factory(64)

*Defined in [index.ts:9](https://github.com/ewasm/fixed-bn.js/blob/master/src/index.ts#L9)*

___

134 changes: 134 additions & 0 deletions docs/classes/factory.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
[fixed-bn.js](../README.md) > [Factory](../classes/factory.md)

# Class: Factory

Convenience class for creating FixedWidthBN values with the same width.

## Hierarchy

**Factory**

## Index

### Constructors

* [constructor](factory.md#constructor)

### Properties

* [width](factory.md#width)

### Methods

* [fromBN](factory.md#frombn)
* [fromBuffer](factory.md#frombuffer)
* [fromNumber](factory.md#fromnumber)
* [fromString](factory.md#fromstring)

---

## Constructors

<a id="constructor"></a>

### constructor

⊕ **new Factory**(width: *`number`*): [Factory](factory.md)

*Defined in [factory.ts:9](https://github.com/ewasm/fixed-bn.js/blob/master/src/factory.ts#L9)*

**Parameters:**

| Name | Type |
| ------ | ------ |
| width | `number` |

**Returns:** [Factory](factory.md)

___

## Properties

<a id="width"></a>

### width

**● width**: *`number`*

*Defined in [factory.ts:9](https://github.com/ewasm/fixed-bn.js/blob/master/src/factory.ts#L9)*

___

## Methods

<a id="frombn"></a>

### fromBN

▸ **fromBN**(value: *`BN`*): [FixedWidthBN](fixedwidthbn.md)

*Defined in [factory.ts:19](https://github.com/ewasm/fixed-bn.js/blob/master/src/factory.ts#L19)*

**Parameters:**

| Name | Type |
| ------ | ------ |
| value | `BN` |

**Returns:** [FixedWidthBN](fixedwidthbn.md)

___
<a id="frombuffer"></a>

### fromBuffer

▸ **fromBuffer**(value: *`Buffer`*, endian?: *[Endianness](../#endianness)*): [FixedWidthBN](fixedwidthbn.md)

*Defined in [factory.ts:27](https://github.com/ewasm/fixed-bn.js/blob/master/src/factory.ts#L27)*

**Parameters:**

| Name | Type | Default value |
| ------ | ------ | ------ |
| value | `Buffer` | - |
| `Default value` endian | [Endianness](../#endianness) | &quot;be&quot; |

**Returns:** [FixedWidthBN](fixedwidthbn.md)

___
<a id="fromnumber"></a>

### fromNumber

▸ **fromNumber**(value: *`number`*): [FixedWidthBN](fixedwidthbn.md)

*Defined in [factory.ts:15](https://github.com/ewasm/fixed-bn.js/blob/master/src/factory.ts#L15)*

**Parameters:**

| Name | Type |
| ------ | ------ |
| value | `number` |

**Returns:** [FixedWidthBN](fixedwidthbn.md)

___
<a id="fromstring"></a>

### fromString

▸ **fromString**(value: *`string`*, base?: *`number`*): [FixedWidthBN](fixedwidthbn.md)

*Defined in [factory.ts:23](https://github.com/ewasm/fixed-bn.js/blob/master/src/factory.ts#L23)*

**Parameters:**

| Name | Type | Default value |
| ------ | ------ | ------ |
| value | `string` | - |
| `Default value` base | `number` | 16 |

**Returns:** [FixedWidthBN](fixedwidthbn.md)

___

Loading