From 56aff80e5251f9d55c3e9f92b369969570fc07cc Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Thu, 14 Dec 2017 20:14:17 +0100 Subject: [PATCH] Expand promisify (with this), add stringShorten (#50) --- docs/README.md | 7 ++++--- docs/string.md | 22 ++++++++++++++++++++++ package.json | 2 +- src/index.js | 3 ++- src/promisify.js | 10 +++++----- src/promisify.spec.js | 17 +++++++++++++---- src/string/index.js | 11 +++++++++++ src/string/shorten.js | 23 +++++++++++++++++++++++ src/string/shorten.spec.js | 29 +++++++++++++++++++++++++++++ 9 files changed, 110 insertions(+), 14 deletions(-) create mode 100644 docs/string.md create mode 100644 src/string/index.js create mode 100644 src/string/shorten.js create mode 100644 src/string/shorten.spec.js diff --git a/docs/README.md b/docs/README.md index 41abb9698..4deb74f0d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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'); // TEST: blah Wraps an async callback into a `Promise` ```js -function promisify (fn: (any) => void, ...params: Array): Promise +function promisify (this: any, fn: Function, ...params: Array): Promise ``` @@ -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!` ``` \ No newline at end of file diff --git a/docs/string.md b/docs/string.md new file mode 100644 index 000000000..22b655715 --- /dev/null +++ b/docs/string.md @@ -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 +``` \ No newline at end of file diff --git a/package.json b/package.json index 0406c6d26..d79145f01 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/src/index.js b/src/index.js index 44fcfceb1..8ec380d88 100644 --- a/src/index.js +++ b/src/index.js @@ -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 ); diff --git a/src/promisify.js b/src/promisify.js index afcb6c93c..c68b60f60 100644 --- a/src/promisify.js +++ b/src/promisify.js @@ -6,21 +6,21 @@ const isFunction = require('./is/function'); /** @name promisify - @signature function promisify (fn: (any) => void, ...params: Array): Promise + @signature function promisify (this: any, fn: Function, ...params: Array): Promise @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): Promise { +module.exports = function promisify (self: any, fn: Function, ...params: Array): Promise { 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); diff --git a/src/promisify.spec.js b/src/promisify.spec.js index da10f35d3..9f2fb6a8b 100644 --- a/src/promisify.spec.js +++ b/src/promisify.spec.js @@ -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(); + }); + }); }); diff --git a/src/string/index.js b/src/string/index.js new file mode 100644 index 000000000..83925e84e --- /dev/null +++ b/src/string/index.js @@ -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 +}; diff --git a/src/string/shorten.js b/src/string/shorten.js new file mode 100644 index 000000000..466be4509 --- /dev/null +++ b/src/string/shorten.js @@ -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)}`; +}; diff --git a/src/string/shorten.spec.js b/src/string/shorten.spec.js new file mode 100644 index 000000000..54c00bbbd --- /dev/null +++ b/src/string/shorten.spec.js @@ -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'); + }); +});