arrayFilter, isIp, isObject & isHex (optional bitLength) (#46)

This commit is contained in:
Jaco Greeff
2017-12-11 20:37:50 +01:00
committed by GitHub
parent c41ac8a421
commit 77fccc3760
14 changed files with 219 additions and 12 deletions
+3 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/util",
"version": "0.6.10",
"version": "0.7.0",
"description": "A collection of useful utilities for @polkadot",
"main": "index.js",
"keywords": [
@@ -29,12 +29,13 @@
"test": "jest --coverage"
},
"devDependencies": {
"@polkadot/api-jsonrpc": "^0.6.3",
"@polkadot/api-jsonrpc": "^0.6.5",
"@polkadot/dev": "^0.13.3"
},
"dependencies": {
"babel-runtime": "^6.26.0",
"bn.js": "^4.11.8",
"ip-regex": "^2.1.0",
"keccak": "^1.3.0"
}
}
+26
View File
@@ -0,0 +1,26 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
const assert = require('../assert');
const isNull = require('../is/null');
const isUndefined = require('../is/undefined');
/**
@name arrayFilter
@signature arrayFilter (array: Array<any>, allowNulls: boolean = true): Array<any>
@summary Filters undefined and (optionally) null values from an array
@description
Returns a new array with all `undefined` values removed. Optionally, when `allowNulls = false`, it removes the `null` values as well
@example
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, '']
*/
module.exports = function arrayFilter (array: Array<any>, allowNulls: boolean = true): Array<any> {
assert(Array.isArray(array), `Expected Array input value, received '${(array: any)}'`);
return array.filter((value) => {
return !isUndefined(value) && (allowNulls || !isNull(value));
});
};
+23
View File
@@ -0,0 +1,23 @@
// ISC, Copyright 2017 Jaco Greeff
const { arrayFilter } = require('./index');
describe('filterArray', () => {
it('expects an array input', () => {
expect(
() => arrayFilter('notAnArray')
).toThrow(/Expected Array input value/);
});
it('filters arrays, removing undefined', () => {
expect(
arrayFilter([0, '', null, false, void 0, NaN])
).toEqual([0, '', null, false, NaN]);
});
it('filters arrays, removing undefined & null (allowNull = false)', () => {
expect(
arrayFilter([0, '', null, false, void 0, NaN], false)
).toEqual([0, '', false, NaN]);
});
});
+11
View File
@@ -0,0 +1,11 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
const arrayFilter = require('./filter');
/**
@summary Utility methods that operates on arrays
*/
module.exports = {
arrayFilter
};
+2 -1
View File
@@ -1,6 +1,7 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
const array = require('./array');
const assert = require('./assert');
const bn = require('./bn');
const buffer = require('./buffer');
@@ -17,5 +18,5 @@ const u8a = require('./u8a');
@summary Utility methods for this package are split into groups
*/
module.exports = Object.assign(
{}, { assert }, bn, buffer, ext, hex, is, jsonrpc, keccak, { logger }, number, u8a
{}, array, { assert }, bn, buffer, ext, hex, is, jsonrpc, keccak, { logger }, number, u8a
);
+12 -5
View File
@@ -7,15 +7,22 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
/**
@name isHex
@signature isHex (value: any): boolean
@signature isHex (value: any, bitLength: number = -1): boolean
@summary Tests for a hex string.
@description
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.
@example
import { isHex } from '@polkadot/util';
console.log('isHex', isHex('0x1234')); // => true
isHex('0x1234'); // => true
isHex('0x1234', 8); // => false
*/
module.exports = function isHex (value: any): boolean {
return isString(value) && HEX_REGEX.test(value);
module.exports = function isHex (value: any, bitLength: number = -1): boolean {
const isValidHex = isString(value) && HEX_REGEX.test(value);
if (isValidHex && bitLength !== -1) {
return (value: string).length === 2 + bitLength / 4;
}
return isValidHex;
};
+12
View File
@@ -28,4 +28,16 @@ describe('isHex', () => {
isHex(false)
).toEqual(false);
});
it('returns true when valid hex and bitLength matches', () => {
expect(
isHex('0x1234', 16)
).toEqual(true);
});
it('returns true when valid hex and bitLength does not match', () => {
expect(
isHex('0x1234', 8)
).toEqual(false);
});
});
+4
View File
@@ -7,8 +7,10 @@ const isError = require('./error');
const isFunction = require('./function');
const isHex = require('./hex');
const isInstanceOf = require('./instanceOf');
const isIp = require('./ip');
const isNull = require('./null');
const isNumber = require('./number');
const isObject = require('./object');
const isString = require('./string');
const isUint8Array = require('./uint8Array');
const isUndefined = require('./undefined');
@@ -23,6 +25,8 @@ module.exports = {
isFunction,
isHex,
isInstanceOf,
isIp,
isObject,
isNull,
isNumber,
isString,
+1 -1
View File
@@ -10,7 +10,7 @@
@example
import { isInstanceOf } from '@polkadot/util';
console.log('isinstanceOf', isInstanceOf(new Array(0), Array)); // => true
console.log('isInstanceOf', isInstanceOf(new Array(0), Array)); // => true
*/
module.exports = function isInstanceOf (value: any, clazz: any): boolean {
return value instanceof clazz;
+30
View File
@@ -0,0 +1,30 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
type IpTypes = 'v4' | 'v6';
const ipRegex = require('ip-regex');
/**
@name isIp
@signature isIp (value: any, type?: 'v4' | 'v6'): boolean
@summary Tests if the value is a valid IP address
@description
Checks to see if the value is a valid IP address. Optionally check for either v4/v6
@example
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
*/
module.exports = function isIp (value: any, type?: IpTypes): boolean {
if (type === 'v4') {
return ipRegex.v4({ exact: true }).test(value);
} else if (type === 'v6') {
return ipRegex.v6({ exact: true }).test(value);
}
return ipRegex({ exact: true }).test(value);
};
+47
View File
@@ -0,0 +1,47 @@
// ISC, Copyright 2017 Jaco Greeff
const { isIp } = require('./index');
describe('isIp', () => {
it('returns false when non-ip found', () => {
expect(
isIp('not.an.ip.1')
).toEqual(false);
});
it('returns true on IPv4 addresses', () => {
expect(
isIp('192.168.0.1')
).toEqual(true);
});
it('returns true on IPv4 addresses (IPV4 type)', () => {
expect(
isIp('192.168.0.1', 'v4')
).toEqual(true);
});
it('returns false on IPv4 addresses (IPv6 type)', () => {
expect(
isIp('192.168.0.1', 'v6')
).toEqual(false);
});
it('returns true on IPv6 addresses', () => {
expect(
isIp('1:2:3:4:5:6:7:8')
).toEqual(true);
});
it('returns true on IPv6 addresses (IPv6 type)', () => {
expect(
isIp('1:2:3:4:5:6:7:8', 'v6')
).toEqual(true);
});
it('returns false on IPv6 addresses (IPv4 type)', () => {
expect(
isIp('1:2:3:4:5:6:7:8', 'v4')
).toEqual(false);
});
});
+18
View File
@@ -0,0 +1,18 @@
// ISC, Copyright 2017 Jaco Greeff
// @flow
/**
@name isObject
@signature isObject (value: any): boolean
@summary Tests for an `object`.
@description
Checks to see if the input value is a JavaScript object.
@example
import { isObject } from '@polkadot/util';
isObject({}); // => true
isObject('something'); // => false
*/
module.exports = function isObject (value: any): boolean {
return typeof value === 'object';
};
+17
View File
@@ -0,0 +1,17 @@
// ISC, Copyright 2017 Jaco Greeff
const { isObject } = require('./index');
describe('isObject', () => {
it('returns true on valid objects', () => {
expect(
isObject({})
).toEqual(true);
});
it('returns false on invalid objects', () => {
expect(
isObject('notAnObject')
).toEqual(false);
});
});
+13 -3
View File
@@ -54,9 +54,9 @@
lodash "^4.2.0"
to-fast-properties "^2.0.0"
"@polkadot/api-jsonrpc@^0.6.3":
version "0.6.4"
resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.6.4.tgz#baf04b774a36fe9e97e106f2fd3c9a29a76f5661"
"@polkadot/api-jsonrpc@^0.6.5":
version "0.6.5"
resolved "https://registry.yarnpkg.com/@polkadot/api-jsonrpc/-/api-jsonrpc-0.6.5.tgz#fa78923847e17e0c50c431b1935478e226284e1d"
dependencies:
babel-runtime "^6.26.0"
@@ -2217,6 +2217,10 @@ 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:
version "2.1.0"
resolved "https://registry.yarnpkg.com/ip-regex/-/ip-regex-2.1.0.tgz#fa78bf5d2e6913c911ce9f819ee5146bb6d844e9"
is-alphabetical@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/is-alphabetical/-/is-alphabetical-1.0.1.tgz#c77079cc91d4efac775be1034bf2d243f95e6f08"
@@ -2318,6 +2322,12 @@ 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"