Skip to content

Commit 4622ec2

Browse files
authored
Merge 5ead9be into 5ade5da
2 parents 5ade5da + 5ead9be commit 4622ec2

File tree

8 files changed

+288
-65
lines changed

8 files changed

+288
-65
lines changed

packages/client/lib/cluster/commands.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ import * as SISMEMBER from '../commands/SISMEMBER';
110110
import * as SMEMBERS from '../commands/SMEMBERS';
111111
import * as SMISMEMBER from '../commands/SMISMEMBER';
112112
import * as SMOVE from '../commands/SMOVE';
113+
import * as SORT_RO from '../commands/SORT_RO';
114+
import * as SORT_STORE from '../commands/SORT_STORE';
113115
import * as SORT from '../commands/SORT';
114116
import * as SPOP from '../commands/SPOP';
115117
import * as SRANDMEMBER_COUNT from '../commands/SRANDMEMBER_COUNT';
@@ -408,6 +410,10 @@ export default {
408410
smIsMember: SMISMEMBER,
409411
SMOVE,
410412
sMove: SMOVE,
413+
SORT_RO,
414+
sortRo: SORT_RO,
415+
SORT_STORE,
416+
sortStore: SORT_STORE,
411417
SORT,
412418
sort: SORT,
413419
SPOP,

packages/client/lib/commands/SORT.spec.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,7 @@ describe('SORT', () => {
7070
);
7171
});
7272

