1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-09 00:22:08 +03:00

clean code

This commit is contained in:
leibale
2022-04-14 15:01:15 -04:00
parent c26968a32b
commit 4558ec6a31
10 changed files with 89 additions and 85 deletions

View File

@@ -8,17 +8,14 @@ describe('BLMPOP', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments(0, 'key', {
SIDE: 'LEFT'
}),
transformArguments(0, 'key', 'LEFT'),
['BLMPOP', '0', '1', 'key', 'LEFT']
);
});
it('with score and count', () => {
it('with COUNT', () => {
assert.deepEqual(
transformArguments(0, 'key', {
SIDE: 'LEFT',
transformArguments(0, 'key', 'LEFT', {
COUNT: 2
}),
['BLMPOP', '0', '1', 'key', 'LEFT', 'COUNT', '2']
@@ -28,9 +25,7 @@ describe('BLMPOP', () => {
testUtils.testWithClient('client.zmScore', async client => {
assert.deepEqual(
await client.blmPop(0, 'key', {
SIDE: 'LEFT'
}),
await client.blmPop(0, 'key', 'RIGHT'),
null
);
}, GLOBAL.SERVERS.OPEN);

View File

@@ -1,14 +1,20 @@
import { RedisCommandArguments } from '.';
import { transformLMPopArguments, LMPopOptions } from './generic-transformers';
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { transformLMPopArguments, LMPopOptions, ListSide } from './generic-transformers';
export const FIRST_KEY_INDEX = 3;
export function transformArguments(
timeout: number,
keys: string | Array<string>,
options: LMPopOptions
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: ListSide,
options?: LMPopOptions
): RedisCommandArguments {
return transformLMPopArguments(['BLMPOP', timeout.toString()], keys, options);
return transformLMPopArguments(
['BLMPOP', timeout.toString()],
keys,
side,
options
);
}
export { transformReply } from './LMPOP';

View File

@@ -8,17 +8,14 @@ describe('BZMPOP', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments(0, 'key', {
SCORE: 'MIN'
}),
transformArguments(0, 'key', 'MIN'),
['BZMPOP', '0', '1', 'key', 'MIN']
);
});
it('with score and count', () => {
it('with COUNT', () => {
assert.deepEqual(
transformArguments(0, 'key', {
SCORE: 'MIN',
transformArguments(0, 'key', 'MIN', {
COUNT: 2
}),
['BZMPOP', '0', '1', 'key', 'MIN', 'COUNT', '2']
@@ -26,11 +23,9 @@ describe('BZMPOP', () => {
});
});
testUtils.testWithClient('client.zmScore', async client => {
testUtils.testWithClient('client.bzmPop', async client => {
assert.deepEqual(
await client.bzmPop(0, 'key', {
SCORE: 'MAX'
}),
await client.bzmPop(0, 'key', 'MAX'),
null
);
}, GLOBAL.SERVERS.OPEN);

View File

@@ -1,14 +1,20 @@
import { RedisCommandArguments } from '.';
import { transformZMPopArguments, ZMPopOptions } from './generic-transformers';
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { SortedSetSide, transformZMPopArguments, ZMPopOptions } from './generic-transformers';
export const FIRST_KEY_INDEX = 3;
export function transformArguments(
timeout: number,
keys: string | Array<string>,
options: ZMPopOptions
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: SortedSetSide,
options?: ZMPopOptions
): RedisCommandArguments {
return transformZMPopArguments(['BZMPOP', timeout.toString()], keys, options);
return transformZMPopArguments(
['BZMPOP', timeout.toString()],
keys,
side,
options
);
}
export { transformReply } from './ZMPOP';

View File

@@ -1,14 +1,13 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { ListSide } from './generic-transformers';
export const FIRST_KEY_INDEX = 1;
export type LMoveSide = 'LEFT' | 'RIGHT';
export function transformArguments(
source: RedisCommandArgument,
destination: RedisCommandArgument,
sourceSide: LMoveSide,
destinationSide: LMoveSide
sourceSide: ListSide,
destinationSide: ListSide
): RedisCommandArguments {
return [
'LMOVE',

View File

@@ -8,17 +8,14 @@ describe('LMPOP', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments('key', {
SIDE: 'LEFT'
}),
transformArguments('key', 'LEFT'),
['LMPOP', '1', 'key', 'LEFT']
);
});
it('with score and count', () => {
it('with COUNT', () => {
assert.deepEqual(
transformArguments('key', {
SIDE: 'LEFT',
transformArguments('key', 'LEFT', {
COUNT: 2
}),
['LMPOP', '1', 'key', 'LEFT', 'COUNT', '2']
@@ -26,11 +23,9 @@ describe('LMPOP', () => {
});
});
testUtils.testWithClient('client.zmScore', async client => {
testUtils.testWithClient('client.lmPop', async client => {
assert.deepEqual(
await client.lmPop('key', {
SIDE: 'RIGHT'
}),
await client.lmPop('key', 'RIGHT'),
null
);
}, GLOBAL.SERVERS.OPEN);

View File

@@ -1,20 +1,22 @@
import { RedisCommandArguments } from '.';
import { transformLMPopArguments, LMPopOptions } from './generic-transformers';
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { transformLMPopArguments, LMPopOptions, ListSide } from './generic-transformers';
export const FIRST_KEY_INDEX = 2;
export const IS_READ_ONLY = true;
export function transformArguments(
keys: string | Array<string>,
options: LMPopOptions
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: ListSide,
options?: LMPopOptions
): RedisCommandArguments {
return transformLMPopArguments(['LMPOP'], keys, options);
return transformLMPopArguments(
['LMPOP'],
keys,
side,
options
);
}
type LMPopReply = null | [
key: string,
elements: Array<String>
export declare function transformReply(): null | [
key: string,
elements: Array<string>
];
export declare function transformReply(): LMPopReply;

View File

@@ -8,17 +8,14 @@ describe('ZMPOP', () => {
describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments('key', {
SCORE: 'MIN'
}),
transformArguments('key', 'MIN'),
['ZMPOP', '1', 'key', 'MIN']
);
});
it('with score and count', () => {
assert.deepEqual(
transformArguments('key', {
SCORE: 'MIN',
transformArguments('key', 'MIN', {
COUNT: 2
}),
['ZMPOP', '1', 'key', 'MIN', 'COUNT', '2']
@@ -28,9 +25,7 @@ describe('ZMPOP', () => {
testUtils.testWithClient('client.zmScore', async client => {
assert.deepEqual(
await client.zmPop('key', {
SCORE: 'MAX'
}),
await client.zmPop('key', 'MIN'),
null
);
}, GLOBAL.SERVERS.OPEN);

View File

@@ -1,26 +1,34 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { transformSortedSetMemberReply, transformZMPopArguments, ZMember, ZMPopOptions } from './generic-transformers';
import { SortedSetSide, transformSortedSetMemberReply, transformZMPopArguments, ZMember, ZMPopOptions } from './generic-transformers';
export const FIRST_KEY_INDEX = 2;
export const IS_READ_ONLY = true;
export function transformArguments(
keys: string | Array<string>,
options: ZMPopOptions
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: SortedSetSide,
options?: ZMPopOptions
): RedisCommandArguments {
return transformZMPopArguments(['ZMPOP'], keys, options);
return transformZMPopArguments(
['ZMPOP'],
keys,
side,
options
);
}
type ZMPopRawReply = null | [string, Array<[RedisCommandArgument, RedisCommandArgument]>];
type ZMPopReply = null | [
key: string,
elements: Array<ZMember>
type ZMPopRawReply = null | [
key: string,
elements: Array<[RedisCommandArgument, RedisCommandArgument]>
];
export function transformReply(reply: ZMPopRawReply): ZMPopReply {
if (reply == null) return null;
type ZMPopReply = null | {
key: string,
elements: Array<ZMember>
};
return [reply[0], reply[1].map(transformSortedSetMemberReply)];
export function transformReply(reply: ZMPopRawReply): ZMPopReply {
return reply === null ? null : {
key: reply[0],
elements: reply[1].map(transformSortedSetMemberReply)
};
}

View File

@@ -157,20 +157,21 @@ export function transformSortedSetWithScoresReply(reply: Array<RedisCommandArgum
return members;
}
export type SortedSetSide = 'MIN' | 'MAX';
export interface ZMPopOptions {
SCORE: 'MIN' | 'MAX';
COUNT?: number;
}
export function transformZMPopArguments(
args: RedisCommandArguments,
keys: string | Array<string>,
options: ZMPopOptions
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: SortedSetSide,
options?: ZMPopOptions
): RedisCommandArguments {
pushVerdictArgument(args, keys);
args.push(options.SCORE);
args.push(side);
if (options?.COUNT) {
args.push('COUNT', options.COUNT.toString());
@@ -179,19 +180,21 @@ export function transformZMPopArguments(
return args;
}
export type ListSide = 'LEFT' | 'RIGHT';
export interface LMPopOptions {
SIDE: 'LEFT' | 'RIGHT';
COUNT?: number;
}
export function transformLMPopArguments(
args: RedisCommandArguments,
keys: string | Array<string>,
options: LMPopOptions
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: ListSide,
options?: LMPopOptions
): RedisCommandArguments {
pushVerdictArgument(args, keys);
args.push(options.SIDE);
args.push(side);
if (options?.COUNT) {
args.push('COUNT', options.COUNT.toString());