Skip to content

Commit

Permalink
Improve formatting of return value test value for JsonRpcCondition si…
Browse files Browse the repository at this point in the history
…nce it can e either a string or a number.
  • Loading branch information
derekpierre committed Feb 27, 2025
1 parent 27fd762 commit f414c4e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/components/blocks/JsonPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React, { useState } from 'react';
import { TacoCondition } from '../../types/taco';
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
import { atomDark } from 'react-syntax-highlighter/dist/esm/styles/prism';
import { isNumericString } from './blockUtils';

interface JsonPreviewProps {
condition: TacoCondition | null;
Expand Down Expand Up @@ -103,7 +104,7 @@ const JsonPreview: React.FC<JsonPreviewProps> = ({ condition }) => {
lines.push(` method: '${method}',`);

const params = getProperty(condition, 'params');
if (params) {
if (params && params.length > 0) {
const paramsStr = JSON.stringify(params)
.replace(/"([^"]+)":/g, '$1:')
.replace(/"/g, '\'');
Expand All @@ -118,7 +119,8 @@ const JsonPreview: React.FC<JsonPreviewProps> = ({ condition }) => {

const returnValueTest = getProperty(condition, 'returnValueTest');
if (returnValueTest && 'value' in returnValueTest) {
lines.push(` returnValueTest: { comparator: "${returnValueTest.comparator || '>='}", value: ${returnValueTest.value} },`);
const formattedValue = isNumericString(returnValueTest.value) ? returnValueTest.value : `'${returnValueTest.value}'`;
lines.push(` returnValueTest: { comparator: "${returnValueTest.comparator || '>='}", value: ${formattedValue} },`);
}

lines.push('});');
Expand Down
7 changes: 6 additions & 1 deletion src/components/blocks/blockUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ const parseChainId = (value: string): ChainId => {
return 11155111;
};

// Helper function for checking whether a string is actually numeric
export function isNumericString(value: string): boolean {
return !isNaN(Number(value));
}

export const blocksToJson = (blocks: Block[]): TacoCondition | null => {
if (!blocks.length) return null;

Expand Down Expand Up @@ -207,7 +212,7 @@ const blockToJson = (block: Block): TacoCondition | null => {

jsonRpcCondition.returnValueTest = {
comparator,
value: (typeof expectedValueInput.value === "number") ? parseInt(expectedValueInput.value) : expectedValueInput.value
value: isNumericString(expectedValueInput.value) ? parseInt(expectedValueInput.value) : expectedValueInput.value
};
} else if (block.properties?.returnValueTest) {
jsonRpcCondition.returnValueTest = block.properties.returnValueTest as ReturnValueTest;
Expand Down

0 comments on commit f414c4e

Please sign in to comment.