Skip to content

Commit 43e546d

Browse files
committed
fix #1665 - add ZRANGEBYLEX, ZRANGEBYSCORE, ZRANGEBYSCORE_WITHSCORES
1 parent 068f7f4 commit 43e546d

7 files changed

+197
-0
lines changed

lib/commands/ZRANGEBYLEX.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { strict as assert } from 'assert';
2+
import { TestRedisServers, itWithClient } from '../test-utils';
3+
import { transformArguments } from './ZRANGEBYLEX';
4+
5+
describe('ZRANGEBYLEX', () => {
6+
describe('transformArguments', () => {
7+
it('simple', () => {
8+
assert.deepEqual(
9+
transformArguments('src', '-', '+'),
10+
['ZRANGEBYLEX', 'src', '-', '+']
11+
);
12+
});
13+
14+
it('with LIMIT', () => {
15+
assert.deepEqual(
16+
transformArguments('src', '-', '+', {
17+
LIMIT: {
18+
offset: 0,
19+
count: 1
20+
}
21+
}),
22+
['ZRANGEBYLEX', 'src', '-', '+', 'LIMIT', '0', '1']
23+
);
24+
});
25+
});
26+
27+
itWithClient(TestRedisServers.OPEN, 'client.zRangeByLex', async client => {
28+
assert.deepEqual(
29+
await client.zRangeByLex('src', '-', '+'),
30+
[]
31+
);
32+
});
33+
});

lib/commands/ZRANGEBYLEX.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { TransformArgumentsReply } from '.';
2+
import { transformArgumentNumberInfinity } from './generic-transformers';
3+
4+
export const FIRST_KEY_INDEX = 1;
5+
6+
export const IS_READ_ONLY = true;
7+
8+
export interface ZRangeByLexOptions {
9+
LIMIT?: {
10+
offset: number;
11+
count: number;
12+
};
13+
}
14+
15+
export function transformArguments(
16+
key: string,
17+
min: number | string,
18+
max: number | string,
19+
options?: ZRangeByLexOptions
20+
): TransformArgumentsReply {
21+
const args = [
22+
'ZRANGEBYLEX',
23+
key,
24+
typeof min === 'string' ? min : transformArgumentNumberInfinity(min),
25+
typeof max === 'string' ? max : transformArgumentNumberInfinity(max)
26+
];
27+
28+
if (options?.LIMIT) {
29+
args.push('LIMIT', options.LIMIT.offset.toString(), options.LIMIT.count.toString());
30+
}
31+
32+
return args;
33+
}
34+
35+
export declare function transformReply(): Array<string>;

lib/commands/ZRANGEBYSCORE.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { strict as assert } from 'assert';
2+
import { TestRedisServers, itWithClient } from '../test-utils';
3+
import { transformArguments } from './ZRANGEBYSCORE';
4+
5+
describe('ZRANGEBYSCORE', () => {
6+
describe('transformArguments', () => {
7+
it('simple', () => {
8+
assert.deepEqual(
9+
transformArguments('src', 0, 1),
10+
['ZRANGEBYSCORE', 'src', '0', '1']
11+
);
12+
});
13+
14+
it('with LIMIT', () => {
15+
assert.deepEqual(
16+
transformArguments('src', 0, 1, {
17+
LIMIT: {
18+
offset: 0,
19+
count: 1
20+
}
21+
}),
22+
['ZRANGEBYSCORE', 'src', '0', '1', 'LIMIT', '0', '1']
23+
);
24+
});
25+
});
26+
27+
itWithClient(TestRedisServers.OPEN, 'client.zRangeByScore', async client => {
28+
assert.deepEqual(
29+
await client.zRangeByScore('src', 0, 1),
30+
[]
31+
);
32+
});
33+
});

