Skip to content

Commit

Permalink
added total count to the verbose output
Browse files Browse the repository at this point in the history
  • Loading branch information
CluEleSsUK committed Nov 8, 2023
1 parent 5cb3a72 commit 8fbb4bb
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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"]}`
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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"
},
Expand Down
10 changes: 6 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@ export async function main(params: CLIParams) {
type DrawResult = {
round?: number
randomness?: string
totalCount: number
winners: Array<string>
}

export async function draw(params: CLIParams): Promise<DrawResult> {
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]> {
Expand Down
4 changes: 4 additions & 0 deletions test/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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)
Expand All @@ -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)
})
})
})

0 comments on commit 8fbb4bb

Please sign in to comment.