-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: sort account keys when generating file
- Loading branch information
Showing
5 changed files
with
52 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@clarigen/cli': patch | ||
--- | ||
|
||
The accounts type now has a fixed alphabetical ordering. This will prevent git changes just from re-running `clarigen`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@clarigen/boot': patch | ||
--- | ||
|
||
Adds updated boot package exports |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,23 @@ | ||
import { Session } from '../session'; | ||
import { Session, SessionAccount } from '../session'; | ||
|
||
export function generateAccountsCode(session: Session) { | ||
const accounts = Object.fromEntries( | ||
session.accounts.map(account => { | ||
const { name, ...rest } = account; | ||
export function generateAccountsCode(accounts: SessionAccount[]) { | ||
const sortedAccounts = sortAccounts(accounts); | ||
return Object.fromEntries( | ||
sortedAccounts.map(a => { | ||
const { name, ...rest } = a; | ||
return [name, rest]; | ||
}) | ||
); | ||
} | ||
|
||
return `export const accounts = ${JSON.stringify(accounts)} as const;`; | ||
// Sort accounts alphabetically by their name. | ||
// Used to preserve ordering when generating files | ||
export function sortAccounts(accounts: SessionAccount[]): SessionAccount[] { | ||
const nameSorted = [...accounts].sort((a, b) => { | ||
if (a.name < b.name) { | ||
return -1; | ||
} | ||
return 1; | ||
}); | ||
return nameSorted; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { generateAccountsCode } from '../src/files/accounts'; | ||
import { SessionAccount } from '../src/session'; | ||
import { describe, expect, test } from 'vitest'; | ||
|
||
test('accounts are sorted before serialization', async () => { | ||
const accounts: SessionAccount[] = [ | ||
{ | ||
name: 'wallet_2', | ||
balance: '100', | ||
address: 'aaa', | ||
}, | ||
{ | ||
name: 'wallet_1', | ||
balance: '200', | ||
address: 'bbb', | ||
}, | ||
]; | ||
const serialized = generateAccountsCode(accounts); | ||
expect(Object.keys(serialized)[0]).toEqual('wallet_1'); | ||
expect(serialized.wallet_1).toEqual({ | ||
balance: '200', | ||
address: 'bbb', | ||
}); | ||
}); |