Converts tests to TypeScript (#300)
This commit is contained in:
@@ -7,7 +7,6 @@ import isUndefined from '../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
|
||||
|
||||
@@ -7,9 +7,10 @@ import isFunction from './is/function';
|
||||
|
||||
type MessageFn = () => string;
|
||||
|
||||
type Falsy = null | undefined | false | 0 | ''; // No NaN type
|
||||
|
||||
/**
|
||||
* @name assert
|
||||
* @signature assert (test: any, message: string | () => string, code: number = ExtError.CODES.ASSERT, data: any): 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.
|
||||
@@ -24,7 +25,7 @@ type MessageFn = () => string;
|
||||
* assert(false, () => 'message'); // ExtError with 'message'
|
||||
* ```
|
||||
*/
|
||||
export default function assert (test: any, message: string | MessageFn, code: number = ExtError.CODES.ASSERT, data?: any): boolean {
|
||||
export default function assert<T> (test: Falsy | T, message: string | MessageFn, code: number = ExtError.CODES.ASSERT, data?: any): test is T {
|
||||
if (test) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import bnFromHex from '../hex/toBn';
|
||||
|
||||
/**
|
||||
* @name bnFromHex
|
||||
* @signature bnFromHex (value?: string): BN
|
||||
* @summary Creates a BN.js bignumber object from a hex string.
|
||||
* @alias hex/toBn
|
||||
*/
|
||||
|
||||
@@ -6,7 +6,6 @@ import BN from 'bn.js';
|
||||
|
||||
/**
|
||||
* @name bnToBn
|
||||
* @signature bnToBn (value?: BN : number): BN
|
||||
* @summary Creates a BN value from a BN.js bignumber or number input.
|
||||
* @description
|
||||
* `null` inputs returns a `0x0` result, BN values returns the value, numnbers returns a BN representation.
|
||||
|
||||
@@ -17,7 +17,6 @@ interface Options extends ToBnOptions {
|
||||
|
||||
/**
|
||||
* @name bnToHex
|
||||
* @signature bnToHex (value?: BN, bitLength: number = -1): 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. With `bitLength` set, it fixes the number to the specified length.
|
||||
|
||||
@@ -14,7 +14,6 @@ interface Options extends ToBnOptions {
|
||||
|
||||
/**
|
||||
* @name bnToU8a
|
||||
* @signature bnToU8a (value?: BN | number, bitLength: number = -1, isLE: boolean = false): Uint8Array
|
||||
* @summary Creates a Uint8Array object from a BN.
|
||||
* @description
|
||||
* `null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `BN` input values return the actual bytes value converted to a `Uint8Array`. Optionally convert using little-endian format if `isLE` is set.
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
/**
|
||||
* @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.
|
||||
@@ -17,7 +16,7 @@
|
||||
* bufferToU8a(Buffer.from([1, 2, 3]));
|
||||
* ```
|
||||
*/
|
||||
export default function bufferToU8a (buffer?: Buffer | number[]): Uint8Array {
|
||||
export default function bufferToU8a (buffer?: Buffer | number[] | null): Uint8Array {
|
||||
if (!buffer) {
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ describe('ExtError', () => {
|
||||
});
|
||||
|
||||
describe('stack traces', () => {
|
||||
let captureStackTrace;
|
||||
let captureStackTrace: any;
|
||||
|
||||
beforeEach(() => {
|
||||
captureStackTrace = Error.captureStackTrace;
|
||||
@@ -89,7 +89,7 @@ describe('ExtError', () => {
|
||||
});
|
||||
|
||||
it('captures via stack when captureStackTrace not available', () => {
|
||||
Error.captureStackTrace = null;
|
||||
Error.captureStackTrace = null as any; // grrr
|
||||
|
||||
expect(
|
||||
new ExtError().stack.length
|
||||
@@ -18,7 +18,6 @@ function extend (that: any, name: string, value: any): void {
|
||||
|
||||
/**
|
||||
* @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` (any types) parameters are available on the object.
|
||||
|
||||
@@ -6,7 +6,6 @@ import hasPrefix from './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.
|
||||
@@ -19,7 +18,7 @@ import hasPrefix from './hasPrefix';
|
||||
* console.log('With prefix', hexAddPrefix('0a0b12')); // => 0x0a0b12
|
||||
* ```
|
||||
*/
|
||||
export default function hexAddPrefix (value: string | null | undefined): string {
|
||||
export default function hexAddPrefix (value?: string | null): string {
|
||||
if (value && hasPrefix(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import hexStripPrefix from './stripPrefix';
|
||||
|
||||
/**
|
||||
* @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 values are padded with `0`.
|
||||
|
||||
@@ -25,7 +25,7 @@ describe('hexHasPrefix', () => {
|
||||
|
||||
it('returns false when non-string value supplied', () => {
|
||||
expect(
|
||||
hexHasPrefix(false)
|
||||
hexHasPrefix(false as any)
|
||||
).toEqual(false);
|
||||
});
|
||||
});
|
||||
@@ -6,7 +6,6 @@ import isHex from '../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`
|
||||
@@ -19,6 +18,6 @@ import isHex from '../is/hex';
|
||||
* console.log('has prefix', hexHasPrefix('0x1234')); // => true
|
||||
* ```
|
||||
*/
|
||||
export default function hexHasPrefix (value: string | null | undefined): boolean {
|
||||
export default function hexHasPrefix (value?: string | null): boolean {
|
||||
return !!(value && isHex(value, -1, true) && value.substr(0, 2) === '0x');
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ const UNPREFIX_HEX_REGEX = /^[a-fA-F0-9]+$/;
|
||||
|
||||
/**
|
||||
* @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.
|
||||
@@ -21,7 +20,7 @@ const UNPREFIX_HEX_REGEX = /^[a-fA-F0-9]+$/;
|
||||
* console.log('stripped', hexStripPrefix('0x1234')); // => 1234
|
||||
* ```
|
||||
*/
|
||||
export default function hexStripPrefix (value: string | null | undefined): string {
|
||||
export default function hexStripPrefix (value?: string | null): string {
|
||||
if (!value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ function reverse (value: string): string {
|
||||
|
||||
/**
|
||||
* @name hexToBn
|
||||
* @signature hexToBn (value?: string, _options: ToBnOptions | boolean = { isLe: false, isNegative: false }): 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.
|
||||
|
||||
@@ -6,7 +6,6 @@ import hexToBn from './toBn';
|
||||
|
||||
/**
|
||||
* @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`.
|
||||
@@ -19,7 +18,7 @@ import hexToBn from './toBn';
|
||||
* hexToNumber('0x1234'); // => 0x1234
|
||||
* ```
|
||||
*/
|
||||
export default function hexToNumber (value?: string): number {
|
||||
export default function hexToNumber (value?: string | null): number {
|
||||
if (!value) {
|
||||
return NaN;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import hexStripPrefix from './stripPrefix';
|
||||
|
||||
/**
|
||||
* @name hexToU8a
|
||||
* @signature hexToU8a (value?: string, bitLength: number = -1): Uint8Array
|
||||
* @summary Creates a Buffer object from a hex string.
|
||||
* @description
|
||||
* `null` inputs returns an empty `Uint8Array` result. Hex input values return the actual bytes value converted to a Uint8Array. Anything that is not a hex string (including the `0x` prefix) throws an error.
|
||||
|
||||
@@ -6,7 +6,6 @@ import BN from 'bn.js';
|
||||
|
||||
/**
|
||||
* @name isBn
|
||||
* @signature isBN (value: any): boolean
|
||||
* @summary Tests for a `BN` object instance.
|
||||
* @description
|
||||
* Checks to see if the input object is an instance of `BN` (bn.js).
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
/**
|
||||
* @name isBoolean
|
||||
* @signature isBoolean (value: any): boolean
|
||||
* @summary Tests for a boolean value.
|
||||
* @description
|
||||
* Checks to see if the input value is a JavaScript boolean.
|
||||
|
||||
@@ -6,7 +6,6 @@ import isInstanceOf from './instanceOf';
|
||||
|
||||
/**
|
||||
* @name isBuffer
|
||||
* @signature isBuffer (value: any): boolean
|
||||
* @summary Tests for a `Buffer` object instance.
|
||||
* @description
|
||||
* Checks to see if the input object is an instance of `Buffer`.
|
||||
|
||||
@@ -6,7 +6,6 @@ import isInstanceOf from './instanceOf';
|
||||
|
||||
/**
|
||||
* @name isError
|
||||
* @signature isError (value: any): boolean
|
||||
* @summary Tests for a `Error` object instance.
|
||||
* @description
|
||||
* Checks to see if the input object is an instance of `Error`.
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
/**
|
||||
* @name isFunction
|
||||
* @signature isFunction (value: any): boolean
|
||||
* @summary Tests for a `function`.
|
||||
* @description
|
||||
* Checks to see if the input value is a JavaScript function.
|
||||
|
||||
@@ -19,7 +19,6 @@ describe('isHex', () => {
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
|
||||
it('returns true on hex values with String', () => {
|
||||
expect(
|
||||
isHex(new String(`0x${test}`))
|
||||
@@ -8,7 +8,6 @@ const HEX_REGEX = /^0x[a-fA-F0-9]+$/;
|
||||
|
||||
/**
|
||||
* @name isHex
|
||||
* @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. Optionally (`bitLength` !== -1) checks to see if the bitLength is correct.
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
/**
|
||||
* @name isInstanceOf
|
||||
* @signature isInstanceOf (value: any, clazz: Class<any>): boolean
|
||||
* @summary Tests for a instance of a class.
|
||||
* @description
|
||||
* Checks to see if the input value is an instance of the test class.
|
||||
|
||||
@@ -8,7 +8,6 @@ type IpTypes = 'v4' | 'v6';
|
||||
|
||||
/**
|
||||
* @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
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
/**
|
||||
* @name isNull
|
||||
* @signature isNull (value: any): boolean
|
||||
* @summary Tests for a `null` values.
|
||||
* @description
|
||||
* Checks to see if the input value is `null`.
|
||||
@@ -17,6 +16,6 @@
|
||||
* console.log('isNull', isNull(null)); // => true
|
||||
* ```
|
||||
*/
|
||||
export default function isNull (value: any): value is null {
|
||||
export default function isNull (value?: any): value is null {
|
||||
return value === null;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
/**
|
||||
* @name isNumber
|
||||
* @signature isNumber (value: any): boolean
|
||||
* @summary Tests for a JavaScript number.
|
||||
* @description
|
||||
* Checks to see if the input value is a valid number.
|
||||
|
||||
@@ -8,7 +8,6 @@ type ObjectIndexed = {
|
||||
|
||||
/**
|
||||
* @name isObject
|
||||
* @signature isObject (value: any): boolean
|
||||
* @summary Tests for an `object`.
|
||||
* @description
|
||||
* Checks to see if the input value is a JavaScript object.
|
||||
|
||||
@@ -11,7 +11,6 @@ type Observable = {
|
||||
|
||||
/**
|
||||
* @name isBObservable
|
||||
* @signature isObservable (value: any): boolean
|
||||
* @summary Tests for a `Observable` object instance.
|
||||
* @description
|
||||
* Checks to see if the input object is an instance of `BN` (bn.js).
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
/**
|
||||
* @name isString
|
||||
* @signature isString (value: any): boolean
|
||||
* @summary Tests for a string.
|
||||
* @description
|
||||
* Checks to see if the input value is a JavaScript string.
|
||||
|
||||
@@ -6,7 +6,6 @@ import isInstanceOf from './instanceOf';
|
||||
|
||||
/**
|
||||
* @name isU8a
|
||||
* @signature isU8a (value: any): boolean
|
||||
* @summary Tests for a `Uint8Array` object instance.
|
||||
* @description
|
||||
* Checks to see if the input object is an instance of `Uint8Array`.
|
||||
@@ -19,6 +18,6 @@ import isInstanceOf from './instanceOf';
|
||||
* console.log('isU8a', isU8a([])); // => false
|
||||
* ```
|
||||
*/
|
||||
export default function isU8a (value: any): value is Uint8Array {
|
||||
export default function isU8a (value?: any): value is Uint8Array {
|
||||
return isInstanceOf(value, Uint8Array);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
/**
|
||||
* @name isUndefined
|
||||
* @signature isUndefined (value: any): boolean
|
||||
* @summary Tests for a `undefined` values.
|
||||
* @description
|
||||
* Checks to see if the input value is `undefined`.
|
||||
@@ -17,6 +16,6 @@
|
||||
* console.log('isUndefined', isUndefined(void(0))); // => true
|
||||
* ```
|
||||
*/
|
||||
export default function isUndefined (value: any): value is undefined {
|
||||
export default function isUndefined (value?: any): value is undefined {
|
||||
return typeof value === 'undefined';
|
||||
}
|
||||
|
||||
@@ -6,12 +6,12 @@ import { logger } from './index';
|
||||
import { format } from './logger';
|
||||
|
||||
describe('logger', () => {
|
||||
let dateMatch;
|
||||
let prefixMatch;
|
||||
let l;
|
||||
let ln;
|
||||
let spy;
|
||||
let oldEnv;
|
||||
let dateMatch: any;
|
||||
let prefixMatch: any;
|
||||
let l: any;
|
||||
let ln: any;
|
||||
let spy: any;
|
||||
let oldEnv: any;
|
||||
|
||||
beforeEach(() => {
|
||||
oldEnv = process.env;
|
||||
@@ -28,8 +28,7 @@ describe('logger', () => {
|
||||
log: jest.fn(),
|
||||
warn: jest.fn()
|
||||
};
|
||||
global.console = spy;
|
||||
|
||||
(global as any).console = spy;
|
||||
|
||||
dateMatch = expect.stringMatching(/20[0-9]{2}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/);
|
||||
prefixMatch = expect.stringMatching(/TEST:/);
|
||||
@@ -143,7 +142,7 @@ describe('logger', () => {
|
||||
});
|
||||
|
||||
it('does not debug log when no process.env', () => {
|
||||
process.env = void 0;
|
||||
process.env = void 0 as any;
|
||||
|
||||
l = logger('test');
|
||||
l.debug('test');
|
||||
@@ -89,7 +89,6 @@ function noop (...values: Logger$Data): void {
|
||||
|
||||
/**
|
||||
* @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` (controlled 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.
|
||||
|
||||
@@ -8,7 +8,6 @@ import isUndefined from '../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`. With `bitLength` set, it converts the number to the equivalent size.
|
||||
@@ -22,7 +21,7 @@ import isUndefined from '../is/undefined';
|
||||
* numberToHex(0x1234, 32); // => 0x00001234
|
||||
* ```
|
||||
*/
|
||||
export default function numberToHex (value?: number, bitLength: number = -1): string {
|
||||
export default function numberToHex (value?: number | null, bitLength: number = -1): string {
|
||||
if (isUndefined(value) || isNull(value) || isNaN(value)) {
|
||||
return '0x';
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import numberToHex from './toHex';
|
||||
|
||||
/**
|
||||
* @name numberToU8a
|
||||
* @signature numberToU8a (value?: number, bitLenght: number = -1): Uint8Array
|
||||
* @summary Creates a Uint8Array object from a number.
|
||||
* @description
|
||||
* `null`/`undefined`/`NaN` inputs returns an empty `Uint8Array` result. `number` input values return the actual bytes value converted to a `Uint8Array`. With `bitLength`, it converts the value to the equivalent size.
|
||||
@@ -20,7 +19,7 @@ import numberToHex from './toHex';
|
||||
* numberToU8a(0x1234); // => [0x12, 0x34]
|
||||
* ```
|
||||
*/
|
||||
export default function numberToU8a (value?: number, bitLength: number = -1): Uint8Array {
|
||||
export default function numberToU8a (value?: number | null, bitLength: number = -1): Uint8Array {
|
||||
if (!value || isNaN(value)) {
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
|
||||
+7
-7
@@ -3,27 +3,27 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
describe('TextDecoder', () => {
|
||||
let origTD;
|
||||
let origTD: TextDecoder;
|
||||
|
||||
beforeEach(() => {
|
||||
origTD = global.TextDecoder;
|
||||
origTD = (global as any).TextDecoder;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
global.TextDecoder = origTD;
|
||||
(global as any).TextDecoder = origTD;
|
||||
});
|
||||
|
||||
it('polyfills with no exceptions (without TextDecoder)', () => {
|
||||
global.TextDecoder = null;
|
||||
(global as any).TextDecoder = null;
|
||||
|
||||
expect(require('./textDecoder')).toBeDefined();
|
||||
expect(global.TextDecoder).toBeDefined();
|
||||
expect((global as any).TextDecoder).toBeDefined();
|
||||
});
|
||||
|
||||
it('polyfills with no exceptions (with TextDecoder)', () => {
|
||||
global.TextDecoder = require('util').TextDecoder;
|
||||
(global as any).TextDecoder = require('util').TextDecoder;
|
||||
|
||||
expect(require('./textDecoder')).toBeDefined();
|
||||
expect(global.TextDecoder).toBeDefined();
|
||||
expect((global as any).TextDecoder).toBeDefined();
|
||||
});
|
||||
});
|
||||
+7
-7
@@ -3,27 +3,27 @@
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
describe('TextEncoder', () => {
|
||||
let origTE;
|
||||
let origTE: TextEncoder;
|
||||
|
||||
beforeEach(() => {
|
||||
origTE = global.TextEncoder;
|
||||
origTE = (global as any).TextEncoder;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
global.TextEncoder = origTE;
|
||||
(global as any).TextEncoder = origTE;
|
||||
});
|
||||
|
||||
it('polyfills with no exceptions (without TextEncoder)', () => {
|
||||
global.TextEncoder = null;
|
||||
(global as any).TextEncoder = null;
|
||||
|
||||
expect(require('./textEncoder')).toBeDefined();
|
||||
expect(global.TextEncoder).toBeDefined();
|
||||
expect((global as any).TextEncoder).toBeDefined();
|
||||
});
|
||||
|
||||
it('polyfills with no exceptions (with TextEncoder)', () => {
|
||||
global.TextEncoder = require('util').TextEncoder;
|
||||
(global as any).TextEncoder = require('util').TextEncoder;
|
||||
|
||||
expect(require('./textEncoder')).toBeDefined();
|
||||
expect(global.TextEncoder).toBeDefined();
|
||||
expect((global as any).TextEncoder).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -6,7 +6,7 @@ import { promisify } from './index';
|
||||
|
||||
describe('promisify', () => {
|
||||
it('handles functions with no parameters (resolve)', () => {
|
||||
const fn = (cb) => cb(null, [true, 'test', 1]);
|
||||
const fn = (cb: Function) => cb(null, [true, 'test', 1]);
|
||||
|
||||
return promisify(null, fn).then((result) => {
|
||||
expect(result).toEqual([true, 'test', 1]);
|
||||
@@ -14,7 +14,7 @@ describe('promisify', () => {
|
||||
});
|
||||
|
||||
it('handles functions with no parameters (reject)', () => {
|
||||
const fn = (cb) => cb(new Error('test reject'));
|
||||
const fn = (cb: Function) => cb(new Error('test reject'));
|
||||
|
||||
return promisify(null, fn).catch((error) => {
|
||||
expect(error.message).toEqual('test reject');
|
||||
@@ -22,7 +22,7 @@ describe('promisify', () => {
|
||||
});
|
||||
|
||||
it('handles functions with parameters (resolve)', () => {
|
||||
const fn = (a, b, c, cb) => cb(null, [a, b, c]);
|
||||
const fn = (a: any, b: any, c: any, cb: Function) => cb(null, [a, b, c]);
|
||||
|
||||
return promisify(null, fn, 2, false, null).then((result) => {
|
||||
expect(result).toEqual([2, false, null]);
|
||||
@@ -30,7 +30,7 @@ describe('promisify', () => {
|
||||
});
|
||||
|
||||
it('handles functions with parameters (reject)', () => {
|
||||
const fn = (a, b, c, cb) => cb(new Error(`test reject: ${a},${b},${c}`));
|
||||
const fn = (a: any, b: any, c: any, cb: Function) => 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');
|
||||
@@ -40,7 +40,8 @@ describe('promisify', () => {
|
||||
it('applies the correct this argument', () => {
|
||||
const self = { something: 'something' };
|
||||
|
||||
return promisify(self, function (cb) {
|
||||
return promisify(self, function (cb: Function) {
|
||||
// @ts-ignore
|
||||
expect(this).toEqual(self);
|
||||
cb();
|
||||
});
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
/**
|
||||
* @name promisify
|
||||
* @signature function promisify (this: ?Object, fn: Function, ...params: Array<any>): 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.
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
import { stringCamelCase } from './index';
|
||||
|
||||
describe('stringCamelCase', () => {
|
||||
it("works correctly", () => {
|
||||
it('works correctly', () => {
|
||||
expect(
|
||||
stringCamelCase('Snake_case')
|
||||
).toBe('snakeCase');
|
||||
+4
-4
@@ -17,15 +17,15 @@ describe('stringLowerFirst', () => {
|
||||
).toBe('abc');
|
||||
});
|
||||
|
||||
it('returns undefined as undefined', () => {
|
||||
it('returns undefined as empty', () => {
|
||||
expect(
|
||||
stringLowerFirst()
|
||||
).toBe(undefined);
|
||||
).toBe('');
|
||||
});
|
||||
|
||||
it('returns null as null', () => {
|
||||
it('returns null as empty', () => {
|
||||
expect(
|
||||
stringLowerFirst(null)
|
||||
).toBe(null);
|
||||
).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -2,12 +2,8 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import isNull from '../is/null';
|
||||
import isUndefined from '../is/undefined';
|
||||
|
||||
/**
|
||||
* @name stringLowerFirst
|
||||
* @signature stringLowerFirst (_value: string): string
|
||||
* @summary Lowercase the first letter of a string
|
||||
* @description
|
||||
* Lowercase the first letter of a string
|
||||
@@ -20,10 +16,10 @@ import isUndefined from '../is/undefined';
|
||||
* stringLowerFirst('ABC'); // => 'aBC'
|
||||
* ```
|
||||
*/
|
||||
export default function stringLowerFirst (_value: string): string {
|
||||
if (isNull(_value) || isUndefined(_value)) {
|
||||
return _value;
|
||||
export default function stringLowerFirst (value?: string | null): string {
|
||||
if (!value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return _value.charAt(0).toLowerCase() + _value.slice(1);
|
||||
return value.charAt(0).toLowerCase() + value.slice(1);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
/**
|
||||
* @name stringShorten
|
||||
* @signature stringShorten (value: any, 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
|
||||
@@ -17,7 +16,7 @@
|
||||
* stringShorten('1234567890', 2); // => 12..90
|
||||
* ```
|
||||
*/
|
||||
export default function stringShorten (_value: any, prefixLength: number = 6): string {
|
||||
export default function stringShorten (_value?: any, prefixLength: number = 6): string {
|
||||
const value = `${_value}`;
|
||||
|
||||
if (value.length <= 2 + 2 * prefixLength) {
|
||||
|
||||
@@ -24,7 +24,6 @@ try {
|
||||
|
||||
/**
|
||||
* @name stringToU8a
|
||||
* @signature stringToU8a (value?: string): UInt8Array
|
||||
* @summary Creates a Uint8Array object from a utf-8 string.
|
||||
* @description
|
||||
* String input values return the actual encoded `UInt8Array`. `null` or `undefined` values returns an empty encoded array.
|
||||
|
||||
+4
-4
@@ -17,15 +17,15 @@ describe('stringUpperFirst', () => {
|
||||
).toBe('Abc');
|
||||
});
|
||||
|
||||
it('returns undefined as undefined', () => {
|
||||
it('returns undefined as empty', () => {
|
||||
expect(
|
||||
stringUpperFirst()
|
||||
).toBe(undefined);
|
||||
).toBe('');
|
||||
});
|
||||
|
||||
it('returns null as null', () => {
|
||||
it('returns null as empty', () => {
|
||||
expect(
|
||||
stringUpperFirst(null)
|
||||
).toBe(null);
|
||||
).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -2,12 +2,8 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import isNull from '../is/null';
|
||||
import isUndefined from '../is/undefined';
|
||||
|
||||
/**
|
||||
* @name stringUpperFirst
|
||||
* @signature stringUpperFirst (_value: string): string
|
||||
* @summary Lowercase the first letter of a string
|
||||
* @description
|
||||
* Lowercase the first letter of a string
|
||||
@@ -20,10 +16,10 @@ import isUndefined from '../is/undefined';
|
||||
* stringUpperFirst('abc'); // => 'Abc'
|
||||
* ```
|
||||
*/
|
||||
export default function stringUpperFirst (_value: string): string {
|
||||
if (isNull(_value) || isUndefined(_value)) {
|
||||
return _value;
|
||||
export default function stringUpperFirst (value?: string | null): string {
|
||||
if (!value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return _value.charAt(0).toUpperCase() + _value.slice(1);
|
||||
return value.charAt(0).toUpperCase() + value.slice(1);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import u8aToU8a from './toU8a';
|
||||
|
||||
/**
|
||||
* @name u8aConcat
|
||||
* @signature u8aConcat (...values: Array<Uint8Array | string>): Uint8Array
|
||||
* @summary Creates a concatenated Uint8Array from the inputs.
|
||||
* @description
|
||||
* Concatenates the input arrays into a single `UInt8Array`.
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
/**
|
||||
* @name u8aFixLength
|
||||
* @signature u8aFixLength (value: string, bitLength: number = -1, withPadding: boolean = false): string
|
||||
* @summary Shifts a Uint8Array to a specific bitLength
|
||||
* @description
|
||||
* Returns a uint8Array 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.
|
||||
|
||||
@@ -10,7 +10,6 @@ import { ToBnOptions } from '../types';
|
||||
|
||||
/**
|
||||
* @name u8aToBn
|
||||
* @signature u8aToHex (value?: Uint8Array, options: ToBnOptions | boolean = { isLe: true, isNegative: false }): BN
|
||||
* @summary Creates a BN from a Uint8Array object.
|
||||
* @description
|
||||
* `UInt8Array` input values return the actual BN. `null` or `undefined` values returns an `0x0` value.
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
/**
|
||||
* @name u8aToBuffer
|
||||
* @signature u8aToBuffer (value?: UInt8Array): Buffer
|
||||
* @summary Creates a Buffer object from a hex string.
|
||||
* @description
|
||||
* `null` inputs returns an empty `Buffer` result. `UInt8Array` input values return the actual bytes value converted to a `Buffer`. Anything that is not a `UInt8Array` throws an error.
|
||||
@@ -17,7 +16,7 @@
|
||||
* console.log('Buffer', u8aToBuffer('0x123480001f'));
|
||||
* ```
|
||||
*/
|
||||
export default function u8aToBuffer (value?: Uint8Array): Buffer {
|
||||
export default function u8aToBuffer (value?: Uint8Array | null): Buffer {
|
||||
if (!value) {
|
||||
return Buffer.from([]);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ describe('u8aToHex', () => {
|
||||
|
||||
it('returns empty as "" (unprefixed)', () => {
|
||||
expect(
|
||||
u8aToHex('', -1, false)
|
||||
u8aToHex(null, -1, false)
|
||||
).toEqual('');
|
||||
});
|
||||
|
||||
@@ -45,7 +45,7 @@ describe('u8aToHex', () => {
|
||||
it('returns the hex value where allowed < max', () => {
|
||||
expect(
|
||||
u8aToHex(
|
||||
new Uint8Array([128, 0, 10, 11], 64)
|
||||
new Uint8Array(Uint8Array.from([128, 0, 10, 11]), 64)
|
||||
)
|
||||
).toEqual('0x80000a0b');
|
||||
});
|
||||
@@ -6,7 +6,6 @@ const ALPHABET = '0123456789abcdef';
|
||||
|
||||
/**
|
||||
* @name u8aToHex
|
||||
* @signature u8aToHex (value?: UInt8Array): string
|
||||
* @summary Creates a hex string from a Uint8Array object.
|
||||
* @description
|
||||
* `UInt8Array` input values return the actual hex string. `null` or `undefined` values returns an `0x` string.
|
||||
|
||||
@@ -20,7 +20,6 @@ try {
|
||||
|
||||
/**
|
||||
* @name u8aToString
|
||||
* @signature u8aToString (value?: UInt8Array): string
|
||||
* @summary Creates a utf-8 string from a Uint8Array object.
|
||||
* @description
|
||||
* `UInt8Array` input values return the actual decoded utf-8 string. `null` or `undefined` values returns an empty string.
|
||||
@@ -33,7 +32,7 @@ try {
|
||||
* u8aToString(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f])); // hello
|
||||
* ```
|
||||
*/
|
||||
export default function u8aToString (value?: Uint8Array): string {
|
||||
export default function u8aToString (value?: Uint8Array | null): string {
|
||||
if (!value || !value.length) {
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import stringToU8a from '../string/toU8a';
|
||||
|
||||
/**
|
||||
* @name u8aToU8a
|
||||
* @signature u8aToU8a (value?: Array<number> | Buffer | Uint8Array | string | null): Uint8Array
|
||||
* @summary Creates a Uint8Array value from a Uint8Array bignumber or hex input.
|
||||
* @description
|
||||
* `null` inputs returns a `[]` result, Uint8Array values returns the value, hex strings returns a Uint8Array representation.
|
||||
|
||||
Reference in New Issue
Block a user