From fe16dc0eae9eea0e1540ea2b885b8760229ddea6 Mon Sep 17 00:00:00 2001 From: Avital Fine <98389525+Avital-Fine@users.noreply.github.com> Date: Thu, 24 Mar 2022 16:00:08 +0100 Subject: [PATCH] Support BIT|BYTE option (#2035) * Support BIT|BYTE option * clean code * clean code * clean code Co-authored-by: leibale --- packages/client/lib/commands/BITCOUNT.spec.ts | 29 ++++++++++++++----- packages/client/lib/commands/BITCOUNT.ts | 5 ++++ packages/client/lib/commands/BITPOS.spec.ts | 9 +++++- packages/client/lib/commands/BITPOS.ts | 7 ++++- 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/packages/client/lib/commands/BITCOUNT.spec.ts b/packages/client/lib/commands/BITCOUNT.spec.ts index 8919957cf9..76e7b03f7c 100644 --- a/packages/client/lib/commands/BITCOUNT.spec.ts +++ b/packages/client/lib/commands/BITCOUNT.spec.ts @@ -11,14 +11,27 @@ describe('BITCOUNT', () => { ); }); - it('with range', () => { - assert.deepEqual( - transformArguments('key', { - start: 0, - end: 1 - }), - ['BITCOUNT', 'key', '0', '1'] - ); + describe('with range', () => { + it('simple', () => { + assert.deepEqual( + transformArguments('key', { + start: 0, + end: 1 + }), + ['BITCOUNT', 'key', '0', '1'] + ); + }); + + it('with mode', () => { + assert.deepEqual( + transformArguments('key', { + start: 0, + end: 1, + mode: 'BIT' + }), + ['BITCOUNT', 'key', '0', '1', 'BIT'] + ); + }); }); }); diff --git a/packages/client/lib/commands/BITCOUNT.ts b/packages/client/lib/commands/BITCOUNT.ts index efbc6f2220..d937af2b5d 100644 --- a/packages/client/lib/commands/BITCOUNT.ts +++ b/packages/client/lib/commands/BITCOUNT.ts @@ -7,6 +7,7 @@ export const IS_READ_ONLY = true; interface BitCountRange { start: number; end: number; + mode?: 'BYTE' | 'BIT'; } export function transformArguments( @@ -20,6 +21,10 @@ export function transformArguments( range.start.toString(), range.end.toString() ); + + if (range?.mode) { + args.push(range.mode); + } } return args; diff --git a/packages/client/lib/commands/BITPOS.spec.ts b/packages/client/lib/commands/BITPOS.spec.ts index 354deea619..2a0758fe5d 100644 --- a/packages/client/lib/commands/BITPOS.spec.ts +++ b/packages/client/lib/commands/BITPOS.spec.ts @@ -18,12 +18,19 @@ describe('BITPOS', () => { ); }); - it('with start, end', () => { + it('with start and end', () => { assert.deepEqual( transformArguments('key', 1, 1, -1), ['BITPOS', 'key', '1', '1', '-1'] ); }); + + it('with start, end and mode', () => { + assert.deepEqual( + transformArguments('key', 1, 1, -1, 'BIT'), + ['BITPOS', 'key', '1', '1', '-1', 'BIT'] + ); + }); }); testUtils.testWithClient('client.bitPos', async client => { diff --git a/packages/client/lib/commands/BITPOS.ts b/packages/client/lib/commands/BITPOS.ts index 2e54b11bc9..a9a035fd9f 100644 --- a/packages/client/lib/commands/BITPOS.ts +++ b/packages/client/lib/commands/BITPOS.ts @@ -9,7 +9,8 @@ export function transformArguments( key: RedisCommandArgument, bit: BitValue, start?: number, - end?: number + end?: number, + mode?: 'BYTE' | 'BIT' ): RedisCommandArguments { const args = ['BITPOS', key, bit.toString()]; @@ -21,6 +22,10 @@ export function transformArguments( args.push(end.toString()); } + if (mode) { + args.push(mode); + } + return args; }