Skip to content

Commit 429b11e

Browse files
authored
fix #1959 - LCS (#2129)
* fix #1959 - LCS * newlines at end of files
1 parent 24c2c86 commit 429b11e

File tree

10 files changed

+288
-0
lines changed

10 files changed

+288
-0
lines changed

packages/client/lib/cluster/commands.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ import * as HVALS from '../commands/HVALS';
7171
import * as INCR from '../commands/INCR';
7272
import * as INCRBY from '../commands/INCRBY';
7373
import * as INCRBYFLOAT from '../commands/INCRBYFLOAT';
74+
import * as LCS_IDX_WITHMATCHLEN from '../commands/LCS_IDX_WITHMATCHLEN';
75+
import * as LCS_IDX from '../commands/LCS_IDX';
76+
import * as LCS_LEN from '../commands/LCS_LEN';
77+
import * as LCS from '../commands/LCS';
7478
import * as LINDEX from '../commands/LINDEX';
7579
import * as LINSERT from '../commands/LINSERT';
7680
import * as LLEN from '../commands/LLEN';
@@ -351,6 +355,14 @@ export default {
351355
incrBy: INCRBY,
352356
INCRBYFLOAT,
353357
incrByFloat: INCRBYFLOAT,
358+
LCS_IDX_WITHMATCHLEN,
359+
lcsIdxWithMatchLen: LCS_IDX_WITHMATCHLEN,
360+
LCS_IDX,
361+
lcsIdx: LCS_IDX,
362+
LCS_LEN,
363+
lcsLen: LCS_LEN,
364+
LCS,
365+
lcs: LCS,
354366
LINDEX,
355367
lIndex: LINDEX,
356368
LINSERT,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { transformArguments } from './LCS';
4+
5+
describe('LCS', () => {
6+
testUtils.isVersionGreaterThanHook([7]);
7+
8+
it('transformArguments', () => {
9+
assert.deepEqual(
10+
transformArguments('1', '2'),
11+
['LCS', '1', '2']
12+
);
13+
});
14+
15+
testUtils.testWithClient('client.lcs', async client => {
16+
assert.equal(
17+
await client.lcs('1', '2'),
18+
''
19+
);
20+
}, GLOBAL.SERVERS.OPEN);
21+
22+
testUtils.testWithCluster('cluster.lcs', async cluster => {
23+
assert.equal(
24+
await cluster.lcs('{tag}1', '{tag}2'),
25+
''
26+
);
27+
}, GLOBAL.CLUSTERS.OPEN);
28+
});

packages/client/lib/commands/LCS.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { RedisCommandArgument, RedisCommandArguments } from '.';
2+
3+
export const FIRST_KEY_INDEX = 1;
4+
5+
export const IS_READ_ONLY = true;
6+
7+
export function transformArguments(
8+
key1: RedisCommandArgument,
9+
key2: RedisCommandArgument
10+
): RedisCommandArguments {
11+
return [
12+
'LCS',
13+
key1,
14+
key2
15+
];
16+
}
17+
18+
export declare function transformReply(): string | Buffer;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { transformArguments } from './LCS_IDX';
4+
5+
describe('LCS_IDX', () => {
6+
testUtils.isVersionGreaterThanHook([7]);
7+
8+
it('transformArguments', () => {
9+
assert.deepEqual(
10+
transformArguments('1', '2'),
11+
['LCS', '1', '2', 'IDX']
12+
);
13+
});
14+
15+
testUtils.testWithClient('client.lcsIdx', async client => {
16+
const [, reply] = await Promise.all([
17+
client.mSet({
18+
'1': 'abc',
19+
'2': 'bc'
20+
}),
21+
client.lcsIdx('1', '2')
22+
]);
23+
24+
assert.deepEqual(
25+
reply,
26+
{
27+
matches: [{
28+
key1: {
29+
start: 1,
30+
end: 2
31+
},
32+
key2: {
33+
start: 0,
34+
end: 1
35+
}
36+
}],
37+
length: 2
38+
}
39+
);
40+
}, GLOBAL.SERVERS.OPEN);
41+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { RedisCommandArgument, RedisCommandArguments } from '.';
2+
import { RangeReply, RawRangeReply, transformRangeReply } from './generic-transformers';
3+
import { transformArguments as transformLcsArguments } from './LCS';
4+
5+
export { FIRST_KEY_INDEX, IS_READ_ONLY } from './LCS';
6+
7+
export function transformArguments(
8+
key1: RedisCommandArgument,
9+
key2: RedisCommandArgument
10+
): RedisCommandArguments {
11+
const args = transformLcsArguments(key1, key2);
12+
args.push('IDX');
13+
return args;
14+
}
15+
16+
type RawReply = [
17+
'matches',
18+
Array<[
19+
key1: RawRangeReply,
20+
key2: RawRangeReply
21+
]>,
22+
'len',
23+
number
24+
];
25+
26+
interface Reply {
27+
matches: Array<{
28+
key1: RangeReply;
29+
key2: RangeReply;
30+
}>;
31+
length: number;
32+
}
33+
34+
export function transformReply(reply: RawReply): Reply {
35+
return {
36+
matches: reply[1].map(([key1, key2]) => ({
37+
key1: transformRangeReply(key1),
38+
key2: transformRangeReply(key2)
39+
})),
40+
length: reply[3]
41+
};
42+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { transformArguments } from './LCS_IDX_WITHMATCHLEN';
4+
5+
describe('LCS_IDX_WITHMATCHLEN', () => {
6+
testUtils.isVersionGreaterThanHook([7]);
7+
8+
it('transformArguments', () => {
9+
assert.deepEqual(
10+
transformArguments('1', '2'),
11+
['LCS', '1', '2', 'IDX', 'WITHMATCHLEN']
12+
);
13+
});
14+
15+
testUtils.testWithClient('client.lcsIdxWithMatchLen', async client => {
16+
const [, reply] = await Promise.all([
17+
client.mSet({
18+
'1': 'abc',
19+
'2': 'bc'
20+
}),
21+
client.lcsIdxWithMatchLen('1', '2')
22+
]);
23+
24+
assert.deepEqual(
25+
reply,
26+
{
27+
matches: [{
28+
key1: {
29+
start: 1,
30+
end: 2
31+
},
32+
key2: {
33+
start: 0,
34+
end: 1
35+
},
36+
length: 2
37+
}],
38+
length: 2
39+
}
40+
);
41+
}, GLOBAL.SERVERS.OPEN);
42+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { RedisCommandArgument, RedisCommandArguments } from '.';
2+
import { RangeReply, RawRangeReply, transformRangeReply } from './generic-transformers';
3+
import { transformArguments as transformLcsArguments } from './LCS';
4+
5+
export { FIRST_KEY_INDEX, IS_READ_ONLY } from './LCS';
6+
7+
export function transformArguments(
8+
key1: RedisCommandArgument,
9+
key2: RedisCommandArgument
10+
): RedisCommandArguments {
11+
const args = transformLcsArguments(key1, key2);
12+
args.push('IDX', 'WITHMATCHLEN');
13+
return args;
14+
}
15+
16+
type RawReply = [
17+
'matches',
18+
Array<[
19+
key1: RawRangeReply,
20+
key2: RawRangeReply,
21+
length: number
22+
]>,
23+
'len',
24+
number
25+
];
26+
27+
interface Reply {
28+
matches: Array<{
29+
key1: RangeReply;
30+
key2: RangeReply;
31+
length: number;
32+
}>;
33+
length: number;
34+
}
35+
36+
export function transformReply(reply: RawReply): Reply {
37+
return {
38+
matches: reply[1].map(([key1, key2, length]) => ({
39+
key1: transformRangeReply(key1),
40+
key2: transformRangeReply(key2),
41+
length
42+
})),
43+
length: reply[3]
44+
};
45+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { transformArguments } from './LCS_LEN';
4+
5+
describe('LCS_LEN', () => {
6+
testUtils.isVersionGreaterThanHook([7]);
7+
8+
it('transformArguments', () => {
9+
assert.deepEqual(
10+
transformArguments('1', '2'),
11+
['LCS', '1', '2', 'LEN']
12+
);
13+
});
14+
15+
testUtils.testWithClient('client.lcsLen', async client => {
16+
assert.equal(
17+
await client.lcsLen('1', '2'),
18+
0
19+
);
20+
}, GLOBAL.SERVERS.OPEN);
21+
22+
testUtils.testWithCluster('cluster.lcsLen', async cluster => {
23+
assert.equal(
24+
await cluster.lcsLen('{tag}1', '{tag}2'),
25+
0
26+
);
27+
}, GLOBAL.CLUSTERS.OPEN);
28+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { RedisCommandArgument, RedisCommandArguments } from '.';
2+
import { transformArguments as transformLcsArguments } from './LCS';
3+
4+
export { FIRST_KEY_INDEX, IS_READ_ONLY } from './LCS';
5+
6+
export function transformArguments(
7+
key1: RedisCommandArgument,
8+
key2: RedisCommandArgument
9+
): RedisCommandArguments {
10+
const args = transformLcsArguments(key1, key2);
11+
args.push('LEN');
12+
return args;
13+
}
14+
15+
export declare function transformReply(): number;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,20 @@ export function pushSlotRangesArguments(
670670

671671
return args;
672672
}
673+
674+
export type RawRangeReply = [
675+
start: number,
676+
end: number
677+
];
678+
679+
export interface RangeReply {
680+
start: number;
681+
end: number;
682+
}
683+
684+
export function transformRangeReply([start, end]: RawRangeReply): RangeReply {
685+
return {
686+
start,
687+
end
688+
};
689+
}

0 commit comments

Comments
 (0)