From 8fbb4bb2ad4f508f55c145c8a4aa17660a0e1853 Mon Sep 17 00:00:00 2001 From: Patrick McClurg Date: Wed, 8 Nov 2023 11:07:27 +0100 Subject: [PATCH] added total count to the verbose output --- README.md | 5 ++++- index.js | 9 +++++---- package.json | 2 +- src/main.ts | 10 ++++++---- test/main.test.ts | 4 ++++ 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e484e56..defaf43 100644 --- a/README.md +++ b/README.md @@ -40,4 +40,7 @@ This can be useful for verifying past draws or repeatability. * `-v, --verbose` -A verbose output format for when you want to know a little more about the draw, or want to repeat it \ No newline at end of file +A verbose output format for when you want to know a little more about the draw, or want to repeat it. + +The output format looks something like: +`{"round":1,"randomness":"f73220684f2a171ffb00ec3b21983e01738bc863e26f374b0a76081416eebda8","totalCount":2,winners":["heads"]}` \ No newline at end of file diff --git a/index.js b/index.js index 2cad505..dfc14ec 100755 --- a/index.js +++ b/index.js @@ -7203,19 +7203,20 @@ async function main(params) { } async function draw(params) { const { values, count, drandURL } = params; + const totalCount = values.length; if (count === 0) { - return { winners: [] }; + return { totalCount, winners: [] }; } if (values.length <= count) { - return { winners: values }; + return { totalCount, winners: values }; } if (params.randomness) { const winners2 = select(count, values, Buffer.from(params.randomness, "hex")); - return { randomness: params.randomness, winners: winners2 }; + return { randomness: params.randomness, totalCount, winners: winners2 }; } const [round, randomness] = await fetchDrandRandomness(drandURL); const winners = select(count, values, Buffer.from(randomness, "hex")); - return { round, randomness, winners }; + return { round, randomness, totalCount, winners }; } async function fetchDrandRandomness(drandURL) { const drandClient = new import_drand_client.HttpChainClient(new import_drand_client.HttpCachingChain(drandURL)); diff --git a/package.json b/package.json index f679e13..07d231b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "dchoose", "description": "a CLI tool for choosing items from lists using drand", - "version": "1.0.2", + "version": "1.0.3", "bin": { "dchoose": "index.js" }, diff --git a/src/main.ts b/src/main.ts index ae250cd..bf2c6e4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,27 +16,29 @@ export async function main(params: CLIParams) { type DrawResult = { round?: number randomness?: string + totalCount: number winners: Array } export async function draw(params: CLIParams): Promise { const {values, count, drandURL} = params + const totalCount = values.length if (count === 0) { - return {winners: []} + return {totalCount, winners: []} } if (values.length <= count) { - return {winners: values} + return {totalCount, winners: values} } if (params.randomness) { const winners = select(count, values, Buffer.from(params.randomness, "hex")) - return {randomness: params.randomness, winners} + return {randomness: params.randomness, totalCount, winners} } const [round, randomness] = await fetchDrandRandomness(drandURL) const winners = select(count, values, Buffer.from(randomness, "hex")) - return {round, randomness, winners} + return {round, randomness, totalCount, winners} } async function fetchDrandRandomness(drandURL: string): Promise<[number, string]> { diff --git a/test/main.test.ts b/test/main.test.ts index 2d5bd67..804a1a2 100644 --- a/test/main.test.ts +++ b/test/main.test.ts @@ -33,6 +33,7 @@ describe("draws", () => { const result = await draw(params) expect(result.round).toBeUndefined() expect(result.randomness).toBeUndefined() + expect(result.totalCount).toEqual(3) expect(result.winners).toEqual([]) }) it("should return all the values for a count less than the number of values", async () => { @@ -45,6 +46,7 @@ describe("draws", () => { const result = await draw(params) expect(result.round).toBeUndefined() expect(result.randomness).toBeUndefined() + expect(result.totalCount).toEqual(3) expect(result.winners).toEqual(params.values) }) it("should return the same result each time for custom randomness", async () => { @@ -58,6 +60,7 @@ describe("draws", () => { const result = await draw(params) expect(result.round).toBeUndefined() expect(result.randomness).toEqual(params.randomness) + expect(result.totalCount).toEqual(3) const result2 = await draw(params) expect(result).toEqual(result2) @@ -72,6 +75,7 @@ describe("draws", () => { const result = await draw(params) expect(result.round).toBeGreaterThan(1) expect(result.winners).toHaveLength(params.count) + expect(result.totalCount).toEqual(3) }) }) })