UNPKG

@provenanceio/wallet-utils

Version:

Typescript Utilities for Provenance Blockchain Wallet

41 lines (31 loc) 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.trimString = void 0; // Limit the maxLength of a string // ellipsisIndex arg will add ellipsis at that index (defaults to the end) // Examples: // trimString('fantastic', 3, -1) => 'fan...'; // trimString('fantastic', 3, 2) => 'fa...c' // trimString('fantastic', 6, 1) => 'f...astic' // trimString('fantastic', 6, 3) => 'fan...tic' // trimString('fantastic', 3, 0) => '...tic' var trimString = function trimString() { var text = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; var maxLength = arguments.length > 1 ? arguments[1] : undefined; var ellipsisIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : -1; var ellipsisValue = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '...'; if (!text) return text; var textLength = text.length; // If maxLength is 0, just return the ellipsis if (!maxLength) return ellipsisValue; // Text must be a string, if it isn't return whatever was passed in if (typeof text !== 'string') return text; // EllipsisIndex requested past the length of the string if (ellipsisIndex > maxLength) return text; // If the text is shorter than the max, just return it if (textLength <= maxLength) return text; // Ellipsis at end (ellipsisIndex -1 | Prov...) if (ellipsisIndex === -1) return text.slice(0, maxLength - textLength).concat(ellipsisValue); // Ellipsis at start (ellipsisIndex 0 | ...ance) if (ellipsisIndex === 0) return ''.concat(ellipsisValue, text.slice(textLength - maxLength)); // Ellipsis middle (ellipsisIndex >0 | Pr...ce) var front = text.slice(0, ellipsisIndex); var back = text.split('').reverse().join('').slice(0, maxLength - ellipsisIndex).split('').reverse().join(''); return ''.concat(front, ellipsisValue, back); }; exports.trimString = trimString;