From 11974072c23623c57db8954b0d96c1606189d7de Mon Sep 17 00:00:00 2001 From: janniks Date: Mon, 6 May 2024 14:51:10 +0800 Subject: [PATCH] fix: deprecate prettyPrint in favor of stringify --- packages/transactions/src/cl.ts | 3 ++- packages/transactions/src/clarity/parser.ts | 11 +++++++++++ packages/transactions/src/clarity/prettyPrint.ts | 9 ++++++--- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/packages/transactions/src/cl.ts b/packages/transactions/src/cl.ts index fe54bfa87..04f6cd57c 100644 --- a/packages/transactions/src/cl.ts +++ b/packages/transactions/src/cl.ts @@ -18,7 +18,8 @@ import { uintCV, } from './clarity'; -export { prettyPrint } from './clarity/prettyPrint'; +export { prettyPrint, stringify } from './clarity/prettyPrint'; +export { parse } from './clarity/parser'; // todo: https://github.com/hirosystems/clarinet/issues/786 diff --git a/packages/transactions/src/clarity/parser.ts b/packages/transactions/src/clarity/parser.ts index 47658f63a..5272f7c81 100644 --- a/packages/transactions/src/clarity/parser.ts +++ b/packages/transactions/src/clarity/parser.ts @@ -291,6 +291,17 @@ function clValue(map: (combinator: Combinator) => Combinator = v => v) { ); } +/** + * Parse a piece of string text as Clarity value syntax. + * Supports all Clarity value types (primitives, sequences, composite types). + * + * @example + * ``` + * const repr = Cl.parse("u4"); + * const repr = Cl.parse(`"hello"`); + * const repr = Cl.parse('(tuple (a 1) (b 2))'); + * ``` + */ export function parse(clarityValueString: string): ClarityValue { const result = clValue(entire)(clarityValueString); if (!result.success || !result.capture) throw 'Parse error'; // todo: we can add better error messages and add position tracking diff --git a/packages/transactions/src/clarity/prettyPrint.ts b/packages/transactions/src/clarity/prettyPrint.ts index 22ce4e672..adcace11e 100644 --- a/packages/transactions/src/clarity/prettyPrint.ts +++ b/packages/transactions/src/clarity/prettyPrint.ts @@ -110,8 +110,8 @@ function prettyPrintWithDepth(cv: ClarityValue, space = 0, depth: number): strin } /** - * @description format clarity values in clarity style strings - * with the ability to prettify the result with line break end space indentation + * Format clarity values in clarity style strings with the ability to prettify + * the result with line break end space indentation. * @param cv The Clarity Value to format * @param space The indentation size of the output string. There's no indentation and no line breaks if space = 0 * @example @@ -126,6 +126,9 @@ function prettyPrintWithDepth(cv: ClarityValue, space = 0, depth: number): strin * // } * ``` */ -export function prettyPrint(cv: ClarityValue, space = 0): string { +export function stringify(cv: ClarityValue, space = 0): string { return prettyPrintWithDepth(cv, space, 0); } + +/** @deprecated alias for {@link Cl.stringify} */ +export const prettyPrint = stringify;