From 90c37bd1af8262f3f954a5b3493e978c808a131c Mon Sep 17 00:00:00 2001 From: leibale Date: Sat, 27 Nov 2021 22:58:53 -0500 Subject: [PATCH] fix #1739 - add support for number as value in HSET --- packages/client/lib/commands/SET.spec.ts | 9 +++++++- packages/client/lib/commands/SET.ts | 28 ++++++++++++------------ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/packages/client/lib/commands/SET.spec.ts b/packages/client/lib/commands/SET.spec.ts index 353885a309..f09f8621f2 100644 --- a/packages/client/lib/commands/SET.spec.ts +++ b/packages/client/lib/commands/SET.spec.ts @@ -4,13 +4,20 @@ import { transformArguments } from './SET'; describe('SET', () => { describe('transformArguments', () => { - it('simple', () => { + it('string', () => { assert.deepEqual( transformArguments('key', 'value'), ['SET', 'key', 'value'] ); }); + it('number', () => { + assert.deepEqual( + transformArguments('key', 1), + ['SET', 'key', '1'] + ); + }); + describe('TTL', () => { it('with EX', () => { assert.deepEqual( diff --git a/packages/client/lib/commands/SET.ts b/packages/client/lib/commands/SET.ts index fdc7eef198..7a004e1fb2 100644 --- a/packages/client/lib/commands/SET.ts +++ b/packages/client/lib/commands/SET.ts @@ -24,32 +24,32 @@ interface SetCommonOptions { type SetOptions = SetTTL & SetGuards & SetCommonOptions; -export function transformArguments(key: string | Buffer, value: string | Buffer, options?: SetOptions): RedisCommandArguments { - const args = ['SET', key, value]; +export function transformArguments(key: string | Buffer, value: string | number | Buffer, options?: SetOptions): RedisCommandArguments { + const args = [ + 'SET', + key, + typeof value === 'number' ? value.toString() : value + ]; - if (!options) { - return args; - } - - if (options.EX) { + if (options?.EX) { args.push('EX', options.EX.toString()); - } else if (options.PX) { + } else if (options?.PX) { args.push('PX', options.PX.toString()); - } else if (options.EXAT) { + } else if (options?.EXAT) { args.push('EXAT', options.EXAT.toString()); - } else if (options.PXAT) { + } else if (options?.PXAT) { args.push('PXAT', options.PXAT.toString()); - } else if (options.KEEPTTL) { + } else if (options?.KEEPTTL) { args.push('KEEPTTL'); } - if (options.NX) { + if (options?.NX) { args.push('NX'); - } else if (options.XX) { + } else if (options?.XX) { args.push('XX'); } - if (options.GET) { + if (options?.GET) { args.push('GET'); }