diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 2d85a466..fcfb330b 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -430,9 +430,9 @@ jobs: pnpm add -g wrangler working-directory: examples/cloudflare-workers - - name: Add account ID - run: echo 'account_id = "${{ secrets.CLOUDFLARE_ACCOUNT_ID }}"' >> wrangler.toml - working-directory: examples/cloudflare-workers + # - name: Add account ID + # run: echo 'account_id = "${{ secrets.CLOUDFLARE_ACCOUNT_ID }}"' >> wrangler.toml + # working-directory: examples/cloudflare-workers - name: Start example run: wrangler dev & sleep 5 @@ -440,8 +440,6 @@ jobs: env: CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }} - # - run: while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' localhost:8787)" != "200" ]]; do sleep 1; done - # timeout-minutes: 2 - name: Test run: deno test -A ./test.ts diff --git a/pkg/commands/zrange.test.ts b/pkg/commands/zrange.test.ts index ca025ca3..d961de6f 100644 --- a/pkg/commands/zrange.test.ts +++ b/pkg/commands/zrange.test.ts @@ -132,3 +132,46 @@ Deno.test("bylex", async (t) => { assertEquals(res3![1], "b"); }); }); + +Deno.test("rev", async (t) => { + await t.step("returns the set in reverse order", async () => { + const key = newKey(); + const score1 = 2; + const member1 = randomID(); + + const score2 = 5; + const member2 = randomID(); + + await new ZAddCommand([ + key, + { score: score1, member: member1 }, + { score: score2, member: member2 }, + ]).exec(client); + + const res = await new ZRangeCommand([key, 0, 7, { rev: true }]).exec( + client, + ); + assertEquals(res.length, 2); + assertEquals(res![0], member2); + assertEquals(res![1], member1); + }); +}); + +Deno.test("limit", async (t) => { + await t.step("returns only the first 2", async () => { + const key = newKey(); + for (let i = 0; i < 10; i++) { + await new ZAddCommand([ + key, + { score: i, member: randomID() }, + ]).exec(client); + } + + const res = await new ZRangeCommand([key, 0, 7, { offset: 0, count: 2 }]) + .exec( + client, + ); + console.log({ res }); + assertEquals(res.length, 2); + }); +}); diff --git a/pkg/commands/zrange.ts b/pkg/commands/zrange.ts index fe2c0fba..7edfcdb1 100644 --- a/pkg/commands/zrange.ts +++ b/pkg/commands/zrange.ts @@ -1,13 +1,19 @@ import { Command, CommandOptions } from "./command.ts"; export type ZRangeCommandOptions = - & { withScores?: boolean } + & { + withScores?: boolean; + rev?: boolean; + } & ( | { byScore: true; byLex?: never } | { byScore?: never; byLex: true } | { byScore?: never; byLex?: never } + ) + & ( + | { offset: number; count: number } + | { offset?: never; count?: never } ); - /** * @see https://redis.io/commands/zrange */ @@ -55,6 +61,14 @@ export class ZRangeCommand extends Command< if (opts?.byLex) { command.push("bylex"); } + if (opts?.rev) { + command.push("rev"); + } + if ( + typeof opts?.count !== "undefined" && typeof opts?.offset !== "undefined" + ) { + command.push("limit", opts.offset, opts.count); + } if (opts?.withScores) { command.push("withscores"); }