You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-11 22:42:42 +03:00
* wip * Worked on phrasing etc for v5 doc changes. * Removed quite repetition of 'Rather' * Update v4-to-v5.md * Update v4-to-v5.md * Update v4-to-v5.md * WIP * WIP * clean SET command * some more commands, multi.exec<'typed'> * "typed" multi * WIP * upgrade deps * wip * wip * fix #2469 * wip * npm update * wip * wip * wip * wip * some tests * tests.yml * wip * wip * merge master into v5 * some more commands * some more commands * WIP * Release client@2.0.0-next.1 --------- Co-authored-by: Simon Prickett <simon@redislabs.com>
76 lines
1.9 KiB
TypeScript
76 lines
1.9 KiB
TypeScript
import { RedisArgument, ArrayReply, BlobStringReply, Command } from '../RESP/types';
|
|
import { transformDoubleArgument } from './generic-transformers';
|
|
|
|
export type ZInterKeyAndWeight = {
|
|
key: RedisArgument;
|
|
weight: number;
|
|
};
|
|
|
|
export type ZInterKeys<T> = T | [T, ...Array<T>];
|
|
|
|
export interface ZInterOptions {
|
|
AGGREGATE?: 'SUM' | 'MIN' | 'MAX';
|
|
}
|
|
|
|
export function pushZInterArguments(
|
|
args: Array<RedisArgument>,
|
|
keys: ZInterKeys<RedisArgument> | ZInterKeys<ZInterKeyAndWeight>,
|
|
options?: ZInterOptions
|
|
) {
|
|
if (Array.isArray(keys)) {
|
|
args.push(keys.length.toString());
|
|
|
|
if (keys.length) {
|
|
if (isPlainKeys(keys)) {
|
|
args = args.concat(keys);
|
|
} else {
|
|
const start = args.length;
|
|
args[start + keys.length] = 'WEIGHTS';
|
|
for (let i = 0; i < keys.length; i++) {
|
|
const index = start + i;
|
|
args[index] = keys[i].key;
|
|
args[index + 1 + keys.length] = transformDoubleArgument(keys[i].weight);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
args.push('1');
|
|
|
|
if (isPlainKey(keys)) {
|
|
args.push(keys);
|
|
} else {
|
|
args.push(
|
|
keys.key,
|
|
'WEIGHTS',
|
|
transformDoubleArgument(keys.weight)
|
|
);
|
|
}
|
|
}
|
|
|
|
if (options?.AGGREGATE) {
|
|
args.push('AGGREGATE', options.AGGREGATE);
|
|
}
|
|
|
|
return args;
|
|
}
|
|
|
|
function isPlainKey(key: RedisArgument | ZInterKeyAndWeight): key is RedisArgument {
|
|
return typeof key === 'string' || Buffer.isBuffer(key);
|
|
}
|
|
|
|
function isPlainKeys(keys: Array<RedisArgument> | Array<ZInterKeyAndWeight>): keys is Array<RedisArgument> {
|
|
return isPlainKey(keys[0]);
|
|
}
|
|
|
|
export default {
|
|
FIRST_KEY_INDEX: 2,
|
|
IS_READ_ONLY: true,
|
|
transformArguments(
|
|
keys: ZInterKeys<RedisArgument> | ZInterKeys<ZInterKeyAndWeight>,
|
|
options?: ZInterOptions
|
|
) {
|
|
return pushZInterArguments(['ZINTER'], keys, options);
|
|
},
|
|
transformReply: undefined as unknown as () => ArrayReply<BlobStringReply>
|
|
} as const satisfies Command;
|