73-
it('with STORE', () => {
74-
assert.deepEqual(
75-
transformArguments('key', {
76-
STORE: 'destination'
77-
}),
78-
['SORT', 'key', 'STORE', 'destination']
79-
);
80-
});
81-
82-
it('with BY, LIMIT, GET, DIRECTION, ALPHA, STORE', () => {
73+
it('with BY, LIMIT, GET, DIRECTION, ALPHA', () => {
8374
assert.deepEqual(
8475
transformArguments('key', {
8576
BY: 'pattern',
@@ -89,10 +80,9 @@ describe('SORT', () => {
8980
},
9081
GET: 'pattern',
9182
DIRECTION: 'ASC',
92-
ALPHA: true,
93-
STORE: 'destination'
83+
ALPHA: true
9484
}),
95-
['SORT', 'key', 'BY', 'pattern', 'LIMIT', '0', '1', 'GET', 'pattern', 'ASC', 'ALPHA', 'STORE', 'destination']
85+
['SORT', 'key', 'BY', 'pattern', 'LIMIT', '0', '1', 'GET', 'pattern', 'ASC', 'ALPHA']
9686
);
9787
});
9888
});

packages/client/lib/commands/SORT.ts

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,13 @@
1-
export const FIRST_KEY_INDEX = 1;
2-
3-
export const IS_READ_ONLY = true;
4-
5-
interface SortOptions {
6-
BY?: string;
7-
LIMIT?: {
8-
offset: number;
9-
count: number;
10-
},
11-
GET?: string | Array<string>;
12-
DIRECTION?: 'ASC' | 'DESC';
13-
ALPHA?: true;
14-
STORE?: string;
15-
}
16-
17-
export function transformArguments(key: string, options?: SortOptions): Array<string> {
18-
const args = ['SORT', key];
19-
20-
if (options?.BY) {
21-
args.push('BY', options.BY);
22-
}
1+
import { RedisCommandArguments } from '.';
2+
import { pushSortArguments, SortOptions } from './generic-transformers';
233

24-
if (options?.LIMIT) {
25-
args.push(
26-
'LIMIT',
27-
options.LIMIT.offset.toString(),
28-
options.LIMIT.count.toString()
29-
);
30-
}
31-
32-
if (options?.GET) {
33-
for (const pattern of (typeof options.GET === 'string' ? [options.GET] : options.GET)) {
34-
args.push('GET', pattern);
35-
}
36-
}
37-
38-
if (options?.DIRECTION) {
39-
args.push(options.DIRECTION);
40-
}
41-
42-
if (options?.ALPHA) {
43-
args.push('ALPHA');
44-
}
45-
46-
if (options?.STORE) {
47-
args.push('STORE', options.STORE);
48-
}
4+
export const FIRST_KEY_INDEX = 1;
495

50-
return args;
6+
export function transformArguments(
7+
key: string,
8+
options?: SortOptions
9+
): RedisCommandArguments {
10+
return pushSortArguments(['SORT', key], options);
5111
}
5212

53-
// integer when using `STORE`
54-
export function transformReply(reply: Array<string> | number): Array<string> | number {
55-
return reply;
56-
}
13+
export declare function transformReply(): Array<string>;
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { transformArguments } from './SORT_RO';
4+
5+
describe('SORT_RO', () => {
6+
testUtils.isVersionGreaterThanHook([7, 0]);
7+
8+
describe('transformArguments', () => {
9+
it('simple', () => {
10+
assert.deepEqual(
11+
transformArguments('key'),
12+
['SORT_RO', 'key']
13+
);
14+
});
15+
16+
it('with BY', () => {
17+
assert.deepEqual(
18+
transformArguments('key', {
19+
BY: 'pattern'
20+
}),
21+
['SORT_RO', 'key', 'BY', 'pattern']
22+
);
23+
});
24+
25+
it('with LIMIT', () => {
26+
assert.deepEqual(
27+
transformArguments('key', {
28+
LIMIT: {
29+
offset: 0,
30+
count: 1
31+
}
32+
}),
33+
['SORT_RO', 'key', 'LIMIT', '0', '1']
34+
);
35+
});
36+
37+
describe('with GET', () => {
38+
it('string', () => {
39+
assert.deepEqual(
40+
transformArguments('key', {
41+
GET: 'pattern'
42+
}),
43+
['SORT_RO', 'key', 'GET', 'pattern']
44+
);
45+
});
46+
47+
it('array', () => {
48+
assert.deepEqual(
49+
transformArguments('key', {
50+
GET: ['1', '2']
51+
}),
52+
['SORT_RO', 'key', 'GET', '1', 'GET', '2']
53+
);
54+
});
55+
});
56+
57+
it('with DIRECTION', () => {
58+
assert.deepEqual(
59+
transformArguments('key', {
60+
DIRECTION: 'ASC'
61+
}),
62+
['SORT_RO', 'key', 'ASC']
63+
);
64+
});
65+
66+
it('with ALPHA', () => {
67+
assert.deepEqual(
68+
transformArguments('key', {
69+
ALPHA: true
70+
}),
71+
['SORT_RO', 'key', 'ALPHA']
72+
);
73+
});
74+
75+
it('with BY, LIMIT, GET, DIRECTION, ALPHA', () => {
76+
assert.deepEqual(
77+
transformArguments('key', {
78+
BY: 'pattern',
79+
LIMIT: {
80+
offset: 0,
81+
count: 1
82+
},
83+
GET: 'pattern',
84+
DIRECTION: 'ASC',
85+
ALPHA: true,
86+
}),
87+
['SORT_RO', 'key', 'BY', 'pattern', 'LIMIT', '0', '1', 'GET', 'pattern', 'ASC', 'ALPHA']
88+
);
89+
});
90+
});
91+
92+
testUtils.testWithClient('client.sortRo', async client => {
93+
assert.deepEqual(
94+
await client.sortRo('key'),
95+
[]
96+
);
97+
}, GLOBAL.SERVERS.OPEN);
98+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { RedisCommandArguments } from '.';
2+
import { pushSortArguments, SortOptions } from "./generic-transformers";
3+
4+
export const FIRST_KEY_INDEX = 1;
5+
6+
export const IS_READ_ONLY = true;
7+
8+
export function transformArguments(
9+
key: string,
10+
options?: SortOptions
11+
): RedisCommandArguments {
12+
return pushSortArguments(['SORT_RO', key], options);
13+
}
14+
15+
export declare function transformReply(): Array<string>;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { transformArguments } from './SORT_STORE';
4+
5+
describe('SORT STORE', () => {
6+
describe('transformArguments', () => {
7+
it('simple', () => {
8+
assert.deepEqual(
9+
transformArguments('source', 'destination'),
10+
['SORT', 'source', 'STORE', 'destination']
11+
);
12+
});
13+
14+
it('with BY', () => {
15+
assert.deepEqual(
16+
transformArguments('source', 'destination', {
17+
BY: 'pattern'
18+
}),
19+
['SORT', 'source', 'BY', 'pattern', 'STORE', 'destination']
20+
);
21+
});
22+
23+
it('with LIMIT', () => {
24+
assert.deepEqual(
25+
transformArguments('source', 'destination', {
26+
LIMIT: {
27+
offset: 0,
28+
count: 1
29+
}
30+
}),
31+
['SORT', 'source', 'LIMIT', '0', '1', 'STORE', 'destination']
32+
);
33+
});
34+
35+
describe('with GET', () => {
36+
it('string', () => {
37+
assert.deepEqual(
38+
transformArguments('source', 'destination', {
39+
GET: 'pattern'
40+
}),
41+
['SORT', 'source', 'GET', 'pattern', 'STORE', 'destination']
42+
);
43+
});
44+
45+
it('array', () => {
46+
assert.deepEqual(
47+
transformArguments('source', 'destination', {
48+
GET: ['1', '2']
49+
}),
50+
['SORT', 'source', 'GET', '1', 'GET', '2', 'STORE', 'destination']
51+
);
52+
});
53+
});
54+
55+
it('with DIRECTION', () => {
56+
assert.deepEqual(
57+
transformArguments('source', 'destination', {
58+
DIRECTION: 'ASC'
59+
}),
60+
['SORT', 'source', 'ASC', 'STORE', 'destination']
61+
);
62+
});
63+
64+
it('with ALPHA', () => {
65+
assert.deepEqual(
66+
transformArguments('source', 'destination', {
67+
ALPHA: true
68+
}),
69+
['SORT', 'source', 'ALPHA', 'STORE', 'destination']
70+
);
71+
});
72+
73+
it('with BY, LIMIT, GET, DIRECTION, ALPHA', () => {
74+
assert.deepEqual(
75+
transformArguments('source', 'destination', {
76+
BY: 'pattern',
77+
LIMIT: {
78+
offset: 0,
79+
count: 1
80+
},
81+
GET: 'pattern',
82+
DIRECTION: 'ASC',
83+
ALPHA: true
84+
}),
85+
['SORT', 'source', 'BY', 'pattern', 'LIMIT', '0', '1', 'GET', 'pattern', 'ASC', 'ALPHA', 'STORE', 'destination']
86+
);
87+
});
88+
});
89+
90+
testUtils.testWithClient('client.sortStore', async client => {
91+
assert.equal(
92+
await client.sortStore('source', 'destination'),
93+
0
94+
);
95+
}, GLOBAL.SERVERS.OPEN);
96+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { RedisCommandArguments } from '.';
2+
import { SortOptions } from './generic-transformers';
3+
import { transformArguments as transformSortArguments } from './SORT';
4+
5+
export const FIRST_KEY_INDEX = 1;
6+
7+
export function transformArguments(
8+
source: string,
9+
destination: string,
10+
options?: SortOptions
11+
): RedisCommandArguments {
12+
const args = transformSortArguments(source, options);
13+
args.push('STORE', destination);
14+
return args;
15+
}
16+
17+
export declare function transformReply(): number;

packages/client/lib/commands/generic-transformers.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,50 @@ export function transformCommandReply(
423423
};
424424
}
425425

426+
export interface SortOptions {
427+
BY?: string;
428+
LIMIT?: {
429+
offset: number;
430+
count: number;
431+
},
432+
GET?: string | Array<string>;
433+
DIRECTION?: 'ASC' | 'DESC';
434+
ALPHA?: true;
435+
}
436+
437+
export function pushSortArguments(
438+
args: RedisCommandArguments,
439+
options?: SortOptions
440+
): RedisCommandArguments {
441+
if (options?.BY) {
442+
args.push('BY', options.BY);
443+
}
444+
445+
if (options?.LIMIT) {
446+
args.push(
447+
'LIMIT',
448+
options.LIMIT.offset.toString(),
449+
options.LIMIT.count.toString()
450+
);
451+
}
452+
453+
if (options?.GET) {
454+
for (const pattern of (typeof options.GET === 'string' ? [options.GET] : options.GET)) {
455+
args.push('GET', pattern);
456+
}
457+
}
458+
459+
if (options?.DIRECTION) {
460+
args.push(options.DIRECTION);
461+
}
462+
463+
if (options?.ALPHA) {
464+
args.push('ALPHA');
465+
}
466+
467+
return args;
468+
}
469+
426470
export interface SlotRange {
427471
start: number;
428472
end: number;

0 commit comments

Comments
 (0)