import { parseJSON, setOwnProperty } from "../util"; import type { CommandOptions } from "./command"; import { Command } from "./command"; export class XRevRangeCommand< TData extends Record>, > extends Command { constructor( [key, end, start, count]: [key: string, end: string, start: string, count?: number], opts?: CommandOptions ) { const command: unknown[] = ["XREVRANGE", key, end, start]; if (typeof count === "number") { command.push("COUNT", count); } super(command, { deserialize: (result) => deserialize(result as any), ...opts, }); } } function deserialize>>( result: [string, string[]][] ): TData { const obj: Record> = {}; for (const e of result) { for (let i = 0; i < e.length; i += 2) { const streamId = e[i] as string; const entries = e[i + 1]; if (!Object.prototype.hasOwnProperty.call(obj, streamId)) { setOwnProperty(obj, streamId, {}); } for (let j = 0; j < entries.length; j += 2) { const field = (entries as string[])[j]; const value = (entries as string[])[j + 1]; try { setOwnProperty(obj[streamId], field, parseJSON(value)); } catch { setOwnProperty(obj[streamId], field, value); } } } } return obj as TData; }