UNPKG

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