UNPKG

@yoroi/common

Version:

The Common package of Yoroi SDK

44 lines (42 loc) 1.51 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.asConcatenedString = asConcatenedString; exports.asciiToHex = asciiToHex; exports.hexToAscii = hexToAscii; exports.truncateString = truncateString; var _parsers = require("./parsers"); /** * Returns a string representation of the given value, if possible. * @param {string | string[] | undefined | null} value - The value to convert to a string. * @returns {string} A string representation of the value, or undefined if not possible. */ function asConcatenedString(value) { if (value === null || value === undefined) return; if ((0, _parsers.isString)(value)) return value; if ((0, _parsers.isArrayOfType)(value, _parsers.isString)) return value.join(''); return; // TS } function truncateString(_ref) { let { value, maxLength, separator = '...' } = _ref; if (value.length <= maxLength) return value; const partLength = Math.floor((maxLength - separator.length) / 2); const start = value.substring(0, partLength); const end = value.substring(value.length - partLength); return `${start}${separator}${end}`; } function hexToAscii(hex) { if (hex === '' || !/^[0-9a-fA-F]*$/.test(hex) || hex.length % 2 !== 0) { return ''; } return hex.match(/.{1,2}/g).map(byte => String.fromCharCode(parseInt(byte, 16))).join(''); } function asciiToHex(str) { return Array.from(str).map(char => char.charCodeAt(0).toString(16).padStart(2, '0')).join(''); } //# sourceMappingURL=strings.js.map