Expand promisify (with this), add stringShorten (#50)
This commit is contained in:
+4
-3
@@ -11,6 +11,7 @@ Utility methods for this package are split into groups
|
||||
- [jsonrpc](jsonrpc.md) Convenience functions for values from @polkadot/api-jsonrpc
|
||||
- [keccak](keccak.md) Create Keccak256 values as hex, string & buffer output
|
||||
- [number](number.md) Utility methods to convert to and from `number` values
|
||||
- [string](string.md) Utility methods to convert to work with `string` values
|
||||
- [u8a](u8a.md) Utility methods to convert to and from `Uint8Array` objects
|
||||
|
||||
# Available methods
|
||||
@@ -56,7 +57,7 @@ l.log('blah'); // <date> TEST: blah
|
||||
Wraps an async callback into a `Promise`
|
||||
|
||||
```js
|
||||
function promisify (fn: (any) => void, ...params: Array<any>): Promise<any>
|
||||
function promisify (this: any, fn: Function, ...params: Array<any>): Promise<any>
|
||||
```
|
||||
|
||||
|
||||
@@ -65,6 +66,6 @@ Wraps the supplied async function `fn` that has a standard JS callback `(error:
|
||||
```js
|
||||
const promisify = require('@polkadot/util/promisify');
|
||||
|
||||
await promisify((a, cb) => cb(null, a), true); // resolves with `true`
|
||||
await promisify((cb) => cb(new Error('error!'))); // rejects with `error!`
|
||||
await promisify(null, ((a, cb) => cb(null, a), true); // resolves with `true`
|
||||
await promisify(null, (cb) => cb(new Error('error!'))); // rejects with `error!`
|
||||
```
|
||||
@@ -0,0 +1,22 @@
|
||||
# string
|
||||
|
||||
Utility methods to convert to work with `string` values
|
||||
|
||||
- [stringShorten](#stringshorten) Returns a string with maximum length
|
||||
|
||||
## stringShorten
|
||||
|
||||
Returns a string with maximum length
|
||||
|
||||
```js
|
||||
stringShorten (value: any, prefixLength: number = 8): string
|
||||
```
|
||||
|
||||
|
||||
Checks the string against the `prefixLength`, if longer than dopuble this, shortens it by placing `..` in the middle of it
|
||||
|
||||
```js
|
||||
import { stringShorten } from '@polkadot/util';
|
||||
|
||||
stringShorten('1234567890', 2); // => 12..90
|
||||
```
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@polkadot/util",
|
||||
"version": "0.8.3",
|
||||
"version": "0.9.0",
|
||||
"description": "A collection of useful utilities for @polkadot",
|
||||
"main": "index.js",
|
||||
"keywords": [
|
||||
|
||||
+2
-1
@@ -13,11 +13,12 @@ const keccak = require('./keccak');
|
||||
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, keccak, { logger }, number, { promisify }, u8a
|
||||
{}, array, { assert }, bn, buffer, ext, hex, is, jsonrpc, keccak, { logger }, number, { promisify }, string, u8a
|
||||
);
|
||||
|
||||
+5
-5
@@ -6,21 +6,21 @@ const isFunction = require('./is/function');
|
||||
|
||||
/**
|
||||
@name promisify
|
||||
@signature function promisify (fn: (any) => void, ...params: Array<any>): Promise<any>
|
||||
@signature function promisify (this: any, 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.
|
||||
@example
|
||||
const promisify = require('@polkadot/util/promisify');
|
||||
|
||||
await promisify((a, cb) => cb(null, a), true); // resolves with `true`
|
||||
await promisify((cb) => cb(new Error('error!'))); // rejects with `error!`
|
||||
await promisify(null, ((a, cb) => cb(null, a), true); // resolves with `true`
|
||||
await promisify(null, (cb) => cb(new Error('error!'))); // rejects with `error!`
|
||||
*/
|
||||
module.exports = function promisify (fn: (any) => void, ...params: Array<any>): Promise<any> {
|
||||
module.exports = function promisify (self: any, fn: Function, ...params: Array<any>): Promise<any> {
|
||||
assert(isFunction(fn), 'Expected function input to promisify');
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
fn.apply(null, [].concat(params, [
|
||||
fn.apply(self, [].concat(params, [
|
||||
(error: Error, result: any): void => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
|
||||
+13
-4
@@ -12,7 +12,7 @@ describe('promisify', () => {
|
||||
it('handles functions with no parameters (resolve)', () => {
|
||||
const fn = (cb) => cb(null, [true, 'test', 1]);
|
||||
|
||||
return promisify(fn).then((result) => {
|
||||
return promisify(null, fn).then((result) => {
|
||||
expect(result).toEqual([true, 'test', 1]);
|
||||
});
|
||||
});
|
||||
@@ -20,7 +20,7 @@ describe('promisify', () => {
|
||||
it('handles functions with no parameters (reject)', () => {
|
||||
const fn = (cb) => cb(new Error('test reject'));
|
||||
|
||||
return promisify(fn).catch((error) => {
|
||||
return promisify(null, fn).catch((error) => {
|
||||
expect(error.message).toEqual('test reject');
|
||||
});
|
||||
});
|
||||
@@ -28,7 +28,7 @@ describe('promisify', () => {
|
||||
it('handles functions with parameters (resolve)', () => {
|
||||
const fn = (a, b, c, cb) => cb(null, [a, b, c]);
|
||||
|
||||
return promisify(fn, 2, false, null).then((result) => {
|
||||
return promisify(null, fn, 2, false, null).then((result) => {
|
||||
expect(result).toEqual([2, false, null]);
|
||||
});
|
||||
});
|
||||
@@ -36,8 +36,17 @@ describe('promisify', () => {
|
||||
it('handles functions with parameters (reject)', () => {
|
||||
const fn = (a, b, c, cb) => cb(new Error(`test reject: ${a},${b},${c}`));
|
||||
|
||||
return promisify(fn, 3, 'string', true).catch((error) => {
|
||||
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,11 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
const stringShorten = require('./shorten');
|
||||
|
||||
/**
|
||||
@summary Utility methods to convert to work with `string` values
|
||||
*/
|
||||
module.exports = {
|
||||
stringShorten
|
||||
};
|
||||
@@ -0,0 +1,23 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
// @flow
|
||||
|
||||
/**
|
||||
@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
|
||||
@example
|
||||
import { stringShorten } from '@polkadot/util';
|
||||
|
||||
stringShorten('1234567890', 2); // => 12..90
|
||||
*/
|
||||
module.exports = function stringShorten (_value: any, prefixLength: number = 6): string {
|
||||
const value = `${_value}`;
|
||||
|
||||
if (value.length <= 2 + 2 * prefixLength) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return `${value.substr(0, prefixLength)}..${value.slice(-prefixLength)}`;
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
// ISC, Copyright 2017 Jaco Greeff
|
||||
|
||||
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');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user