From 067240185e7bc419639f574501e2c67d1188b9d3 Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 18 Oct 2021 12:18:58 +0300 Subject: [PATCH] Adjust valueToText formatting (no trailing ,) (#6330) --- packages/react-params/src/Param/Static.tsx | 8 ++++---- packages/react-params/src/valueToText.tsx | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/react-params/src/Param/Static.tsx b/packages/react-params/src/Param/Static.tsx index 53228ab839..926a6768fe 100644 --- a/packages/react-params/src/Param/Static.tsx +++ b/packages/react-params/src/Param/Static.tsx @@ -10,6 +10,7 @@ import styled from 'styled-components'; import { Static } from '@polkadot/react-components'; import { useTranslation } from '../translate'; +import { toHumanJson } from '../valueToText'; import Bare from './Bare'; interface Props { @@ -27,12 +28,11 @@ function StaticParam ({ asHex, children, childrenPre, className = '', defaultVal const value = defaultValue && (defaultValue.value as string) && ( asHex ? (defaultValue.value as Codec).toHex() - : JSON.stringify( + : toHumanJson( (defaultValue.value as { toHuman?: () => unknown }).toHuman ? (defaultValue.value as Codec).toHuman() - : defaultValue.value, - null, 2 - ).replace(/"/g, '').replace(/\\/g, '').replace(/\],\[/g, '],\n[') + : defaultValue.value + ) ); return ( diff --git a/packages/react-params/src/valueToText.tsx b/packages/react-params/src/valueToText.tsx index 3986c1026e..3bc0b7894b 100644 --- a/packages/react-params/src/valueToText.tsx +++ b/packages/react-params/src/valueToText.tsx @@ -7,7 +7,7 @@ import type { Codec } from '@polkadot/types/types'; import React from 'react'; import { Option, Raw } from '@polkadot/types'; -import { isFunction, isNull, isUndefined, u8aToHex } from '@polkadot/util'; +import { isFunction, isNull, isUndefined, stringify, u8aToHex } from '@polkadot/util'; interface DivProps { className?: string; @@ -41,9 +41,9 @@ function toHuman (value: Codec | Codec[]): unknown { : value.toString(); } -function toString (value: any): string { - return JSON - .stringify(value, null, 2) +export function toHumanJson (value: any): string { + return stringify(value, 2) + .replace(/,\n/g, '\n') .replace(/"/g, '') .replace(/\\/g, '') .replace(/\],\[/g, '],\n['); @@ -61,13 +61,13 @@ export default function valueToText (type: string, value: Codec | undefined | nu // HACK Handle Keys as hex-only (this should go away once the node value is // consistently swapped to `Bytes`) : type === 'Vec<(ValidatorId,Keys)>' - ? toString(formatKeys(value as unknown as [ValidatorId, Keys][])) + ? toHumanJson(formatKeys(value as unknown as [ValidatorId, Keys][])) : value instanceof Raw ? value.isEmpty ? '' : value.toString() : (value instanceof Option) && value.isNone ? '' - : toString(toHuman(value)) + : toHumanJson(toHuman(value)) ); }