UNPKG

585 BJavaScriptView Raw
1/**
2 * @name stringShorten
3 * @summary Returns a string with maximum length
4 * @description
5 * Checks the string against the `prefixLength`, if longer than double this, shortens it by placing `..` in the middle of it
6 * @example
7 * <BR>
8 *
9 * ```javascript
10 * import { stringShorten } from '@polkadot/util';
11 *
12 * stringShorten('1234567890', 2); // => 12..90
13 * ```
14 */
15export function stringShorten(value, prefixLength = 6) {
16 return value.length <= 2 + 2 * prefixLength
17 ? value.toString()
18 : `${value.substring(0, prefixLength)}${value.slice(-prefixLength)}`;
19}