From 449d9c40ae6581cf6ad61e162e0d7e03e5e2c6d0 Mon Sep 17 00:00:00 2001 From: dovi Date: Tue, 2 May 2023 15:40:00 -0400 Subject: [PATCH] fix XTRIM --- packages/client/lib/commands/XTRIM.spec.ts | 91 +++++++++++----------- packages/client/lib/commands/XTRIM.ts | 50 ++++++------ packages/client/lib/commands/index.ts | 6 +- 3 files changed, 76 insertions(+), 71 deletions(-) diff --git a/packages/client/lib/commands/XTRIM.spec.ts b/packages/client/lib/commands/XTRIM.spec.ts index bcbb1c928c..4c6ea985f1 100644 --- a/packages/client/lib/commands/XTRIM.spec.ts +++ b/packages/client/lib/commands/XTRIM.spec.ts @@ -1,49 +1,52 @@ -// import { strict as assert } from 'assert'; -// import testUtils, { GLOBAL } from '../test-utils'; -// import { transformArguments } from './XTRIM'; +import { strict as assert } from 'assert'; +import testUtils, { GLOBAL } from '../test-utils'; +import XTRIM from './XTRIM'; -// describe('XTRIM', () => { -// describe('transformArguments', () => { -// it('simple', () => { -// assert.deepEqual( -// transformArguments('key', 'MAXLEN', 1), -// ['XTRIM', 'key', 'MAXLEN', '1'] -// ); -// }); +describe('XTRIM', () => { + describe('transformArguments', () => { + it('simple', () => { + assert.deepEqual( + XTRIM.transformArguments('key', 'MAXLEN', 1), + ['XTRIM', 'key', 'MAXLEN', '1'] + ); + }); -// it('with strategyModifier', () => { -// assert.deepEqual( -// transformArguments('key', 'MAXLEN', 1, { -// strategyModifier: '=' -// }), -// ['XTRIM', 'key', 'MAXLEN', '=', '1'] -// ); -// }); + it('with strategyModifier', () => { + assert.deepEqual( + XTRIM.transformArguments('key', 'MAXLEN', 1, { + strategyModifier: '=' + }), + ['XTRIM', 'key', 'MAXLEN', '=', '1'] + ); + }); -// it('with LIMIT', () => { -// assert.deepEqual( -// transformArguments('key', 'MAXLEN', 1, { -// LIMIT: 1 -// }), -// ['XTRIM', 'key', 'MAXLEN', '1', 'LIMIT', '1'] -// ); -// }); + it('with LIMIT', () => { + assert.deepEqual( + XTRIM.transformArguments('key', 'MAXLEN', 1, { + LIMIT: 1 + }), + ['XTRIM', 'key', 'MAXLEN', '1', 'LIMIT', '1'] + ); + }); -// it('with strategyModifier, LIMIT', () => { -// assert.deepEqual( -// transformArguments('key', 'MAXLEN', 1, { -// strategyModifier: '=', -// LIMIT: 1 -// }), -// ['XTRIM', 'key', 'MAXLEN', '=', '1', 'LIMIT', '1'] -// ); -// }); -// }); + it('with strategyModifier, LIMIT', () => { + assert.deepEqual( + XTRIM.transformArguments('key', 'MAXLEN', 1, { + strategyModifier: '=', + LIMIT: 1 + }), + ['XTRIM', 'key', 'MAXLEN', '=', '1', 'LIMIT', '1'] + ); + }); + }); -// testUtils.testWithClient('client.xTrim', async client => { -// assert.equal( -// await client.xTrim('key', 'MAXLEN', 1), -// 0 -// ); -// }, GLOBAL.SERVERS.OPEN); -// }); + testUtils.testAll('xTrim', async client => { + assert.equal( + await client.xTrim('key', 'MAXLEN', 1), + 0 + ); + }, { + client: GLOBAL.SERVERS.OPEN, + cluster: GLOBAL.CLUSTERS.OPEN, + }); +}); diff --git a/packages/client/lib/commands/XTRIM.ts b/packages/client/lib/commands/XTRIM.ts index 780aeb30d7..edbb11f3ea 100644 --- a/packages/client/lib/commands/XTRIM.ts +++ b/packages/client/lib/commands/XTRIM.ts @@ -1,31 +1,31 @@ -// import { RedisCommandArgument, RedisCommandArguments } from '.'; +import { NumberReply, Command, RedisArgument } from '../RESP/types'; -// export const FIRST_KEY_INDEX = 1; +export interface XTrimOptions { + strategyModifier?: '=' | '~'; + LIMIT?: number; +} -// interface XTrimOptions { -// strategyModifier?: '=' | '~'; -// LIMIT?: number; -// } +export default { + FIRST_KEY_INDEX: 1, + IS_READ_ONLY: false, + transformArguments( + key: RedisArgument, + strategy: 'MAXLEN' | 'MINID', + threshold: number, + options?: XTrimOptions) { + const args = ['XTRIM', key, strategy]; -// export function transformArguments( -// key: RedisCommandArgument, -// strategy: 'MAXLEN' | 'MINID', -// threshold: number, -// options?: XTrimOptions -// ): RedisCommandArguments { -// const args = ['XTRIM', key, strategy]; + if (options?.strategyModifier) { + args.push(options.strategyModifier); + } -// if (options?.strategyModifier) { -// args.push(options.strategyModifier); -// } + args.push(threshold.toString()); -// args.push(threshold.toString()); + if (options?.LIMIT) { + args.push('LIMIT', options.LIMIT.toString()); + } -// if (options?.LIMIT) { -// args.push('LIMIT', options.LIMIT.toString()); -// } - -// return args; -// } - -// export declare function transformReply(): number; + return args; + }, + transformReply: undefined as unknown as () => NumberReply +} as const satisfies Command; diff --git a/packages/client/lib/commands/index.ts b/packages/client/lib/commands/index.ts index c5f4e31156..b95d7467a6 100644 --- a/packages/client/lib/commands/index.ts +++ b/packages/client/lib/commands/index.ts @@ -144,7 +144,8 @@ import WATCH from './WATCH'; import XACK from './XACK'; import XADD_NOMKSTREAM from './XADD_NOMKSTREAM'; import XADD from './XADD'; -import XDEL from './XDEL' +import XDEL from './XDEL'; +import XTRIM from './XTRIM'; import XLEN from './XLEN'; import ZADD from './ZADD'; import ZCARD from './ZCARD'; @@ -466,7 +467,8 @@ export default { xAdd: XADD, XDEL, xDel: XDEL, - + XTRIM, + xTrim: XTRIM, XLEN, xLen: XLEN, ZADD,