You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-03 04:01:40 +03:00
69 lines
1.9 KiB
TypeScript
69 lines
1.9 KiB
TypeScript
import { RedisCommandArgument, RedisCommandArguments } from '.';
|
|
|
|
export const FIRST_KEY_INDEX = 1;
|
|
|
|
type Types = RedisCommandArgument | number;
|
|
|
|
type HSETObject = Record<string | number, Types>;
|
|
|
|
type HSETMap = Map<Types, Types>;
|
|
|
|
type HSETTuples = Array<[Types, Types]> | Array<Types>;
|
|
|
|
type GenericArguments = [key: RedisCommandArgument];
|
|
|
|
type SingleFieldArguments = [...generic: GenericArguments, field: Types, value: Types];
|
|
|
|
type MultipleFieldsArguments = [...generic: GenericArguments, value: HSETObject | HSETMap | HSETTuples];
|
|
|
|
export function transformArguments(...[ key, value, fieldValue ]: SingleFieldArguments | MultipleFieldsArguments): RedisCommandArguments {
|
|
const args: RedisCommandArguments = ['HSET', key];
|
|
|
|
if (typeof value === 'string' || typeof value === 'number' || Buffer.isBuffer(value)) {
|
|
pushValue(args, value);
|
|
pushValue(args, fieldValue!);
|
|
} else if (value instanceof Map) {
|
|
pushMap(args, value);
|
|
} else if (Array.isArray(value)) {
|
|
pushTuples(args, value);
|
|
} else {
|
|
pushObject(args, value);
|
|
}
|
|
|
|
return args;
|
|
}
|
|
|
|
function pushMap(args: RedisCommandArguments, map: HSETMap): void {
|
|
for (const [key, value] of map.entries()) {
|
|
pushValue(args, key);
|
|
pushValue(args, value);
|
|
}
|
|
}
|
|
|
|
function pushTuples(args: RedisCommandArguments, tuples: HSETTuples): void {
|
|
for (const tuple of tuples) {
|
|
if (Array.isArray(tuple)) {
|
|
pushTuples(args, tuple);
|
|
continue;
|
|
}
|
|
|
|
pushValue(args, tuple);
|
|
}
|
|
}
|
|
|
|
function pushObject(args: RedisCommandArguments, object: HSETObject): void {
|
|
for (const key of Object.keys(object)) {
|
|
args.push(key.toString(), object[key].toString());
|
|
}
|
|
}
|
|
|
|
function pushValue(args: RedisCommandArguments, value: Types): void {
|
|
args.push(
|
|
typeof value === 'number' ?
|
|
value.toString() :
|
|
value
|
|
);
|
|
}
|
|
|
|
export declare function transformReply(): number;
|