Skip to content

Commit

Permalink
fix: bitfield (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp authored Mar 14, 2020
1 parent 69dae79 commit fd772d5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
8 changes: 4 additions & 4 deletions command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ export type RedisCommands = {
bitcount(key: string): Promise<Integer>;
bitcount(key: string, start: number, end: number): Promise<Integer>;
bitfield(key: string, opts?: {
get?: { type: string; offset: number };
set?: { type: string; offset: number };
incrby?: { type: string; offset: number; increment: number };
get?: { type: string; offset: number | string };
set?: { type: string; offset: number | string; value: number };
incrby?: { type: string; offset: number | string; increment: number };
}): Promise<Integer[]>;
bitfield(key: string, opts?: {
get?: { type: string; offset: number };
set?: { type: string; offset: number };
set?: { type: string; offset: number; value: number };
incrby?: { type: string; offset: number; increment: number };
overflow: "WRAP" | "SAT" | "FAIL";
}): Promise<(Integer | BulkNil)[]>;
Expand Down
25 changes: 23 additions & 2 deletions redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,29 @@ class RedisImpl implements RedisCommands {
} else return this.execIntegerReply("BITCOUNT", key);
}

bitfield(key: string) {
return this.execArrayReply("BITFIELD", key) as Promise<number[]>;
bitfield(key: string, opts?: {
get?: { type: string; offset: number | string };
set?: { type: string; offset: number | string; value: number };
incrby?: { type: string; offset: number | string; increment: number };
overflow?: "WRAP" | "SAT" | "FAIL";
}) {
const args: (number | string)[] = [key];
if (opts?.get) {
const { type, offset } = opts.get;
args.push("GET", type, offset);
}
if (opts?.set) {
const { type, offset, value } = opts.set;
args.push("SET", type, offset, value);
}
if (opts?.incrby) {
const { type, offset, increment } = opts.incrby;
args.push("INCRBY", type, offset, increment);
}
if (opts?.overflow) {
args.push("OVERFLOW", opts.overflow);
}
return this.execArrayReply("BITFIELD", ...args) as Promise<number[]>;
}

bitop(operation: string, destkey: string, ...keys: string[]) {
Expand Down
1 change: 0 additions & 1 deletion tests/geo_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { connect } from "../redis.ts";
import {
assertEquals
} from "../vendor/https/deno.land/std/testing/asserts.ts";
Expand Down
17 changes: 17 additions & 0 deletions tests/string_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ test("bitfieldWithoutOperations", async () => {
assertEquals(v, []);
});

test("bitfield with opts", async () => {
await client.set("key", "4660");
const v = await client.bitfield("key", {
get: { type: "u8", offset: 0 },
set: { type: "i5", offset: 1, value: 0 },
incrby: { type: "u16", offset: 2, increment: 2 }
});
assertEquals(v, [52, 13, 218]);
});

test("bitfield with overflow", async () => {
const v = await client.bitfield("key", {
overflow: "FAIL"
});
assertEquals(v, []);
});

test("bitop", async () => {
await client.set("key1", "foo"); // 01100110 01101111 01101111
await client.set("key2", "bar"); // 01100010 01100001 01110010
Expand Down

0 comments on commit fd772d5

Please sign in to comment.