Files
common/packages/util/docs/is.md
T
2018-05-08 10:49:11 +00:00

5.0 KiB

is

Type checking utilities

  • isBn Tests for a BN object instance.
  • isBoolean Tests for a boolean value.
  • isBuffer Tests for a Buffer object instance.
  • isError Tests for a Error object instance.
  • isFunction Tests for a function.
  • isHex Tests for a hex string.
  • isInstanceOf Tests for a instance of a class.
  • isIp Tests if the value is a valid IP address
  • isNull Tests for a null values.
  • isNumber Tests for a JavaScript number.
  • isObject Tests for an object.
  • isBObservable Tests for a Observable object instance.
  • isString Tests for a string.
  • isU8a Tests for a Uint8Array object instance.
  • isUndefined Tests for a undefined values.

isBn

Tests for a BN object instance.

isBN (value: mixed): boolean

Checks to see if the input object is an instance of BN (bn.js).

import BN from 'bn.js';
import { isBn } from '@polkadot/util';

console.log('isBn', isBn(new BN(1))); // => true

isBoolean

Tests for a boolean value.

isBoolean (value: mixed): boolean

Checks to see if the input value is a JavaScript boolean.

import { isBoolean } from '@polkadot/util';

isBoolean(false); // => true

isBuffer

Tests for a Buffer object instance.

isBuffer (value: mixed): boolean

Checks to see if the input object is an instance of Buffer.

import { isBuffer } from '@polkadot/util';

console.log('isBuffer', isBuffer(Buffer.from([]))); // => true

isError

Tests for a Error object instance.

isError (value: mixed): boolean

Checks to see if the input object is an instance of Error.

import { isError } from '@polkadot/util';

console.log('isError', isError(new Error('message'))); // => true

isFunction

Tests for a function.

isFunction (value: mixed): boolean

Checks to see if the input value is a JavaScript function.

import { isFunction } from '@polkadot/util';

console.log('isFunction', isFunction(() => false)); // => true

isHex

Tests for a hex string.

isHex (value: mixed, bitLength: number = -1): boolean

Checks to see if the input value is a 0x prefixed hex string. Optionally (bitLength !== -1) checks to see if the bitLength is correct.

import { isHex } from '@polkadot/util';

isHex('0x1234'); // => true
isHex('0x1234', 8); // => false

isInstanceOf

Tests for a instance of a class.

isInstanceOf (value: mixed, clazz: Class<mixed>): boolean

Checks to see if the input value is an instance of the test class.

import { isInstanceOf } from '@polkadot/util';

console.log('isInstanceOf', isInstanceOf(new Array(0), Array)); // => true

isIp

Tests if the value is a valid IP address

isIp (value: string, type?: 'v4' | 'v6'): boolean

Checks to see if the value is a valid IP address. Optionally check for either v4/v6

import { isIp } from '@polkadot/util';

isIp('192.168.0.1')); // => true
isIp('1:2:3:4:5:6:7:8'); // => true
isIp('192.168.0.1', 'v6')); // => false
isIp('1:2:3:4:5:6:7:8', 'v4'); // => false

isNull

Tests for a null values.

isNull (value: mixed): boolean

Checks to see if the input value is null.

import { isNull } from '@polkadot/util';

console.log('isNull', isNull(null)); // => true

isNumber

Tests for a JavaScript number.

isNumber (value: mixed): boolean

Checks to see if the input value is a valid number.

import { isNumber } from '@polkadot/util';

console.log('isNumber', isNumber(1234)); // => true

isObject

Tests for an object.

isObject (value: mixed): boolean

Checks to see if the input value is a JavaScript object.

import { isObject } from '@polkadot/util';

isObject({}); // => true
isObject('something'); // => false

isBObservable

Tests for a Observable object instance.

isObservable (value: mixed): boolean

Checks to see if the input object is an instance of BN (bn.js).

import { isObservable } from '@polkadot/util';

console.log('isObservable', isObservable(...));

isString

Tests for a string.

isString (value: mixed): boolean

Checks to see if the input value is a JavaScript string.

import { isString } from '@polkadot/util';

console.log('isString', isString('test')); // => true

isU8a

Tests for a Uint8Array object instance.

isU8a (value: mixed): boolean

Checks to see if the input object is an instance of Uint8Array.

import { isUint8Array } from '@polkadot/util';

console.log('isU8a', isU8a([])); // => false

isUndefined

Tests for a undefined values.

isUndefined (value: mixed): boolean

Checks to see if the input value is undefined.

import { isUndefined } from '@polkadot/util';

console.log('isUndefined', isUndefined(void(0))); // => true