Skip to content

Commit

Permalink
feat: sort tuple properties in cl pretty print
Browse files Browse the repository at this point in the history
  • Loading branch information
hugocaillard committed Feb 19, 2024
1 parent 922e037 commit 920efdf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
19 changes: 12 additions & 7 deletions packages/transactions/src/clarity/prettyPrint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ function formatList(cv: ListCV, space: number, depth = 1): string {

/**
* @description format Tuple clarity values in clarity style strings
* the keys are alphabetically sorted
* with the ability to prettify the result with line break end space indentation
* @example
* ```ts
* formatTuple(Cl.tuple({ id: Cl.uint(1) }))
* // { id: u1 }
* formatTuple(Cl.tuple({ id: Cl.uint(1), age: Cl.uint(20) }))
* // { age: 20, id: u1 }
*
* formatTuple(Cl.tuple({ id: Cl.uint(1) }, 2))
* formatTuple(Cl.tuple({ id: Cl.uint(1), age: Cl.uint(20) }, 2))
* // {
* // age: 20,
* // id: u1
* // }
* ```
Expand All @@ -64,7 +66,9 @@ function formatTuple(cv: TupleCV, space: number, depth = 1): string {
const spaceBefore = formatSpace(space, depth, false);
const endSpace = formatSpace(space, depth, true);

return `{${spaceBefore}${items.join(`,${spaceBefore}`)}${endSpace}}`;
return `{${spaceBefore}${items
.sort((a, b) => a[0].localeCompare(b[0]))
.join(`,${spaceBefore}`)}${endSpace}}`;
}

function exhaustiveCheck(param: never): never {
Expand Down Expand Up @@ -114,11 +118,12 @@ function prettyPrintWithDepth(cv: ClarityValue, space = 0, depth: number): strin
* @param space The indentation size of the output string. There's no indentation and no line breaks if space = 0
* @example
* ```ts
* prettyPrint(Cl.tuple({ id: Cl.some(Cl.uint(1)) }))
* // { id: (some u1) }
* prettyPrint(Cl.tuple({ id: Cl.uint(1), age: Cl.some(Cl.uint(42)) }))
* // { age: (some u42), id: u1 }
*
* prettyPrint(Cl.tuple({ id: Cl.uint(1) }, 2))
* prettyPrint(Cl.tuple({ id: Cl.uint(1), age: Cl.some(Cl.uint(42)) }, 2))
* // {
* // age: (some u42),
* // id: u1
* // }
* ```
Expand Down
18 changes: 9 additions & 9 deletions packages/transactions/tests/prettyPrint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,25 +109,25 @@ describe.only('test format of Stacks.js clarity values into clarity style string

const expected = `{
id: u1,
messageAscii: "hello world",
someMessageUtf8: (some u"hello world"),
items: (some (list
(ok {
id: u1,
owner: (some 'ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG),
valid: (ok u2),
history: (some (list
u1
u2
))
)),
id: u1,
owner: (some 'ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG),
valid: (ok u2)
})
(ok {
history: none,
id: u2,
owner: none,
valid: (err u1000),
history: none
valid: (err u1000)
})
))
)),
messageAscii: "hello world",
someMessageUtf8: (some u"hello world")
}`;

const result = Cl.prettyPrint(value, 2);
Expand Down

0 comments on commit 920efdf

Please sign in to comment.