Skip to content

Commit

Permalink
fix: sorting for listAccounts() if cached value is used
Browse files Browse the repository at this point in the history
  • Loading branch information
koresar committed Dec 8, 2023
1 parent befc781 commit 06f3ab9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
33 changes: 33 additions & 0 deletions spec/book.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import delay from "./helper/delay";
import * as moment from "moment";
import { DateTime } from "luxon";

require("./helper/MongoDB.spec"); // allows single test debugging

describe("book", function () {
describe("constructor", () => {
it("should throw an error when name of book is not a string", () => {
Expand Down Expand Up @@ -624,6 +626,37 @@ describe("book", function () {
expect(accounts1).to.deep.equal(["Assets", "Client Custody", "Income", "Income:Rent Taxable", "Liabilities"]);
});

it("should sort accounts alphabetically including cached values", async () => {
const book1 = new Book("MyBook-listAccounts sorting 1");
await book1.entry("MyBook-listAccounts sorting 1").debit("Income:Rent Taxable", 1).credit("Assets", 1).commit();
await book1.entry("MyBook-listAccounts sorting 1").credit("Liabilities", 1).debit("Client Custody", 1).commit();
await book1.listAccounts(); // creates cached doc
await book1.entry("MyBook-listAccounts sorting 1").credit("Z", 1).debit("ZZ", 1).commit();
await book1.entry("MyBook-listAccounts sorting 1").credit("A", 1).debit("AA", 1).commit();
const accounts1 = await book1.listAccounts();

const book2 = new Book("MyBook-listAccounts sorting 2");
await book2.entry("MyBook-listAccounts sorting 2").debit("Client Custody", 2).credit("Liabilities", 2).commit();
await book2.entry("MyBook-listAccounts sorting 2").credit("Assets", 3).debit("Income:Rent Taxable", 3).commit();
await book2.listAccounts(); // created cached doc
await book2.entry("MyBook-listAccounts sorting 2").credit("Z", 3).debit("ZZ", 3).commit();
await book2.entry("MyBook-listAccounts sorting 2").credit("A", 3).debit("AA", 3).commit();
const accounts2 = await book2.listAccounts();

expect(accounts1).to.deep.equal(accounts2);
expect(accounts1).to.deep.equal([
"A",
"AA",
"Assets",
"Client Custody",
"Income",
"Income:Rent Taxable",
"Liabilities",
"Z",
"ZZ",
]);
});

async function addBalance(book: Book, suffix = "") {
await book
.entry("Test Entry")
Expand Down
18 changes: 9 additions & 9 deletions src/Book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ const GROUP = {

function fromDistinctToAccounts(distinctResult: string[]) {
const accountsSet: Set<string> = new Set();
for (const result of distinctResult) {
const prev: string[] = [];
const paths: string[] = result.split(":");
for (const acct of paths) {
prev.push(acct);
accountsSet.add(prev.join(":"));
for (const fullAccountName of distinctResult) {
const paths = fullAccountName.split(":");
let path = paths[0];
accountsSet.add(path);
for (let i = 1; i < paths.length; ++i) {
path += ":" + paths[i];
accountsSet.add(path);
}
}

return Array.from(accountsSet);
return Array.from(accountsSet).sort();
}

export class Book<U extends ITransaction = ITransaction, J extends IJournal = IJournal> {
Expand Down Expand Up @@ -370,7 +370,7 @@ export class Book<U extends ITransaction = ITransaction, J extends IJournal = IJ
let uniqueAccounts = fromDistinctToAccounts(results);

if (listAccountsSnapshot) {
uniqueAccounts = Array.from(new Set([...uniqueAccounts, ...listAccountsSnapshot.meta.accounts]));
uniqueAccounts = Array.from(new Set([...uniqueAccounts, ...listAccountsSnapshot.meta.accounts])).sort();
}

if (uniqueAccounts.length === 0) return uniqueAccounts;
Expand Down

0 comments on commit 06f3ab9

Please sign in to comment.