UNPKG

@provenanceio/wallet-utils

Version:

Typescript Utilities for Provenance Blockchain Wallet

78 lines (68 loc) 3.16 kB
import _typeof from "@babel/runtime/helpers/esm/typeof"; import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; import { isMatching, P } from 'ts-pattern'; import { formatCustomObj, formatSingleValue } from '../../../utils'; var recurseFormatDisplayValue = function recurseFormatDisplayValue(finalFlattenedDisplayObject, currDisplayObject, parentKey) { Object.entries(currDisplayObject).forEach(function (_ref) { var _ref2 = _slicedToArray(_ref, 2), key = _ref2[0], value = _ref2[1]; var isStringOrNumberOrBool = ['string', 'number', 'boolean'].includes(_typeof(value)); var isArrayOfObjects = isMatching(P.array({}), value); var isArrayOfStringsOrNumbersOrBools = isMatching(P.array(P.string), value) || isMatching(P.array(P.string), value) || isMatching(P.array(P["boolean"]), value); var currentFormattedValue; try { if (isStringOrNumberOrBool) { currentFormattedValue = formatSingleValue(value); } else { currentFormattedValue = formatCustomObj(key, value); } } catch (e) { console.error(e); } if (currentFormattedValue !== null) { parentKey ? finalFlattenedDisplayObject[parentKey][key] = currentFormattedValue : finalFlattenedDisplayObject[key] = currentFormattedValue; return; } // Arrays are displayed as space delimited single values or recursed again. if (isArrayOfObjects || isArrayOfStringsOrNumbersOrBools) { // Array is all string/numbers (combine and display) if (isArrayOfStringsOrNumbersOrBools) { var currentFieldCombinedValue = value.map(function (v) { return formatSingleValue(v); }).join("\n"); parentKey ? finalFlattenedDisplayObject[parentKey][key] = currentFieldCombinedValue : finalFlattenedDisplayObject[key] = currentFieldCombinedValue; return; } // Array needs additional looping (object/array children) else { value.forEach(function (cfArrayVal, index) { var newCfName = value.length > 1 ? "".concat(key, " ").concat(index + 1) : key; finalFlattenedDisplayObject[newCfName] = {}; recurseFormatDisplayValue(finalFlattenedDisplayObject, cfArrayVal, newCfName); return; }); } } // Objects are also recursed again and passed a parent key. else { finalFlattenedDisplayObject[key] = {}; recurseFormatDisplayValue(finalFlattenedDisplayObject, value, key); return; } }); }; /** * Formats a display object from {@link unpackDisplayObjectFromWalletMessage} by * recursing through the nested json object and formatting values based on * formatting functions {@link formatSingleValue} and {@link formatCustomObj} * that match keys and/or values to specific tests. * @param displayObject object being rendered */ export var formatDisplayObject = function formatDisplayObject(_ref3) { var displayObject = _ref3.displayObject; var finalMessage = {}; if (displayObject) { Object.values(displayObject).reduce(function () { return recurseFormatDisplayValue(finalMessage, displayObject); }, {}); } return finalMessage; };