Skip to content

Commit

Permalink
fix: deprecate prettyPrint in favor of stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed May 6, 2024
1 parent 168494c commit 1197407
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/transactions/src/cl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions packages/transactions/src/clarity/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions packages/transactions/src/clarity/prettyPrint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

0 comments on commit 1197407

Please sign in to comment.