diff --git a/docs/README.md b/docs/README.md index 9a422573a..943dd87b8 100644 --- a/docs/README.md +++ b/docs/README.md @@ -2,6 +2,7 @@ Utility methods for this package are split into groups +- [array](array.md) Utility methods that operates on arrays - [bn](bn.md) Utility methods to convert to and from `BN` objects - [buffer](buffer.md) Utility methods to convert to and from `Buffer` objects - [ext](ext.md) Extensions to basic classes diff --git a/docs/array.md b/docs/array.md new file mode 100644 index 000000000..d8842b136 --- /dev/null +++ b/docs/array.md @@ -0,0 +1,23 @@ +# array + +Utility methods that operates on arrays + +- [arrayFilter](#arrayfilter) Filters undefined and (optionally) null values from an array + +## arrayFilter + +Filters undefined and (optionally) null values from an array + +```js +arrayFilter (array: Array, allowNulls: boolean = true): Array +``` + + +Returns a new array with all `undefined` values removed. Optionally, when `allowNulls = false`, it removes the `null` values as well + +```js +import { arrayFilter } from '@polkadot/util'; + +arrayFilter([0, void 0, true, null, false, '']); // [0, true, null, false, ''] +arrayFilter([0, void 0, true, null, false, ''], false); // [0, true, false, ''] +``` \ No newline at end of file diff --git a/docs/is.md b/docs/is.md index f6f70d030..4107bf721 100644 --- a/docs/is.md +++ b/docs/is.md @@ -8,8 +8,10 @@ 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. +- [isIp](#isip) Tests if the value is a valid IP address - [isNull](#isnull) Tests for a `null` values. - [isNumber](#isnumber) Tests for a JavaScript number. +- [isObject](#isobject) Tests for an `object`. - [isString](#isstring) Tests for a string. - [isUint8Array](#isuint8array) Tests for a `Uint8Array` object instance. - [isUndefined](#isundefined) Tests for a `undefined` values. @@ -88,16 +90,17 @@ console.log('isFunction', isFunction(() => false)); // => true Tests for a hex string. ```js -isHex (value: any): boolean +isHex (value: any, bitLength: number = -1): boolean ``` -Checks to see if the input value is a `0x` prefixed hex string. +Checks to see if the input value is a `0x` prefixed hex string. Optionally (`bitLength` !== -1) checks to see if the bitLength is correct. ```js import { isHex } from '@polkadot/util'; -console.log('isHex', isHex('0x1234')); // => true +isHex('0x1234'); // => true +isHex('0x1234', 8); // => false ``` ## isInstanceOf @@ -114,7 +117,27 @@ Checks to see if the input value is an instance of the test class. ```js import { isInstanceOf } from '@polkadot/util'; -console.log('isinstanceOf', isInstanceOf(new Array(0), Array)); // => true +console.log('isInstanceOf', isInstanceOf(new Array(0), Array)); // => true +``` + +## isIp + +Tests if the value is a valid IP address + +```js +isIp (value: any, type?: 'v4' | 'v6'): boolean +``` + + +Checks to see if the value is a valid IP address. Optionally check for either v4/v6 + +```js +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 @@ -151,6 +174,24 @@ import { isNumber } from '@polkadot/util'; console.log('isNumber', isNumber(1234)); // => true ``` +## isObject + +Tests for an `object`. + +```js +isObject (value: any): boolean +``` + + +Checks to see if the input value is a JavaScript object. + +```js +import { isObject } from '@polkadot/util'; + +isObject({}); // => true +isObject('something'); // => false +``` + ## isString Tests for a string. diff --git a/package.json b/package.json index 6e853860c..9c339a6c9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@polkadot/util", - "version": "0.7.0", + "version": "0.7.1", "description": "A collection of useful utilities for @polkadot", "main": "index.js", "keywords": [ diff --git a/yarn.lock b/yarn.lock index fab6e3191..b52aa3c49 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2217,7 +2217,7 @@ invert-kv@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/invert-kv/-/invert-kv-1.0.0.tgz#104a8e4aaca6d3d8cd157a8ef8bfab2d7a3ffdb6" -ip-regex@^2.0.0, ip-regex@^2.1.0: +ip-regex@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/ip-regex/-/ip-regex-2.1.0.tgz#fa78bf5d2e6913c911ce9f819ee5146bb6d844e9" @@ -2322,12 +2322,6 @@ is-hexadecimal@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/is-hexadecimal/-/is-hexadecimal-1.0.1.tgz#6e084bbc92061fbb0971ec58b6ce6d404e24da69" -is-ip@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/is-ip/-/is-ip-2.0.0.tgz#68eea07e8a0a0a94c2d080dd674c731ab2a461ab" - dependencies: - ip-regex "^2.0.0" - is-number@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/is-number/-/is-number-2.1.0.tgz#01fcbbb393463a548f2f466cce16dece49db908f"