import type { CommandOptions } from "./command"; import { Command } from "./command"; type ArOpKeyword = "SUM" | "MIN" | "MAX" | "AND" | "OR" | "XOR" | "USED"; /** * Reduction applied by `AROP`: * - `SUM`, `MIN`, `MAX`: numeric aggregates * - `AND`, `OR`, `XOR`: bitwise folds over the values as 64-bit integers * - `USED`: number of occupied slots * - `{ match: value }`: number of slots whose value equals `value` */ export type ArOpOperation = ArOpKeyword | Lowercase | { match: TData }; /** * Reduces the values in the inclusive range `[start, end]` to a single number on the server. * * `SUM`, `MIN`, `MAX`, `AND`, `OR` and `XOR` skip non-numeric values and return `null` when the * range contains nothing they could use. `USED` and `match` always return a number. * * Results are returned as JavaScript numbers, so bitwise results beyond `Number.MAX_SAFE_INTEGER` * lose precision. * * @see https://upstash.com/docs/redis/commands/array/arop * @see node_modules/@upstash/redis/docs/commands/array/arop.mdx */ export class ArOpCommand extends Command { constructor( [key, start, end, operation]: [ key: string, start: number | string, end: number | string, operation: ArOpOperation, ], opts?: CommandOptions ) { const command: unknown[] = ["AROP", key, start, end]; if (typeof operation === "string") { command.push(operation.toUpperCase()); } else { command.push("MATCH", operation.match); } super(command, { deserialize: (result) => (result === null ? null : Number(result)), ...opts, }); } }