Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions pkg/commands/xadd.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { assert } from "https://deno.land/[email protected]/testing/asserts.ts";
import {
assert,
assertEquals,
} from "https://deno.land/[email protected]/testing/asserts.ts";
import { keygen, newHttpClient, randomID } from "../test-utils.ts";

import { afterAll } from "https://deno.land/[email protected]/testing/bdd.ts";
Expand Down Expand Up @@ -120,6 +123,6 @@ Deno.test("with threshold", async (t) => {
]).exec(client);

const xrangeRes = await new XRangeCommand([key, "-", "+"]).exec(client);
assert(Object.keys(xrangeRes).length === 1);
assertEquals(Object.keys(xrangeRes).length, 2);
});
});
23 changes: 23 additions & 0 deletions pkg/commands/xrange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,26 @@ Deno.test("limit", async (t) => {
});
});
});

Deno.test("many fields", async (t) => {
await t.step("returns all fields", async () => {
const key = newKey();

const fields: Record<string, string> = {};

for (let i = 1; i <= 10; i++) {
const field = randomID();
const value = randomID();
fields[field] = value;
const id = await new XAddCommand([
key,
"*",
fields,
]).exec(client);

const res = await new XRangeCommand([key, "-", "+"]).exec(client);
assertEquals(Object.keys(res).length, i);
assertEquals(res[id], fields);
}
});
});
35 changes: 17 additions & 18 deletions pkg/commands/xrange.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { Command, CommandOptions } from "./command.ts";

function deserialize<TData extends Record<string, Record<string, unknown>>>(
result: (string | string[])[],
result: [string, string[]][],
): TData {
if (result.length === 0) {
return {} as TData;
}
const obj: Record<string, Record<string, unknown>> = {};
while (result.length >= 2) {
const streamId = result.shift() as string;
const entries = result.shift()!;
for (const e of result) {
while (e.length >= 2) {
const streamId = e.shift() as string;
const entries = e.shift()!;

if (!(streamId in obj)) {
obj[streamId] = {};
}
while (entries.length >= 2) {
const field = (entries as string[]).shift()! as string;
const value = (entries as string[]).shift()! as string;
if (!(streamId in obj)) {
obj[streamId] = {};
}
while (entries.length >= 2) {
const field = (entries as string[]).shift()! as string;
const value = (entries as string[]).shift()! as string;

try {
obj[streamId][field] = JSON.parse(value);
} catch {
obj[streamId][field] = value;
try {
obj[streamId][field] = JSON.parse(value);
} catch {
obj[streamId][field] = value;
}
}
}
}
Expand All @@ -48,7 +47,7 @@ export class XRangeCommand<
command.push("COUNT", count);
}
super(command, {
deserialize: (result) => deserialize<any>(result[0] as any),
deserialize: (result) => deserialize<any>(result as any),
...opts,
});
}
Expand Down