Skip to content

Commit

Permalink
fix #1739 - add support for number as value in HSET
Browse files Browse the repository at this point in the history
  • Loading branch information
leibale committed Nov 28, 2021
1 parent 2d2d58d commit 90c37bd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
9 changes: 8 additions & 1 deletion packages/client/lib/commands/SET.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 15 additions & 15 deletions packages/client/lib/commands/SET.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];

if (!options) {
return args;
}

if (options.EX) {
export function transformArguments(key: string | Buffer, value: string | number | Buffer, options?: SetOptions): RedisCommandArguments {
const args = [
'SET',
key,
typeof value === 'number' ? value.toString() : value

This comment has been minimized.

Copy link
@AnnAngela

AnnAngela Nov 28, 2021

Contributor

@leibale Because basically redis only stores strings at set command, I think it's better to detect the type is string or not. Maybe typeof value !== 'string' ? value.toString() : value better?

This comment has been minimized.

Copy link
@leibale

leibale Nov 28, 2021

Author Collaborator

@AnnAngela Buffers are supported as well..

];

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');
}

Expand Down

0 comments on commit 90c37bd

Please sign in to comment.