Add Blake2s & XXHash utility functions (#66)
* Initial split of util & util-crypto * Add initial XXHash version * Blake2s wit tests & documentation * Add sha512 (from NaCl) * Add randomBytes (from NaCl) * Flow fixes * Update documentation * Update latest @polkadot/dev
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const isNull = require('../is/null');
|
||||
const isUndefined = require('../is/undefined');
|
||||
|
||||
/**
|
||||
@name arrayFilter
|
||||
@signature arrayFilter (array: Array<mixed>, allowNulls: boolean = true): Array<mixed>
|
||||
@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<mixed>, allowNulls: boolean = true): Array<mixed> {
|
||||
return array.filter((value) => {
|
||||
return !isUndefined(value) && (allowNulls || !isNull(value));
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { arrayFilter } = require('./index');
|
||||
|
||||
describe('filterArray', () => {
|
||||
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]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const arrayFilter = require('./filter');
|
||||
|
||||
/**
|
||||
@summary Utility methods that operates on arrays
|
||||
*/
|
||||
module.exports = {
|
||||
arrayFilter
|
||||
};
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const ExtError = require('./ext/error');
|
||||
const isFunction = require('./is/function');
|
||||
|
||||
/**
|
||||
@name assert
|
||||
@signature assert (test: mixed, message: string | () => string, code: number = ExtError.CODES.ASSERT, data: mixed): void
|
||||
@summary Checks for a valid test, if not ExtError is thrown.
|
||||
@description
|
||||
Checks that `test` is a truthy value. If value is falsy (`null`, `undefined`, `false`, ...), it throws an ExtError with the supplied `message` and an optional `code` and `data`. When `test` passes, `true` is returned.
|
||||
@example
|
||||
const assert = require('@polkadot/util/assert');
|
||||
|
||||
assert(true, 'True should be true'); // true returned
|
||||
assert(false, 'False should not be true'); // ExtError thrown
|
||||
assert(false, () => 'message'); // ExtError with 'message'
|
||||
*/
|
||||
module.exports = function assert (test: mixed, message: string | () => string, code: number = ExtError.CODES.ASSERT, data: mixed): boolean {
|
||||
// flowlint-next-line sketchy-null:off
|
||||
if (test) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isFunction(message)) {
|
||||
// $FlowFixMe we have a function here
|
||||
message = message();
|
||||
}
|
||||
|
||||
// $FlowFixMe we have a string here
|
||||
throw new ExtError(message, code, data);
|
||||
};
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { assert } = require('./index');
|
||||
|
||||
describe('assert', () => {
|
||||
it('should not throw an error when test is true', () => {
|
||||
expect(
|
||||
assert(true, 'nothing should be thrown')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('should throw an error when test is not true', () => {
|
||||
expect(
|
||||
() => assert(false, 'error thrown')
|
||||
).toThrow(/error thrown/);
|
||||
});
|
||||
|
||||
it('should throw an error when message: () => string', () => {
|
||||
expect(
|
||||
() => assert(false, () => 'message from function')
|
||||
).toThrow(/message from function/);
|
||||
});
|
||||
|
||||
it('throws a valid constructed ExtError', () => {
|
||||
try {
|
||||
assert(false, 'error', -666, { some: 'data' });
|
||||
} catch (error) {
|
||||
expect(error.code).toEqual(-666);
|
||||
expect(error.data).toEqual({ some: 'data' });
|
||||
expect(error.message).toEqual('error');
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const bnFromHex = require('../hex/toBn');
|
||||
|
||||
/**
|
||||
@name bnFromHex
|
||||
@signature bnFromHex (value?: string): BN
|
||||
@summary Creates a BN.js bignumber object from a hex string.
|
||||
@alias hex/toBn
|
||||
*/
|
||||
module.exports = bnFromHex;
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { bnFromHex } = require('./index');
|
||||
|
||||
describe('bnFromHex', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(bnFromHex)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const bnFromHex = require('./fromHex');
|
||||
const bnToHex = require('./toHex');
|
||||
|
||||
/**
|
||||
@summary Utility methods to convert to and from `BN` objects
|
||||
*/
|
||||
module.exports = {
|
||||
bnFromHex,
|
||||
bnToHex
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const hexAddPrefix = require('../hex/addPrefix');
|
||||
|
||||
const ZERO_STR = '0x00';
|
||||
|
||||
/**
|
||||
@name bnToHex
|
||||
@signature bnToHex (value?: BN): string
|
||||
@summary Creates a hex value from a BN.js bignumber object.
|
||||
@description
|
||||
`null` inputs returns a `0x` result, BN values return the actual value as a `0x` prefixed hex value. Anything that is not a BN object throws an error.
|
||||
@example
|
||||
import BN from 'bn.js';
|
||||
import { bnToHex } from '@polkadot/util';
|
||||
|
||||
bnToHex(new BN(0x123456)); // => '0x123456'
|
||||
*/
|
||||
module.exports = function bnToHex (value?: BN): string {
|
||||
if (!value) {
|
||||
return ZERO_STR;
|
||||
}
|
||||
|
||||
return hexAddPrefix(value.toString(16));
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const { bnToHex } = require('./index');
|
||||
|
||||
describe('bnToHex', () => {
|
||||
it('converts BN values to a prefixed hex representation', () => {
|
||||
expect(
|
||||
bnToHex(new BN(128))
|
||||
).toEqual('0x80');
|
||||
});
|
||||
|
||||
it('converts null values to 0x00', () => {
|
||||
expect(
|
||||
bnToHex(null)
|
||||
).toEqual('0x00');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const bufferFromHex = require('../hex/toBuffer');
|
||||
|
||||
/**
|
||||
@name bufferFromHex
|
||||
@signature bufferFromHex (value?: string): Buffer
|
||||
@summary Creates a Buffer object from a hex string.
|
||||
@alias hex/toBuffer
|
||||
*/
|
||||
module.exports = bufferFromHex;
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { bufferFromHex } = require('./index');
|
||||
|
||||
describe('bufferFromHex', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(bufferFromHex)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const bufferFromNumber = require('../number/toBuffer');
|
||||
|
||||
/**
|
||||
@name bufferFromNumber
|
||||
@signature bufferFromNumber (value?: number): Buffer
|
||||
@summary Creates a Buffer object from a number.
|
||||
@alias number/toBuffer
|
||||
*/
|
||||
module.exports = bufferFromNumber;
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { bufferFromNumber } = require('./index');
|
||||
|
||||
describe('bufferFromNumber', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(bufferFromNumber)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const bufferFromU8a = require('../u8a/toBuffer');
|
||||
|
||||
/**
|
||||
@name bufferFromU8a
|
||||
@signature bufferFromU8a (value?: string): Buffer
|
||||
@summary Creates a Buffer object from a Uint8Array.
|
||||
@alias u8a/toBuffer
|
||||
*/
|
||||
module.exports = bufferFromU8a;
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { bufferFromHex } = require('./index');
|
||||
|
||||
describe('bufferFromHex', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(bufferFromHex)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const bufferFromHex = require('./fromHex');
|
||||
const bufferFromNumber = require('./fromNumber');
|
||||
const bufferFromU8a = require('./fromU8a');
|
||||
const bufferToHex = require('./toHex');
|
||||
const bufferToNumber = require('./toNumber');
|
||||
const bufferToU8a = require('./toU8a');
|
||||
|
||||
/**
|
||||
@summary Utility methods to convert to and from `Buffer` objects
|
||||
*/
|
||||
module.exports = {
|
||||
bufferFromHex,
|
||||
bufferFromNumber,
|
||||
bufferFromU8a,
|
||||
bufferToHex,
|
||||
bufferToNumber,
|
||||
bufferToU8a
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const hexAddPrefix = require('../hex/addPrefix');
|
||||
|
||||
const ZERO_HEX = '0x00';
|
||||
|
||||
/**
|
||||
@name bufferToHex
|
||||
@signature bufferToHex (value?: Buffer): string
|
||||
@summary Creates a hex value from a Buffer object.
|
||||
@description
|
||||
`null` inputs returns a `0x` result, `Buffer` values return the actual value as a `0x` prefixed hex value. Anything that is not a `Buffer` object throws an error.
|
||||
@example
|
||||
import { bufferToHex } from '@polkadot/util';
|
||||
|
||||
bufferToHex(Buffer.from([1, 2, 3]); // => 0x010203
|
||||
*/
|
||||
module.exports = function bufferToHex (value?: Buffer): string {
|
||||
if (!value) {
|
||||
return ZERO_HEX;
|
||||
}
|
||||
|
||||
return hexAddPrefix(
|
||||
value.toString('hex')
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { bufferToHex } = require('./index');
|
||||
|
||||
describe('bufferToHex', () => {
|
||||
it('returns 0x00 when the buffer is null', () => {
|
||||
expect(
|
||||
bufferToHex(null)
|
||||
).toEqual('0x00');
|
||||
});
|
||||
|
||||
it('returns the hex value for the buffer', () => {
|
||||
expect(
|
||||
bufferToHex(Buffer.from([128, 0, 10]))
|
||||
).toEqual('0x80000a');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name bufferToNumber
|
||||
@signature bufferToNumber (value?: Buffer): number
|
||||
@summary Creates a Number value from a Buffer object.
|
||||
@description
|
||||
`null` inputs returns an NaN result, `Buffer` values return the actual value as a `Number`.
|
||||
@example
|
||||
import { bufferToNumber } from '@polkadot/util';
|
||||
|
||||
bufferToNumber(Buffer.from([12, 34, 56])); // => 0x123456
|
||||
*/
|
||||
module.exports = function bufferToNumber (buffer?: Buffer): number {
|
||||
if (!buffer || !buffer.length) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
const length = buffer.length;
|
||||
let value = 0;
|
||||
|
||||
for (let index = 0; index < length; ++index) {
|
||||
value = (value * 256) + buffer[index];
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { bufferToNumber } = require('./index');
|
||||
|
||||
describe('bufferToNumber', () => {
|
||||
it('converts nothing to NaN', () => {
|
||||
expect(
|
||||
bufferToNumber()
|
||||
).toEqual(NaN);
|
||||
});
|
||||
|
||||
it('converts empty buffer to NaN', () => {
|
||||
expect(
|
||||
bufferToNumber(Buffer.from([]))
|
||||
).toEqual(NaN);
|
||||
});
|
||||
|
||||
it('converts a buffer to a value', () => {
|
||||
expect(
|
||||
bufferToNumber(Buffer.from([0x12, 0x34]))
|
||||
).toEqual(0x1234);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name bufferToU8a
|
||||
@signature bufferToU8a (value?: Buffer): string
|
||||
@summary Creates a Uint8Array value from a Buffer object.
|
||||
@description
|
||||
`null` inputs returns an empty result, `Buffer` values return the actual value as a `Uint8Array`. Anything that is not a `Buffer` object throws an error.
|
||||
@example
|
||||
import { bufferToU8a } from '@polkadot/util';
|
||||
|
||||
bufferToU8a(Buffer.from([1, 2, 3]));
|
||||
*/
|
||||
module.exports = function bufferToU8a (buffer?: Buffer): Uint8Array {
|
||||
if (!buffer) {
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
|
||||
const array = new Uint8Array(buffer.length);
|
||||
|
||||
for (let index = 0; index < buffer.length; ++index) {
|
||||
array[index] = buffer[index];
|
||||
}
|
||||
|
||||
return array;
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { bufferToU8a } = require('./index');
|
||||
|
||||
describe('bufferToU8a', () => {
|
||||
it('returns an empty buffer when null provided', () => {
|
||||
expect(
|
||||
bufferToU8a(null)
|
||||
).toEqual(new Uint8Array([]));
|
||||
});
|
||||
|
||||
it('returns a Uint8Buffer with the correct values', () => {
|
||||
expect(
|
||||
bufferToU8a(Buffer.from([128, 0, 10]))
|
||||
).toEqual(new Uint8Array([128, 0, 10]));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { ExtErrorInterface } from '../types';
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const UNKNOWN = -99999;
|
||||
|
||||
/**
|
||||
@name ExtError
|
||||
@signature ExtError (message: string = '', code: number = UNKNOWN_ERROR, value: any)
|
||||
@summary Extension to the basic JS Error.
|
||||
@description
|
||||
The built-in JavaScript Error class is extended by adding a code to allow for Error categorization. In addition to the normal `stack`, `message`, the numeric `code` and `data` (mixed types) parameters are available on the object.
|
||||
@example
|
||||
const { ExtError } = require('@polkadot/util');
|
||||
|
||||
throw new ExtError('some message', ExtError.CODES.METHOD_NOT_FOUND); // => error.code = -32601
|
||||
*/
|
||||
module.exports = class ExtError extends Error implements ExtErrorInterface {
|
||||
code: number;
|
||||
data: mixed;
|
||||
message: string;
|
||||
name: string;
|
||||
stack: string;
|
||||
|
||||
constructor (message: string = '', code: number = UNKNOWN, data: mixed) {
|
||||
super();
|
||||
|
||||
const extend = (name: string, value: mixed): void => {
|
||||
Object.defineProperty(this, name, {
|
||||
configurable: true,
|
||||
enumerable: false,
|
||||
value
|
||||
});
|
||||
};
|
||||
|
||||
extend('message', String(message));
|
||||
extend('name', this.constructor.name);
|
||||
extend('data', data);
|
||||
extend('code', code);
|
||||
|
||||
if (isFunction(Error.captureStackTrace)) {
|
||||
Error.captureStackTrace(this, this.constructor);
|
||||
} else {
|
||||
extend('stack', (new Error(message)).stack);
|
||||
}
|
||||
}
|
||||
|
||||
static CODES = {
|
||||
ASSERT: -90009,
|
||||
UNKNOWN,
|
||||
INVALID_JSONRPC: -99998,
|
||||
METHOD_NOT_FOUND: -32601 // Rust client
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,99 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isError = require('../is/error');
|
||||
const { ExtError } = require('./index');
|
||||
|
||||
describe('ExtError', () => {
|
||||
describe('constructor', () => {
|
||||
it('constructs an Error that is still an Error', () => {
|
||||
expect(
|
||||
isError(
|
||||
new ExtError()
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('static', () => {
|
||||
it('exposes the .CODES as a static', () => {
|
||||
expect(
|
||||
Object.keys(ExtError.CODES)
|
||||
).not.toEqual(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('constructor properties', () => {
|
||||
it('sets the .message property', () => {
|
||||
expect(
|
||||
new ExtError('test message').message
|
||||
).toEqual('test message');
|
||||
});
|
||||
|
||||
it("sets the .message to '' when not set", () => {
|
||||
expect(
|
||||
new ExtError().message
|
||||
).toEqual('');
|
||||
});
|
||||
|
||||
it('sets the .code property', () => {
|
||||
expect(
|
||||
new ExtError('test message', 1234).code
|
||||
).toEqual(1234);
|
||||
});
|
||||
|
||||
it('sets the .code to UKNOWN when not set', () => {
|
||||
expect(
|
||||
new ExtError('test message').code
|
||||
).toEqual(ExtError.CODES.UNKNOWN);
|
||||
});
|
||||
|
||||
it('sets the .data property', () => {
|
||||
const data = { some: { value: 'here' } };
|
||||
|
||||
expect(
|
||||
new ExtError('test message', 1234, data).data
|
||||
).toEqual(data);
|
||||
});
|
||||
});
|
||||
|
||||
describe('stack traces', () => {
|
||||
let captureStackTrace;
|
||||
|
||||
beforeEach(() => {
|
||||
captureStackTrace = Error.captureStackTrace;
|
||||
|
||||
Error.captureStackTrace = function (error) {
|
||||
Object.defineProperty(error, 'stack', {
|
||||
configurable: true,
|
||||
get: function getStack () {
|
||||
const value = 'some stack returned';
|
||||
|
||||
Object.defineProperty(this, 'stack', { value });
|
||||
|
||||
return value;
|
||||
}
|
||||
});
|
||||
};
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
Error.captureStackTrace = captureStackTrace;
|
||||
});
|
||||
|
||||
it('captures via captureStackTrace when available', () => {
|
||||
expect(
|
||||
new ExtError().stack
|
||||
).toEqual('some stack returned');
|
||||
});
|
||||
|
||||
it('captures via stack when captureStackTrace not available', () => {
|
||||
Error.captureStackTrace = null;
|
||||
|
||||
expect(
|
||||
new ExtError().stack.length
|
||||
).not.toEqual(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const ExtError = require('./error');
|
||||
|
||||
/**
|
||||
@summary Extensions to basic classes
|
||||
*/
|
||||
module.exports = {
|
||||
ExtError
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const hasPrefix = require('./hasPrefix');
|
||||
|
||||
/**
|
||||
@name hexAddPrefix
|
||||
@signature hexAddPrefix (value: ?string): string
|
||||
@summary Adds the `0x` prefix to string values.
|
||||
@description
|
||||
Returns a `0x` prefixed string from the input value. If the input is already prefixed, it is returned unchanged.
|
||||
@example
|
||||
import { hexAddPrefix } from '@polkadot/util';
|
||||
|
||||
console.log('With prefix', hexAddPrefix('0a0b12')) // => 0x0a0b12
|
||||
*/
|
||||
module.exports = function hexAddPrefix (value: ?string): string {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (value && hasPrefix(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
const prefix = value && value.length % 2 === 1
|
||||
? '0'
|
||||
: '';
|
||||
|
||||
return `0x${prefix}${value || ''}`;
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { hexAddPrefix } = require('./index');
|
||||
|
||||
describe('hexAddPrefix', () => {
|
||||
it('does not add when prefix is available', () => {
|
||||
expect(
|
||||
hexAddPrefix('0x0123')
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('adds the prefix when it is not available', () => {
|
||||
expect(
|
||||
hexAddPrefix('0123')
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('adds extra 0 when length % 2 === 1', () => {
|
||||
expect(
|
||||
hexAddPrefix('123')
|
||||
).toEqual('0x0123');
|
||||
});
|
||||
|
||||
it('returns null as 0x', () => {
|
||||
expect(
|
||||
hexAddPrefix(null)
|
||||
).toEqual('0x');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const ZEROS_256 = '0000000000000000000000000000000000000000000000000000000000000000';
|
||||
|
||||
module.exports = {
|
||||
ZEROS_256
|
||||
};
|
||||
@@ -0,0 +1,42 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const hexAddPrefix = require('./addPrefix');
|
||||
const hexStripPrefix = require('./stripPrefix');
|
||||
const { ZEROS_256 } = require('./constants');
|
||||
|
||||
/**
|
||||
@name hexFixLength
|
||||
@signature hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string
|
||||
@summary Shifts a hex string to a specific bitLength
|
||||
@description
|
||||
Returns a `0x` prefixed string with the specified number of bits contained in the return value. (If bitLength is -1, length checking is not done). Values with more bits are trimmed to the specified length. Input values with less bits are returned as-is by default. When `withPadding` is set, shorter val;ues are padded with `0`.
|
||||
@example
|
||||
import { hexFixLength } from '@polkadot/util';
|
||||
|
||||
console.log('fixed', hexFixLength('0x12', 16)) // => 0x12
|
||||
console.log('fixed', hexFixLength('0x12', 16, true)) // => 0x0012
|
||||
console.log('fixed', hexFixLength('0x0012', 8)) // => 0x12
|
||||
*/
|
||||
module.exports = function hexFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string {
|
||||
const strLength = bitLength / 4;
|
||||
const hexLength = strLength + 2;
|
||||
|
||||
if (bitLength === -1 || value.length === hexLength || (!withPadding && value.length < hexLength)) {
|
||||
return hexAddPrefix(
|
||||
hexStripPrefix(value)
|
||||
);
|
||||
}
|
||||
|
||||
if (value.length > hexLength) {
|
||||
return hexAddPrefix(
|
||||
hexStripPrefix(value).slice(-1 * strLength)
|
||||
);
|
||||
}
|
||||
|
||||
return hexAddPrefix(
|
||||
`${ZEROS_256}${hexStripPrefix(value)}`.slice(-1 * strLength)
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { hexFixLength } = require('./index');
|
||||
|
||||
describe('hexFixLength', () => {
|
||||
it('returns bitLength === -1 as-is', () => {
|
||||
expect(
|
||||
hexFixLength('0x12345678')
|
||||
).toEqual('0x12345678');
|
||||
});
|
||||
|
||||
it('returns bitLength === -1 as-is (adding missing 0)', () => {
|
||||
expect(
|
||||
hexFixLength('0x1234567')
|
||||
).toEqual('0x01234567');
|
||||
});
|
||||
|
||||
it('does not change when bitlength === length', () => {
|
||||
expect(
|
||||
hexFixLength('0x12345678', 32)
|
||||
).toEqual('0x12345678');
|
||||
});
|
||||
|
||||
it('trims values when bitLength > length', () => {
|
||||
expect(
|
||||
hexFixLength('0x12345678', 16)
|
||||
).toEqual('0x5678');
|
||||
});
|
||||
|
||||
it('returns as-is when bitLength < length', () => {
|
||||
expect(
|
||||
hexFixLength('0x1234', 32)
|
||||
).toEqual('0x1234');
|
||||
});
|
||||
|
||||
it('adds zeros when bitLength < length (withPadded)', () => {
|
||||
expect(
|
||||
hexFixLength('0x1234', 32, true)
|
||||
).toEqual('0x00001234');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const hexFromBn = require('../bn/toHex');
|
||||
|
||||
/**
|
||||
@name hexFromBn
|
||||
@signature hexFromBn (value?: BN): string
|
||||
@summary Creates a hex value from a BN.js bignumber object.
|
||||
@alias bn/toHex
|
||||
*/
|
||||
module.exports = hexFromBn;
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { hexFromBn } = require('./index');
|
||||
|
||||
describe('hexFromBn', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(hexFromBn)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const bufferToHex = require('../buffer/toHex');
|
||||
|
||||
/**
|
||||
@name hexFromBuffer
|
||||
@signature hexFromBuffer (value?: Buffer): string
|
||||
@summary Creates a hex value from a Buffer object.
|
||||
@alias buffer/toHex
|
||||
*/
|
||||
module.exports = bufferToHex;
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { hexFromBuffer } = require('./index');
|
||||
|
||||
describe('hexFromBuffer', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(hexFromBuffer)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const hexFromNumber = require('../number/toHex');
|
||||
|
||||
/**
|
||||
@name hexFromNumber
|
||||
@signature hexFromNumber (value?: number): string
|
||||
@summary Creates a hex value from a number.
|
||||
@alias number/toHex
|
||||
*/
|
||||
module.exports = hexFromNumber;
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { hexFromNumber } = require('./index');
|
||||
|
||||
describe('hexFromNumber', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(hexFromNumber)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const isHex = require('../is/hex');
|
||||
|
||||
/**
|
||||
@name hexHasPrefix
|
||||
@signature hexHasPrefix (value: ?string): boolean
|
||||
@summary Tests for the existence of a `0x` prefix.
|
||||
@description
|
||||
Checks for a valid hex input value and if the start matched `0x`
|
||||
@example
|
||||
import { hexHasPrefix } from '@polkadot/util';
|
||||
|
||||
console.log('has prefix', hexHasPrefix('0x1234')); // => true
|
||||
*/
|
||||
module.exports = function hexHasPrefix (value: ?string): boolean {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
return !!(value && isHex(value) && value.substr(0, 2) === '0x');
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { hexHasPrefix } = require('./index');
|
||||
|
||||
describe('hexHasPrefix', () => {
|
||||
it('returns true when hex prefix is found', () => {
|
||||
expect(
|
||||
hexHasPrefix('0x123')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false when no prefix attached', () => {
|
||||
expect(
|
||||
hexHasPrefix('123')
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false when null value supplied', () => {
|
||||
expect(
|
||||
hexHasPrefix(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false when non-string value supplied', () => {
|
||||
expect(
|
||||
hexHasPrefix(false)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const hexAddPrefix = require('./addPrefix');
|
||||
const hexFixLength = require('./fixLength');
|
||||
const hexFromBn = require('./fromBn');
|
||||
const hexFromBuffer = require('./fromBuffer');
|
||||
const hexFromNumber = require('./fromNumber');
|
||||
const hexHasPrefix = require('./hasPrefix');
|
||||
const hexStripPrefix = require('./stripPrefix');
|
||||
const hexToBn = require('./toBn');
|
||||
const hexToBuffer = require('./toBuffer');
|
||||
const hexToNumber = require('./toNumber');
|
||||
|
||||
/**
|
||||
@summary Internal utilities to create and test for hex values
|
||||
*/
|
||||
module.exports = {
|
||||
hexAddPrefix,
|
||||
hexFixLength,
|
||||
hexFromBn,
|
||||
hexFromBuffer,
|
||||
hexFromNumber,
|
||||
hexHasPrefix,
|
||||
hexStripPrefix,
|
||||
hexToBn,
|
||||
hexToBuffer,
|
||||
hexToNumber
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const hasPrefix = require('./hasPrefix');
|
||||
|
||||
/**
|
||||
@name hexStripPrefix
|
||||
@signature hexStripPrefix (value: ?string): string
|
||||
@summary Strips any leading `0x` prefix.
|
||||
@description
|
||||
Tests for the existence of a `0x` prefix, and returns the value without the prefix. Un-prefixed values are returned as-is.
|
||||
@example
|
||||
import { hexStripPrefix } from '@polkadot/util';
|
||||
|
||||
console.log('stripped', hexStripPrefix('0x1234')); // => 1234
|
||||
*/
|
||||
module.exports = function hexStripPrefix (value: ?string): string {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (value && hasPrefix(value)) {
|
||||
return value.substr(2);
|
||||
}
|
||||
|
||||
return value || '';
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { hexStripPrefix } = require('./index');
|
||||
|
||||
describe('hexStripPrefix', () => {
|
||||
it('returns the value as-is when no prefix', () => {
|
||||
expect(
|
||||
hexStripPrefix('01ab')
|
||||
).toEqual('01ab');
|
||||
});
|
||||
|
||||
it('returns an empty string when null value supplied', () => {
|
||||
expect(
|
||||
hexStripPrefix(null)
|
||||
).toEqual('');
|
||||
});
|
||||
|
||||
it('strips the prefix from other strings', () => {
|
||||
expect(
|
||||
hexStripPrefix('0x1223')
|
||||
).toEqual('1223');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const hexStripPrefix = require('./stripPrefix');
|
||||
|
||||
const ZERO_BN = new BN(0);
|
||||
|
||||
/**
|
||||
@name hexToBn
|
||||
@signature hexToBn (value?: string): BN
|
||||
@summary Creates a BN.js bignumber object from a hex string.
|
||||
@description
|
||||
`null` inputs returns a `BN(0)` result. Hex input values return the actual value converted to a BN. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
@example
|
||||
import { hexToBn } from '@polkadot/util';
|
||||
|
||||
hexToBn('0x123480001f'); // => BN(0x123480001f)
|
||||
*/
|
||||
module.exports = function hexToBn (value?: string): BN {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (!value) {
|
||||
return ZERO_BN;
|
||||
}
|
||||
|
||||
return new BN(hexStripPrefix(value), 16);
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const { hexToBn } = require('./index');
|
||||
|
||||
describe('hexToBn', () => {
|
||||
const value = '80';
|
||||
|
||||
it('converts prefixed hex values to BN', () => {
|
||||
expect(
|
||||
hexToBn(`0x${value}`).eq(
|
||||
new BN(value, 16)
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('converts null values to BN(0)', () => {
|
||||
expect(
|
||||
hexToBn(null).eq(
|
||||
new BN(0)
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const hexStripPrefix = require('./stripPrefix');
|
||||
|
||||
/**
|
||||
@name hexToBuffer
|
||||
@signature hexToBuffer (value?: string): Buffer
|
||||
@summary Creates a Buffer object from a hex string.
|
||||
@description
|
||||
`null` inputs returns an empty `Buffer` result. Hex input values return the actual bytes value converted to a Buffer. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
@example
|
||||
import { hexToBuffer } from '@polkadot/util';
|
||||
|
||||
hexToBuffer('0x123480001f'); // Buffer([0x12, 0x34, 0x80, 0x00, 0x1f])
|
||||
*/
|
||||
module.exports = function hexToBuffer (value?: string): Buffer {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (!value) {
|
||||
return Buffer.from([]);
|
||||
}
|
||||
|
||||
return Buffer.from(hexStripPrefix(value), 'hex');
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { hexToBuffer } = require('./index');
|
||||
|
||||
describe('hexToBuffer', () => {
|
||||
it('returns an empty buffer when null provided', () => {
|
||||
expect(
|
||||
hexToBuffer(null)
|
||||
).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('returns a buffer with the correct values', () => {
|
||||
expect(
|
||||
Buffer.from([128, 0, 10]).equals(
|
||||
hexToBuffer('0x80000a')
|
||||
)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const bufferToNumber = require('../buffer/toNumber');
|
||||
const hexToBuffer = require('../hex/toBuffer');
|
||||
|
||||
/**
|
||||
@name hexToNumber
|
||||
@signature hexToNumber (value?: string): number
|
||||
@summary Creates a Number value from a Buffer object.
|
||||
@description
|
||||
`null` inputs returns an NaN result, `hex` values return the actual value as a `Number`.
|
||||
@example
|
||||
import { hexToNumber } from '@polkadot/util';
|
||||
|
||||
hexToNumber('0x1234'); // => 0x1234
|
||||
*/
|
||||
module.exports = function hexToNumber (value?: string): number {
|
||||
// flowlint-next-line sketchy-null-string:off
|
||||
if (!value) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
return bufferToNumber(
|
||||
hexToBuffer(value)
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { hexToNumber } = require('./index');
|
||||
|
||||
describe('hexToNumber', () => {
|
||||
it('converts an empty to NaN', () => {
|
||||
expect(
|
||||
hexToNumber()
|
||||
).toEqual(NaN);
|
||||
});
|
||||
|
||||
it('converts to a number from hex', () => {
|
||||
expect(
|
||||
hexToNumber('0x1234')
|
||||
).toEqual(0x1234);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const array = require('./array');
|
||||
const assert = require('./assert');
|
||||
const bn = require('./bn');
|
||||
const buffer = require('./buffer');
|
||||
const ext = require('./ext');
|
||||
const hex = require('./hex');
|
||||
const is = require('./is');
|
||||
const jsonrpc = require('./jsonrpc');
|
||||
const logger = require('./logger');
|
||||
const number = require('./number');
|
||||
const promisify = require('./promisify');
|
||||
const string = require('./string');
|
||||
const u8a = require('./u8a');
|
||||
|
||||
/**
|
||||
@summary Utility methods for this package are split into groups
|
||||
*/
|
||||
module.exports = Object.assign(
|
||||
{}, array, { assert }, bn, buffer, ext, hex, is, jsonrpc, { logger }, number, { promisify }, string, u8a
|
||||
);
|
||||
@@ -0,0 +1,11 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const util = require('./index');
|
||||
|
||||
describe('index', () => {
|
||||
it('exports ok', () => {
|
||||
expect(util).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
/**
|
||||
@name isBN
|
||||
@signature isBN (value: mixed): boolean
|
||||
@summary Tests for a `BN` object instance.
|
||||
@description
|
||||
Checks to see if the input object is an instance of `BN` (bn.js).
|
||||
@example
|
||||
import BN from 'bn.js';
|
||||
import { isBN } from '@polkadot/util';
|
||||
|
||||
console.log('isBN', isBN(new BN(1))); // => true
|
||||
*/
|
||||
module.exports = function isBN (value: mixed): boolean {
|
||||
return BN.isBN(value);
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const BN = require('bn.js');
|
||||
|
||||
const { isBN } = require('./index');
|
||||
|
||||
describe('isBN', () => {
|
||||
it('returns true when a BN value', () => {
|
||||
expect(
|
||||
isBN(new BN(123))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-BN values', () => {
|
||||
expect(
|
||||
isBN(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name isBoolean
|
||||
@signature isBoolean (value: mixed): boolean
|
||||
@summary Tests for a boolean value.
|
||||
@description
|
||||
Checks to see if the input value is a JavaScript boolean.
|
||||
@example
|
||||
import { isBoolean } from '@polkadot/util';
|
||||
|
||||
isBoolean(false); // => true
|
||||
*/
|
||||
module.exports = function isBoolean (value: mixed): boolean {
|
||||
return typeof value === 'boolean';
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { isBoolean } = require('./index');
|
||||
|
||||
describe('isBoolean', () => {
|
||||
it('returns true on false', () => {
|
||||
expect(
|
||||
isBoolean(false)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on true', () => {
|
||||
expect(
|
||||
isBoolean(true)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid booleans', () => {
|
||||
expect(
|
||||
isBoolean('notABool')
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const isInstanceOf = require('./instanceOf');
|
||||
|
||||
/**
|
||||
@name isBuffer
|
||||
@signature isBuffer (value: mixed): boolean
|
||||
@summary Tests for a `Buffer` object instance.
|
||||
@description
|
||||
Checks to see if the input object is an instance of `Buffer`.
|
||||
@example
|
||||
import { isBuffer } from '@polkadot/util';
|
||||
|
||||
console.log('isBuffer', isBuffer(Buffer.from([]))); // => true
|
||||
*/
|
||||
module.exports = function isBuffer (value: mixed): boolean {
|
||||
return isInstanceOf(value, Buffer);
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { isBuffer } = require('./index');
|
||||
|
||||
describe('isBuffer', () => {
|
||||
it('returns true when a Buffer value', () => {
|
||||
expect(
|
||||
isBuffer(Buffer.from([]))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-Buffer values', () => {
|
||||
expect(
|
||||
isBuffer(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const isInstanceOf = require('./instanceOf');
|
||||
|
||||
/**
|
||||
@name isError
|
||||
@signature isError (value: mixed): boolean
|
||||
@summary Tests for a `Error` object instance.
|
||||
@description
|
||||
Checks to see if the input object is an instance of `Error`.
|
||||
@example
|
||||
import { isError } from '@polkadot/util';
|
||||
|
||||
console.log('isError', isError(new Error('message'))); // => true
|
||||
*/
|
||||
module.exports = function isError (value: mixed): boolean {
|
||||
return isInstanceOf(value, Error);
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { isError } = require('./index');
|
||||
|
||||
describe('isError', () => {
|
||||
it('returns true when an Error value', () => {
|
||||
expect(
|
||||
isError(new Error('testing'))
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-Error values', () => {
|
||||
expect(
|
||||
isError(0)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name isFunction
|
||||
@signature isFunction (value: mixed): boolean
|
||||
@summary Tests for a `function`.
|
||||
@description
|
||||
Checks to see if the input value is a JavaScript function.
|
||||
@example
|
||||
import { isFunction } from '@polkadot/util';
|
||||
|
||||
console.log('isFunction', isFunction(() => false)); // => true
|
||||
*/
|
||||
module.exports = function isFunction (value: mixed): boolean {
|
||||
return typeof value === 'function';
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { isFunction } = require('./index');
|
||||
|
||||
describe('isFunction', () => {
|
||||
it('returns true on valid functions', () => {
|
||||
expect(
|
||||
isFunction(isFunction)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid functions', () => {
|
||||
expect(
|
||||
isFunction('notAFunction')
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const isString = require('./string');
|
||||
|
||||
const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
|
||||
|
||||
/**
|
||||
@name isHex
|
||||
@signature isHex (value: string, bitLength: number = -1): boolean
|
||||
@summary Tests for a hex string.
|
||||
@description
|
||||
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';
|
||||
|
||||
isHex('0x1234'); // => true
|
||||
isHex('0x1234', 8); // => false
|
||||
*/
|
||||
module.exports = function isHex (value: string, bitLength: number = -1): boolean {
|
||||
const isValidHex = isString(value) && HEX_REGEX.test(value);
|
||||
|
||||
if (isValidHex && bitLength !== -1) {
|
||||
return value.length === (2 + bitLength / 4);
|
||||
}
|
||||
|
||||
return isValidHex;
|
||||
};
|
||||
@@ -0,0 +1,45 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { isHex } = require('./index');
|
||||
|
||||
describe('isHex', () => {
|
||||
const test = '123abcd45';
|
||||
|
||||
it('returns true on hex values', () => {
|
||||
expect(
|
||||
isHex(`0x${test}`)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on uppercase values', () => {
|
||||
expect(
|
||||
isHex(`0x${test.toUpperCase()}`)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('return false on hex values unprefixed', () => {
|
||||
expect(
|
||||
isHex(test)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on non-string values', () => {
|
||||
expect(
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const isBN = require('./bn');
|
||||
const isBuffer = require('./buffer');
|
||||
const isBoolean = require('./boolean');
|
||||
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');
|
||||
|
||||
/**
|
||||
@summary Type checking utilities
|
||||
*/
|
||||
module.exports = {
|
||||
isBN,
|
||||
isBoolean,
|
||||
isBuffer,
|
||||
isError,
|
||||
isFunction,
|
||||
isHex,
|
||||
isInstanceOf,
|
||||
isIp,
|
||||
isObject,
|
||||
isNull,
|
||||
isNumber,
|
||||
isString,
|
||||
isUint8Array,
|
||||
isUndefined
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name isInstanceOf
|
||||
@signature isInstanceOf (value: mixed, clazz: Class<mixed>): boolean
|
||||
@summary Tests for a instance of a class.
|
||||
@description
|
||||
Checks to see if the input value is an instance of the test class.
|
||||
@example
|
||||
import { isInstanceOf } from '@polkadot/util';
|
||||
|
||||
console.log('isInstanceOf', isInstanceOf(new Array(0), Array)); // => true
|
||||
*/
|
||||
module.exports = function isInstanceOf (value: mixed, clazz: Class<mixed>): boolean {
|
||||
return value instanceof clazz;
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { isInstanceOf } = require('./index');
|
||||
|
||||
describe('isInstanceOf', () => {
|
||||
it('returns true on real instances', () => {
|
||||
expect(
|
||||
isInstanceOf(new Array(2), Array)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-allocated instances', () => {
|
||||
expect(
|
||||
isInstanceOf([], Array)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-instances', () => {
|
||||
expect(
|
||||
isInstanceOf('array', Array)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false when class not specified', () => {
|
||||
expect(
|
||||
isInstanceOf('array', Array)
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on wrong class type', () => {
|
||||
expect(
|
||||
isInstanceOf(new Array(2), String)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
type IpTypes = 'v4' | 'v6';
|
||||
|
||||
const ipRegex = require('ip-regex');
|
||||
|
||||
/**
|
||||
@name isIp
|
||||
@signature isIp (value: string, 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: string, 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);
|
||||
};
|
||||
@@ -0,0 +1,49 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name isNull
|
||||
@signature isNull (value: mixed): boolean
|
||||
@summary Tests for a `null` values.
|
||||
@description
|
||||
Checks to see if the input value is `null`.
|
||||
@example
|
||||
import { isNull } from '@polkadot/util';
|
||||
|
||||
console.log('isNull', isNull(null)); // => true
|
||||
*/
|
||||
module.exports = function isNull (value: mixed): boolean {
|
||||
return value === null;
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { isNull } = require('./index');
|
||||
|
||||
describe('isNull', () => {
|
||||
it('returns true when a null value', () => {
|
||||
expect(
|
||||
isNull(null)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on non-null values', () => {
|
||||
expect(
|
||||
isNull()
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name isNumber
|
||||
@signature isNumber (value: mixed): boolean
|
||||
@summary Tests for a JavaScript number.
|
||||
@description
|
||||
Checks to see if the input value is a valid number.
|
||||
@example
|
||||
import { isNumber } from '@polkadot/util';
|
||||
|
||||
console.log('isNumber', isNumber(1234)); // => true
|
||||
*/
|
||||
module.exports = function isNumber (value: mixed): boolean {
|
||||
return typeof value === 'number';
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { isNumber } = require('./index');
|
||||
|
||||
describe('isNumber', () => {
|
||||
it('returns true on valid numbers', () => {
|
||||
expect(
|
||||
isNumber(2)
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid numbers', () => {
|
||||
expect(
|
||||
isNumber('2')
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name isObject
|
||||
@signature isObject (value: mixed): 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: mixed): boolean {
|
||||
return typeof value === 'object';
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name isString
|
||||
@signature isString (value: mixed): boolean
|
||||
@summary Tests for a string.
|
||||
@description
|
||||
Checks to see if the input value is a JavaScript string.
|
||||
@example
|
||||
import { isString } from '@polkadot/util';
|
||||
|
||||
console.log('isString', isString('test')); // => true
|
||||
*/
|
||||
module.exports = function isString (value: mixed): boolean {
|
||||
return typeof value === 'string';
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { isString } = require('./index');
|
||||
|
||||
describe('isString', () => {
|
||||
it('returns true on valid strings', () => {
|
||||
expect(
|
||||
isString('123')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns true on empty strings', () => {
|
||||
expect(
|
||||
isString('')
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on invalid numbers', () => {
|
||||
expect(
|
||||
isString(2)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const isInstanceOf = require('./instanceOf');
|
||||
|
||||
/**
|
||||
@name isUint8Array
|
||||
@signature isUint8Array (value: mixed): boolean
|
||||
@summary Tests for a `Uint8Array` object instance.
|
||||
@description
|
||||
Checks to see if the input object is an instance of `Uint8Array`.
|
||||
@example
|
||||
import { isUint8Array } from '@polkadot/util';
|
||||
|
||||
console.log('isUint8Array', isUint8Array([])); // => false
|
||||
*/
|
||||
module.exports = function isUint8Array (value: mixed): boolean {
|
||||
return isInstanceOf(value, Uint8Array);
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { isUint8Array } = require('./index');
|
||||
|
||||
describe('isUint8Array', () => {
|
||||
it('returns false on undefined values', () => {
|
||||
expect(
|
||||
isUint8Array()
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns false on Array values', () => {
|
||||
expect(
|
||||
isUint8Array([])
|
||||
).toEqual(false);
|
||||
});
|
||||
|
||||
it('returns true on Uint8Array values', () => {
|
||||
expect(
|
||||
isUint8Array(new Uint8Array([]))
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name isUndefined
|
||||
@signature isUndefined (value: mixed): boolean
|
||||
@summary Tests for a `undefined` values.
|
||||
@description
|
||||
Checks to see if the input value is `undefined`.
|
||||
@example
|
||||
import { isUndefined } from '@polkadot/util';
|
||||
|
||||
console.log('isUndefined', isUndefined(void(0))); // => true
|
||||
*/
|
||||
module.exports = function isUndefined (value: mixed): boolean {
|
||||
return typeof value === 'undefined';
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { isUndefined } = require('./index');
|
||||
|
||||
describe('isUndefined', () => {
|
||||
it('returns true on undefined values', () => {
|
||||
expect(
|
||||
isUndefined()
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('returns false on defined values', () => {
|
||||
expect(
|
||||
isUndefined(null)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const jsonrpcSignature = require('./signature');
|
||||
|
||||
/**
|
||||
@summary Convenience functions for values from @polkadot/api-jsonrpc
|
||||
*/
|
||||
module.exports = {
|
||||
jsonrpcSignature
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { InterfaceInputType, InterfaceOutputType } from '@polkadot/api-jsonrpc/types';
|
||||
|
||||
function formatParam (param: InterfaceInputType | InterfaceOutputType): string {
|
||||
// flowlint-next-line sketchy-null-mixed:off,sketchy-null-string:off
|
||||
return param.name
|
||||
// $FlowFixMe InterfaceInputType found, has name
|
||||
? `${param.name}: ${param.type}`
|
||||
: param.type;
|
||||
}
|
||||
|
||||
/**
|
||||
@name jsonrpcSignature
|
||||
@signature jsonrpcSignature (name: string, _inputs: Array<InterfaceInputType>, _output: InterfaceOutputType): string
|
||||
@summary Returns a string representation of the method with inputs and outputs.
|
||||
@description
|
||||
Formats the name, inputs and outputs into a human-readable string. This contains the input parameter names, input types and output type.
|
||||
@example
|
||||
import { jsonrpcSignature } from '@polkadot/util';
|
||||
|
||||
console.log(jsonrpcSignature(
|
||||
'test_method', [{ name: 'dest', type: 'Address' }], { type: 'Address' }
|
||||
)); // => test_method (destination: Address): Address
|
||||
*/
|
||||
module.exports = function jsonrpcSignature (name: string, _inputs: Array<InterfaceInputType>, _output: InterfaceOutputType): string {
|
||||
const inputs: string = _inputs.map(formatParam).join(', ');
|
||||
const output: string = formatParam(_output);
|
||||
|
||||
return `${name} (${inputs}): ${output}`;
|
||||
};
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { jsonrpcSignature } = require('./index');
|
||||
|
||||
describe('jsonrpcSignature', () => {
|
||||
it('formats the signature according to the specification', () => {
|
||||
expect(
|
||||
jsonrpcSignature(
|
||||
'test_method',
|
||||
[
|
||||
{ name: 'b', type: 'A' },
|
||||
{ name: 'a', type: 'B' },
|
||||
{ type: 'C' },
|
||||
{ type: 'D' }
|
||||
],
|
||||
{ type: 'A' }
|
||||
)
|
||||
).toEqual('test_method (b: A, a: B, C, D): A');
|
||||
});
|
||||
|
||||
it('empty inputs format correctly', () => {
|
||||
expect(
|
||||
jsonrpcSignature(
|
||||
'test_method',
|
||||
[],
|
||||
{ type: 'A' }
|
||||
)
|
||||
).toEqual('test_method (): A');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,46 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
import type { Logger } from './types';
|
||||
|
||||
type ConsoleType = 'error' | 'log' | 'warn';
|
||||
|
||||
function apply (log: ConsoleType, type: string, values: Array<mixed>): void {
|
||||
console[log].apply(console, [new Date().toString(), type].concat(values));
|
||||
}
|
||||
|
||||
/**
|
||||
@name Logger
|
||||
@signature logger (type: string): Logger
|
||||
@summary Creates a consistent log interface for messages
|
||||
@description
|
||||
Returns a `Logger` that has `.log`, `.error`, `.warn` and `.debug` (constrolled with environment `DEBUG=typeA,typeB`) methods. Logging is done with a consistent prefix (type of logger, date) followed by the actual message using the underlying console.
|
||||
@example
|
||||
const l = require('@polkadot/util/logger')('test');
|
||||
|
||||
l.log('blah'); // <date> TEST: blah
|
||||
*/
|
||||
module.exports = function logger (_type: string): Logger {
|
||||
const type = ` ${_type.toUpperCase()}:`.slice(-16);
|
||||
let isDebug;
|
||||
|
||||
try {
|
||||
// $FlowFixMe this can fail (undefined/null) - hence the try/catch
|
||||
const debugList = process.env.DEBUG.split(',');
|
||||
|
||||
isDebug = !!debugList.find((entry) => _type.indexOf(entry) === 0);
|
||||
} catch (error) {
|
||||
isDebug = false;
|
||||
}
|
||||
|
||||
return {
|
||||
debug: isDebug
|
||||
? (...values: Array<mixed>): void => apply('log', type, values)
|
||||
: (...values: Array<mixed>): void => void 0,
|
||||
error: (...values: Array<mixed>): void => apply('error', type, values),
|
||||
log: (...values: Array<mixed>): void => apply('log', type, values),
|
||||
warn: (...values: Array<mixed>): void => apply('warn', type, values)
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,113 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { logger } = require('./index');
|
||||
|
||||
describe('logger', () => {
|
||||
let dateMatch;
|
||||
let prefixMatch;
|
||||
let l;
|
||||
let ln;
|
||||
let spy;
|
||||
|
||||
beforeEach(() => {
|
||||
ln = logger('notDebug');
|
||||
|
||||
process.env.DEBUG = 'test';
|
||||
|
||||
l = logger('test');
|
||||
|
||||
spy = {
|
||||
error: jest.fn(),
|
||||
log: jest.fn(),
|
||||
warn: jest.fn()
|
||||
};
|
||||
global.console = spy;
|
||||
|
||||
dateMatch = expect.stringMatching(/20[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/);
|
||||
prefixMatch = expect.stringMatching(/TEST:/);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('logs to console.error with .error', () => {
|
||||
l.error('console.error test');
|
||||
|
||||
expect(spy.error).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
prefixMatch,
|
||||
'console.error test'
|
||||
);
|
||||
});
|
||||
|
||||
it('logs to console.warn with .warn', () => {
|
||||
l.warn('console.warn test');
|
||||
|
||||
expect(spy.warn).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
prefixMatch,
|
||||
'console.warn test'
|
||||
);
|
||||
});
|
||||
|
||||
it('logs to console.log with .log', () => {
|
||||
l.log('console.log test');
|
||||
|
||||
expect(spy.log).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
prefixMatch,
|
||||
'console.log test'
|
||||
);
|
||||
});
|
||||
|
||||
it('logs to with mulptiple values', () => {
|
||||
l.log('test', 'a', 2);
|
||||
|
||||
expect(spy.log).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
prefixMatch,
|
||||
'test',
|
||||
'a',
|
||||
2
|
||||
);
|
||||
});
|
||||
|
||||
it('does debug log when DEBUG specified', () => {
|
||||
l.debug('test');
|
||||
|
||||
expect(spy.log).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
prefixMatch,
|
||||
'test'
|
||||
);
|
||||
});
|
||||
|
||||
it('does debug log when DEBUG partial specified', () => {
|
||||
l = logger('testing');
|
||||
l.debug('test');
|
||||
|
||||
expect(spy.log).toHaveBeenCalledWith(
|
||||
dateMatch,
|
||||
expect.stringMatching(/TESTING:/),
|
||||
'test'
|
||||
);
|
||||
});
|
||||
|
||||
it('does not debug log when non-matching DEBUG specified', () => {
|
||||
process.env.DEBUG = 'blah';
|
||||
|
||||
l = logger('test');
|
||||
l.debug('test');
|
||||
|
||||
expect(spy.log).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('does not debug log when no DEBUG specified', () => {
|
||||
ln.debug('test');
|
||||
|
||||
expect(spy.log).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const numberFromBuffer = require('../buffer/toNumber');
|
||||
|
||||
/**
|
||||
@name numberFromBuffer
|
||||
@signature numberFromBuffer (value?: Buffer): number
|
||||
@summary Creates a Number value from a Buffer object.
|
||||
@alias buffer/toNumber
|
||||
*/
|
||||
module.exports = numberFromBuffer;
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { numberFromBuffer } = require('./index');
|
||||
|
||||
describe('numberFromBuffer', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(numberFromBuffer)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const numberFromHex = require('../hex/toNumber');
|
||||
|
||||
/**
|
||||
@name numberFromHex
|
||||
@signature numberFromHex (value?: Hex): number
|
||||
@summary Creates a Number value from a Buffer object.
|
||||
@alias hex/toNumber
|
||||
*/
|
||||
module.exports = numberFromHex;
|
||||
@@ -0,0 +1,15 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const isFunction = require('../is/function');
|
||||
|
||||
const { numberFromHex } = require('./index');
|
||||
|
||||
describe('numberFromHex', () => {
|
||||
it('exists as a function', () => {
|
||||
expect(
|
||||
isFunction(numberFromHex)
|
||||
).toEqual(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const numberFromBuffer = require('./fromBuffer');
|
||||
const numberFromHex = require('./fromHex');
|
||||
const numberToBuffer = require('./toBuffer');
|
||||
const numberToHex = require('./toHex');
|
||||
|
||||
/**
|
||||
@summary Utility methods to convert to and from `number` values
|
||||
*/
|
||||
module.exports = {
|
||||
numberFromBuffer,
|
||||
numberFromHex,
|
||||
numberToBuffer,
|
||||
numberToHex
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const hexToBuffer = require('../hex/toBuffer');
|
||||
const isNull = require('../is/null');
|
||||
const isUndefined = require('../is/undefined');
|
||||
const numberToHex = require('./toHex');
|
||||
|
||||
/**
|
||||
@name numberToBuffer
|
||||
@signature numberToBuffer (value?: number): Buffer
|
||||
@summary Creates a Buffer object from a number.
|
||||
@description
|
||||
`null`/`undefined`/`NaN` inputs returns an empty `Buffer` result. `number` input values return the actual bytes value converted to a `Buffer`.
|
||||
@example
|
||||
import { numberToBuffer } from '@polkadot/util';
|
||||
|
||||
const buffer = numberToBuffer(0x1234); // => Buffer.from([0x12, 0x34])
|
||||
*/
|
||||
module.exports = function numberToBuffer (value?: number): Buffer {
|
||||
if (isUndefined(value) || isNull(value) || isNaN(value)) {
|
||||
return Buffer.from([]);
|
||||
}
|
||||
|
||||
return hexToBuffer(
|
||||
numberToHex(value)
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { numberToBuffer } = require('./index');
|
||||
|
||||
describe('numberToBuffer', () => {
|
||||
it('converts undefined to empty', () => {
|
||||
expect(
|
||||
numberToBuffer()
|
||||
).toEqual(Buffer.from([]));
|
||||
});
|
||||
|
||||
it('converts null to empty', () => {
|
||||
expect(
|
||||
numberToBuffer(null)
|
||||
).toEqual(Buffer.from([]));
|
||||
});
|
||||
|
||||
it('converts NaN to empty', () => {
|
||||
expect(
|
||||
numberToBuffer(NaN)
|
||||
).toEqual(Buffer.from([]));
|
||||
});
|
||||
|
||||
it('converts values to the buffer', () => {
|
||||
expect(
|
||||
numberToBuffer(0x3456)
|
||||
).toEqual(Buffer.from([0x34, 0x56]));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const hexAddPrefix = require('../hex/addPrefix');
|
||||
const isNull = require('../is/null');
|
||||
const isUndefined = require('../is/undefined');
|
||||
|
||||
/**
|
||||
@name numberToHex
|
||||
@signature numberToHex (value?: number): string
|
||||
@summary Creates a hex value from a number.
|
||||
@description
|
||||
`null`/`undefined`/`NaN` inputs returns an empty `0x` result. `number` input values return the actual bytes value converted to a `hex`.
|
||||
@example
|
||||
import { numberToHex } from '@polkadot/util';
|
||||
|
||||
const hex = numberToHex(0x1234); // => '0x1234'
|
||||
*/
|
||||
module.exports = function numberToHex (value?: number): string {
|
||||
if (isUndefined(value) || isNull(value) || isNaN(value)) {
|
||||
return '0x';
|
||||
}
|
||||
|
||||
return hexAddPrefix(
|
||||
(value || 0).toString(16)
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { numberToHex } = require('./index');
|
||||
|
||||
describe('numberToHex', () => {
|
||||
it('converts undefined to 0x', () => {
|
||||
expect(
|
||||
numberToHex()
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('converts null to 0x', () => {
|
||||
expect(
|
||||
numberToHex(null)
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('converts Nan to 0x', () => {
|
||||
expect(
|
||||
numberToHex(NaN)
|
||||
).toEqual('0x');
|
||||
});
|
||||
|
||||
it('converts 0 to 0x00', () => {
|
||||
expect(
|
||||
numberToHex(0)
|
||||
).toEqual('0x00');
|
||||
});
|
||||
|
||||
it('converts number to hex', () => {
|
||||
expect(
|
||||
numberToHex(0x1245)
|
||||
).toEqual('0x1245');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name promisify
|
||||
@signature function promisify (this: ?Object, fn: Function, ...params: Array<mixed>): Promise<any>
|
||||
@summary Wraps an async callback into a `Promise`
|
||||
@description
|
||||
Wraps the supplied async function `fn` that has a standard JS callback `(error: Error, result: any)` into a `Promise`, passing the supplied parameters. When `error` is set, the Promise is rejected, else the Promise resolves with the `result` value.
|
||||
@example
|
||||
const promisify = require('@polkadot/util/promisify');
|
||||
|
||||
await promisify(null, ((a, cb) => cb(null, a), true); // resolves with `true`
|
||||
await promisify(null, (cb) => cb(new Error('error!'))); // rejects with `error!`
|
||||
*/
|
||||
// flowlint-next-line unclear-type:off
|
||||
module.exports = function promisify (self: ?Object, fn: Function, ...params: Array<mixed>): Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
fn.apply(self, params.concat([
|
||||
// flowlint-next-line unclear-type:off
|
||||
(error: ?Error, result: any): void => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(result);
|
||||
}
|
||||
}
|
||||
]));
|
||||
});
|
||||
};
|
||||
@@ -0,0 +1,48 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { promisify } = require('./index');
|
||||
|
||||
describe('promisify', () => {
|
||||
it('handles functions with no parameters (resolve)', () => {
|
||||
const fn = (cb) => cb(null, [true, 'test', 1]);
|
||||
|
||||
return promisify(null, fn).then((result) => {
|
||||
expect(result).toEqual([true, 'test', 1]);
|
||||
});
|
||||
});
|
||||
|
||||
it('handles functions with no parameters (reject)', () => {
|
||||
const fn = (cb) => cb(new Error('test reject'));
|
||||
|
||||
return promisify(null, fn).catch((error) => {
|
||||
expect(error.message).toEqual('test reject');
|
||||
});
|
||||
});
|
||||
|
||||
it('handles functions with parameters (resolve)', () => {
|
||||
const fn = (a, b, c, cb) => cb(null, [a, b, c]);
|
||||
|
||||
return promisify(null, fn, 2, false, null).then((result) => {
|
||||
expect(result).toEqual([2, false, null]);
|
||||
});
|
||||
});
|
||||
|
||||
it('handles functions with parameters (reject)', () => {
|
||||
const fn = (a, b, c, cb) => cb(new Error(`test reject: ${a},${b},${c}`));
|
||||
|
||||
return promisify(null, fn, 3, 'string', true).catch((error) => {
|
||||
expect(error.message).toEqual('test reject: 3,string,true');
|
||||
});
|
||||
});
|
||||
|
||||
it('applies the correct this argument', () => {
|
||||
const self = { something: 'something' };
|
||||
|
||||
return promisify(self, function (cb) {
|
||||
expect(this).toEqual(self);
|
||||
cb();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const stringShorten = require('./shorten');
|
||||
|
||||
/**
|
||||
@summary Utility methods to convert to work with `string` values
|
||||
*/
|
||||
module.exports = {
|
||||
stringShorten
|
||||
};
|
||||
@@ -0,0 +1,26 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@name stringShorten
|
||||
@signature stringShorten (value: mixed, prefixLength: number = 8): string
|
||||
@summary Returns a string with maximum length
|
||||
@description
|
||||
Checks the string against the `prefixLength`, if longer than dopuble this, shortens it by placing `..` in the middle of it
|
||||
@example
|
||||
import { stringShorten } from '@polkadot/util';
|
||||
|
||||
stringShorten('1234567890', 2); // => 12..90
|
||||
*/
|
||||
module.exports = function stringShorten (_value: mixed, prefixLength: number = 6): string {
|
||||
// $FlowFixMe coerced into string (by any means)
|
||||
const value = `${_value}`;
|
||||
|
||||
if (value.length <= 2 + 2 * prefixLength) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return `${value.substr(0, prefixLength)}..${value.slice(-prefixLength)}`;
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const { stringShorten } = require('./index');
|
||||
|
||||
describe('stringShorten', () => {
|
||||
it('returns the value as-is when <= maxLength', () => {
|
||||
expect(
|
||||
stringShorten('0123456789', 4)
|
||||
).toEqual('0123456789');
|
||||
});
|
||||
|
||||
it('returns the shortened value when > maxLength', () => {
|
||||
expect(
|
||||
stringShorten('0123456789', 3)
|
||||
).toEqual('012..789');
|
||||
});
|
||||
|
||||
it('returns undefined as undefined', () => {
|
||||
expect(
|
||||
stringShorten()
|
||||
).toEqual('undefined');
|
||||
});
|
||||
|
||||
it('returns non-string values as strings', () => {
|
||||
expect(
|
||||
stringShorten(12345678, 2)
|
||||
).toEqual('12..78');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,34 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const deasync = require('deasync');
|
||||
|
||||
const isError = require('./is/error');
|
||||
const isUndefined = require('./is/undefined');
|
||||
|
||||
// FIXME: This is currently for Node environments only - for Browser environments deasync is not available, we need a different solution.
|
||||
|
||||
// flowlint-next-line unclear-type:off
|
||||
module.exports = function syncify (promise: Promise<any>): any {
|
||||
let result;
|
||||
|
||||
promise
|
||||
.catch((error) => error)
|
||||
.then((_result) => {
|
||||
result = _result;
|
||||
});
|
||||
|
||||
let ms = 0;
|
||||
|
||||
while (isUndefined(result)) {
|
||||
deasync.sleep(ms++);
|
||||
}
|
||||
|
||||
if (isError(result)) {
|
||||
throw result;
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
@@ -0,0 +1,27 @@
|
||||
// Copyright 2017-2018 Jaco Greeff
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
const syncify = require('./syncify');
|
||||
|
||||
describe('syncify', () => {
|
||||
it('returns the result of the promise', () => {
|
||||
expect(
|
||||
syncify(
|
||||
new Promise((resolve) => {
|
||||
setTimeout(() => resolve(12345), 100);
|
||||
})
|
||||
)
|
||||
).toEqual(12345);
|
||||
});
|
||||
|
||||
it('throws promise exceptions', () => {
|
||||
expect(
|
||||
() => syncify(
|
||||
new Promise((resolve, reject) => {
|
||||
setTimeout(() => reject(new Error('test reject error')), 100);
|
||||
})
|
||||
)
|
||||
).toThrow(/test reject error/);
|
||||
});
|
||||
});
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user