Skip to content

Commit

Permalink
fix it so that when values are printed the order of numbers is numeric (
Browse files Browse the repository at this point in the history
#338)

### Description

If a list of values was printed that were keyed by numbers such as
transactions to be executed in a governance proposal, they would be
ordered incorrectly due to numbers being treated like strings. EG `1,
11, 12, 2,3, 4` this fixes that.


<!-- start pr-codex -->

---

## PR-Codex overview
This PR fixes the printing of values in numerical order in the
`printValueMapRecursive` function in the `cli.ts` file.

### Detailed summary
- Fixed sorting of values in `printValueMapRecursive` function to order
numeric keys correctly
- Added tests for printing number keys in numeric order
  • Loading branch information
aaronmgdr authored Sep 10, 2024
1 parent 2c00a44 commit 717809e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/shaggy-cats-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@celo/celocli': patch
---

Fix printing of values that should be ordered numerically
71 changes: 71 additions & 0 deletions packages/cli/src/utils/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { testWithAnvilL2 } from '@celo/dev-utils/lib/anvil-test'
import { stripAnsiCodesFromNestedArray } from '../test-utils/cliUtils'
import { printValueMapRecursive } from './cli'
testWithAnvilL2('printValueMapRecursive', async () => {
it('should print the key-value pairs in the value map recursively', () => {
const valueMap = {
key1: 'value1',
key2: {
nestedKey1: 'nestedValue1',
nestedKey2: 'nestedValue2',
},
}

const mock = jest.fn()
console.log = mock

printValueMapRecursive(valueMap)

expect(stripAnsiCodesFromNestedArray(mock.mock.calls)).toMatchInlineSnapshot(`
[
[
"key1: value1
key2:
nestedKey1: nestedValue1
nestedKey2: nestedValue2",
],
]
`)
})

it('should print number keys in numeric order', () => {
const valueMap = {
1: 'jeden',
2: {
1: '1',
2: '2',
11: '3',
12: '4',
21: '5',
},
3: {
'0x1': 'nestedValue1',
jupiter: 'nestedValue2',
potato: 'nestedValue3',
},
}

const mock = jest.fn()
console.log = mock

printValueMapRecursive(valueMap)

expect(stripAnsiCodesFromNestedArray(mock.mock.calls)).toMatchInlineSnapshot(`
[
[
"1: jeden
2:
1: 1
2: 2
11: 3
12: 4
21: 5
3:
0x1: nestedValue1
jupiter: nestedValue2
potato: nestedValue3",
],
]
`)
})
})
7 changes: 6 additions & 1 deletion packages/cli/src/utils/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ function toStringValueMapRecursive(valueMap: Record<string, any>, prefix: string
return chalk`${v}`
}
return Object.keys(valueMap)
.sort()
.sort((a, b) => {
if (isNaN(Number(a)) && isNaN(Number(b))) {
return a.localeCompare(b)
}
return Number(a) - Number(b)
})
.map((key) => prefix + chalk.yellowBright.bold(`${key}: `) + printValue(valueMap[key]))
.join('\n')
}
Expand Down

0 comments on commit 717809e

Please sign in to comment.