Add number conversion utilities (#29)

* Add number conversion utilities

* Update after checks

* Add isNull
This commit is contained in:
Jaco Greeff
2017-11-29 21:22:49 +01:00
committed by GitHub
parent 98ca7d3a72
commit 0f2b423160
14 changed files with 355 additions and 5 deletions
+1
View File
@@ -9,6 +9,7 @@ Utility methods for this package are split into groups
- [is](is.md) Type checking utilities
- [jsonrpc](jsonrpc.md) Convenience functions for values from @polkadot/api-jsonrpc
- [keccak](keccak.md) Create Keccak256 values as hex, string & buffer output
- [number](number.md) Utility methods to convert to and from `number` values
- [u8a](u8a.md) Utility methods to convert to and from `Uint8Array` objects
# Available methods
+18
View File
@@ -8,6 +8,7 @@ Type checking utilities
- [isFunction](#isfunction) Tests for a `function`.
- [isHex](#ishex) Tests for a hex string.
- [isInstanceOf](#isinstanceof) Tests for a instance of a class.
- [isNull](#isnull) Tests for a `null` values.
- [isNumber](#isnumber) Tests for a JavaScript number.
- [isString](#isstring) Tests for a string.
- [isUint8Array](#isuint8array) Tests for a `Uint8Array` object instance.
@@ -116,6 +117,23 @@ import { isInstanceOf } from '@polkadot/util';
console.log('isinstanceOf', isInstanceOf(new Array(0), Array)); // => true
```
## isNull
Tests for a `null` values.
```js
isNull (value: any): boolean
```
Checks to see if the input value is `null`.
```js
import { isNull } from '@polkadot/util';
console.log('isNull', isNull(null)); // => true
```
## isNumber
Tests for a JavaScript number.
+76
View File
@@ -0,0 +1,76 @@
# number
Utility methods to convert to and from `number` values
- [numberFromBuffer](#numberfrombuffer) Creates a Number value from a Buffer object.
- [numberFromHex](#numberfromhex) Creates a Number value from a Buffer object.
- [numberToBuffer](#numbertobuffer) Creates a Buffer object from a number.
- [numberToHex](#numbertohex) Creates a hex value from a number.
## numberFromBuffer
Creates a Number value from a Buffer object.
```js
numberFromBuffer (value?: Buffer): number
```
`null` inputs returns an NaN result, `Buffer` values return the actual value as a `Number`.
```js
import { numberFromBuffer } from '@polkadot/util';
numberFromBuffer(Buffer.from([12, 34, 56])); // => 0x123456
```
## numberFromHex
Creates a Number value from a Buffer object.
```js
numberFromHex (value?: Hex): number
```
`null` inputs returns an Nan result, `hex` values return the actual value as a `Number`.
```js
import { numberFromHex } from '@polkadot/util';
numberFromBuffer('0x1234'); // => 0x1234
```
## numberToBuffer
Creates a Buffer object from a number.
```js
numberToBuffer (value?: number): Buffer
```
`null`/`undefined`/`NaN` inputs returns an empty `Buffer` result. `number` input values return the actual bytes value converted to a `Buffer`.
```js
import { numberToBuffer } from '@polkadot/util';
const buffer = numberToBuffer('0x1234'); // => Buffer.from([0x12, 0x34])
```
## numberToHex
Creates a hex value from a number.
```js
numberToHex (value?: number): string
```
`null`/`undefined`/`NaN` inputs returns an empty `0x` result. `number` input values return the actual bytes value converted to a `hex`.
```js
import { numberToHex } from '@polkadot/util';
const hex = numberToHex(0x1234); // => '0x1234'
```