UNPKG

@provenanceio/wallet-utils

Version:

Typescript Utilities for Provenance Blockchain Wallet

154 lines (145 loc) 4.71 kB
import { trimString } from './trimString'; import { hashFormat } from './hashFormat'; import { numberFormat } from './numberFormat'; import { capitalize } from './capitalize'; import { format } from 'date-fns'; import { match, P } from 'ts-pattern'; export var displayCapitalizedString = function displayCapitalizedString(fieldValue) { return capitalize("".concat(fieldValue)); }; /** * Displays a wallet address by trimming length to 11. */ export var displayAddress = function displayAddress() { var fieldValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; return fieldValue ? trimString(fieldValue, 11, 3) : fieldValue; }; /** * amountList is an array of objects: amountList: [{ denom: 'a', amount: '1' }, {...}] * Note: If the denom is nhash, autoconvert to hash */ export var displayAmountList = function displayAmountList(fieldValue) { return fieldValue.map(function (coin) { return displayCoinAsObject(coin); }); }; /** * Hash is fixed to 5 dec points by default. */ export var displayHashFormat = function displayHashFormat(fieldValue) { var toFixed = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5; return "".concat(hashFormat(fieldValue).toFixed(toFixed), " Hash"); }; /** * Amount be a CoinAsObject or an amount. Automatically convert nhash to hash. * For other denoms, defaults to display a capital case of the denom. */ export var displayCoinAsObject = function displayCoinAsObject(fieldValue) { if (typeof fieldValue === 'string' || typeof fieldValue === 'number') return numberFormat(fieldValue, 5); var _ref = fieldValue, denom = _ref.denom, amount = _ref.amount; switch (denom) { case 'hash': return "".concat(amount, " ").concat(denom); case 'nhash': return "".concat(hashFormat(amount), " Hash"); default: return "".concat(amount, " ").concat(denom); } }; export var displayGasPrice = function displayGasPrice(fieldValue) { return displayCoinAsObject({ denom: fieldValue.gasPriceDenom, amount: fieldValue.gasPrice }); }; export var displayDateAndTime = function displayDateAndTime(fieldValue) { return format(new Date(fieldValue), 'MMM d, h:mm:ss a'); }; /** * Deduce the formatting function based on the field and/or value. Value * is formatted below, otherwise it may be an array/object which is * recursively unpacked message-service and passed through the formatting * function again. * */ export var formatSingleValue = function formatSingleValue(value) { return match({ value: value }) // True => Yes, False => No ["with"]({ value: P["boolean"] }, function () { return value ? 'Yes' : 'No'; }) // Attempt to convert number or string to a Date ["with"]({ value: P.when(function () { return new Date(value); }).toString() === 'Invalid Date' }, function () { return displayDateAndTime(value); }) // Blockchain address usually starts with tp. Trim all other strings without spaces. ["with"]({ value: P.when(function (str) { return str.toString().indexOf('tp') === 0 || str.toString().indexOf(' ') < 0 && str.length > 30; }) }, function () { return displayAddress(value); }) // Values that are all caps separated by underscores, i.e. MARKER_TYPE_COIN ["with"]({ value: P.when(function () { return new RegExp('^[A-Z]+(?:_[A-Z]+)*$').test(value); }) }, function () { return displayCapitalizedString(value); }) // Default print as string .otherwise(function () { return value.toString(); }); }; /** * Deduce the formatting function based on the field and/or value. Value * is formatted below, otherwise it may be an array/object which is * recursively unpacked message-service and passed through the formatting * function again. */ export var formatCustomObj = function formatCustomObj(key, value) { return match({ key: key, value: value })["with"]({ value: P.array({ denom: P.string, amount: P.string || P.number }) }, function () { return displayAmountList(value); })["with"]({ value: { denom: P.string, amount: P.string || P.number } }, function () { return displayCoinAsObject(value); })["with"]({ value: { gasPriceDenom: P.string, gasPrice: P.string || P.number } }, function () { return displayGasPrice(value); })["with"]({ key: 'feeAmount' }, function () { return displayHashFormat(value); })["with"]({ value: P.when(function (p) { return new Date(p); }).toString() === 'Invalid Date' }, function () { return displayDateAndTime(value); }).otherwise(function () { return null; }); };