lib/commands/ZRANGEBYSCORE.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { TransformArgumentsReply } from '.';
2+
import { transformArgumentNumberInfinity } from './generic-transformers';
3+
4+
export const FIRST_KEY_INDEX = 1;
5+
6+
export const IS_READ_ONLY = true;
7+
8+
export interface ZRangeByScoreOptions {
9+
LIMIT?: {
10+
offset: number;
11+
count: number;
12+
};
13+
}
14+
15+
export function transformArguments(
16+
key: string,
17+
min: number | string,
18+
max: number | string,
19+
options?: ZRangeByScoreOptions
20+
): TransformArgumentsReply {
21+
const args = [
22+
'ZRANGEBYSCORE',
23+
key,
24+
typeof min === 'string' ? min : transformArgumentNumberInfinity(min),
25+
typeof max === 'string' ? max : transformArgumentNumberInfinity(max)
26+
];
27+
28+
if (options?.LIMIT) {
29+
args.push('LIMIT', options.LIMIT.offset.toString(), options.LIMIT.count.toString());
30+
}
31+
32+
return args;
33+
}
34+
35+
export declare function transformReply(): Array<string>;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { strict as assert } from 'assert';
2+
import { TestRedisServers, itWithClient } from '../test-utils';
3+
import { transformArguments } from './ZRANGEBYSCORE_WITHSCORES';
4+
5+
describe('ZRANGEBYSCORE WITHSCORES', () => {
6+
describe('transformArguments', () => {
7+
it('simple', () => {
8+
assert.deepEqual(
9+
transformArguments('src', 0, 1),
10+
['ZRANGEBYSCORE', 'src', '0', '1', 'WITHSCORES']
11+
);
12+
});
13+
14+
it('with LIMIT', () => {
15+
assert.deepEqual(
16+
transformArguments('src', 0, 1, {
17+
LIMIT: {
18+
offset: 0,
19+
count: 1
20+
}
21+
}),
22+
['ZRANGEBYSCORE', 'src', '0', '1', 'LIMIT', '0', '1', 'WITHSCORES']
23+
);
24+
});
25+
});
26+
27+
itWithClient(TestRedisServers.OPEN, 'client.zRangeByScoreWithScores', async client => {
28+
assert.deepEqual(
29+
await client.zRangeByScoreWithScores('src', 0, 1),
30+
[]
31+
);
32+
});
33+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { TransformArgumentsReply } from '.';
2+
import { transformReplySortedSetWithScores } from './generic-transformers';
3+
import { ZRangeByScoreOptions, transformArguments as transformZRangeByScoreArguments } from './ZRANGEBYSCORE';
4+
5+
export { FIRST_KEY_INDEX, IS_READ_ONLY } from './ZRANGEBYSCORE';
6+
7+
export function transformArguments(
8+
key: string,
9+
min: number | string,
10+
max: number | string,
11+
options?: ZRangeByScoreOptions
12+
): TransformArgumentsReply {
13+
return [
14+
...transformZRangeByScoreArguments(key, min, max, options),
15+
'WITHSCORES'
16+
];
17+
}
18+
19+
export const transformReply = transformReplySortedSetWithScores;

lib/commands/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ import * as ZRANDMEMBER_COUNT from './ZRANDMEMBER_COUNT';
232232
import * as ZRANDMEMBER from './ZRANDMEMBER';
233233
import * as ZRANGE_WITHSCORES from './ZRANGE_WITHSCORES';
234234
import * as ZRANGE from './ZRANGE';
235+
import * as ZRANGEBYLEX from './ZRANGEBYLEX';
236+
import * as ZRANGEBYSCORE_WITHSCORES from './ZRANGEBYSCORE_WITHSCORES';
237+
import * as ZRANGEBYSCORE from './ZRANGEBYSCORE';
235238
import * as ZRANGESTORE from './ZRANGESTORE';
236239
import * as ZRANK from './ZRANK';
237240
import * as ZREM from './ZREM';
@@ -713,6 +716,12 @@ export default {
713716
zRangeWithScores: ZRANGE_WITHSCORES,
714717
ZRANGE,
715718
zRange: ZRANGE,
719+
ZRANGEBYLEX,
720+
zRangeByLex: ZRANGEBYLEX,
721+
ZRANGEBYSCORE_WITHSCORES,
722+
zRangeByScoreWithScores: ZRANGEBYSCORE_WITHSCORES,
723+
ZRANGEBYSCORE,
724+
zRangeByScore: ZRANGEBYSCORE,
716725
ZRANGESTORE,
717726
zRangeStore: ZRANGESTORE,
718727
ZRANK,

0 commit comments

Comments
 (0)