1
1
import { RedisCommandArguments } from '.' ;
2
2
3
- type HSETObject = Record < string | number , string | number > ;
3
+ type Types = string | number | Buffer ;
4
4
5
- type HSETMap = Map < string | number , string | number > ;
5
+ type HSETObject = Record < string | number , Types > ;
6
6
7
- type HSETTuples = Array < [ string , string ] > | Array < string > ;
7
+ type HSETMap = Map < Types , Types > ;
8
+
9
+ type HSETTuples = Array < [ Types , Types ] > | Array < Types > ;
8
10
9
11
export const FIRST_KEY_INDEX = 1 ;
10
12
11
- type GenericArguments = [ key : string ] ;
13
+ type GenericArguments = [ key : string | Buffer ] ;
12
14
13
- type SingleFieldArguments = [ ...generic : GenericArguments , field : string , value : string ] ;
15
+ type SingleFieldArguments = [ ...generic : GenericArguments , field : Types , value : Types ] ;
14
16
15
17
type MultipleFieldsArguments = [ ...generic : GenericArguments , value : HSETObject | HSETMap | HSETTuples ] ;
16
18
17
19
export function transformArguments ( ...[ key , value , fieldValue ] : SingleFieldArguments | MultipleFieldsArguments ) : RedisCommandArguments {
18
- const args = [ 'HSET' , key ] ;
20
+ const args : RedisCommandArguments = [ 'HSET' , key ] ;
19
21
20
- if ( typeof value === 'string' ) {
21
- args . push ( value , fieldValue ! ) ;
22
+ if ( typeof value === 'string' || typeof value === 'number' || Buffer . isBuffer ( value ) ) {
23
+ pushValue ( args , value ) ;
24
+ pushValue ( args , fieldValue ! ) ;
22
25
} else if ( value instanceof Map ) {
23
26
pushMap ( args , value ) ;
24
27
} else if ( Array . isArray ( value ) ) {
@@ -30,20 +33,36 @@ export function transformArguments(...[ key, value, fieldValue ]: SingleFieldArg
30
33
return args ;
31
34
}
32
35
33
- function pushMap ( args : Array < string > , map : HSETMap ) : void {
36
+ function pushMap ( args : RedisCommandArguments , map : HSETMap ) : void {
34
37
for ( const [ key , value ] of map . entries ( ) ) {
35
- args . push ( key . toString ( ) , value . toString ( ) ) ;
38
+ pushValue ( args , key ) ;
39
+ pushValue ( args , value ) ;
36
40
}
37
41
}
38
42
39
- function pushTuples ( args : Array < string > , tuples : HSETTuples ) : void {
40
- args . push ( ...tuples . flat ( ) ) ;
43
+ function pushTuples ( args : RedisCommandArguments , tuples : HSETTuples ) : void {
44
+ for ( const tuple of tuples ) {
45
+ if ( Array . isArray ( tuple ) ) {
46
+ pushTuples ( args , tuple ) ;
47
+ continue ;
48
+ }
49
+
50
+ pushValue ( args , tuple ) ;
51
+ }
41
52
}
42
53
43
- function pushObject ( args : Array < string > , object : HSETObject ) : void {
54
+ function pushObject ( args : RedisCommandArguments , object : HSETObject ) : void {
44
55
for ( const key of Object . keys ( object ) ) {
45
56
args . push ( key . toString ( ) , object [ key ] . toString ( ) ) ;
46
57
}
47
58
}
48
59
60
+ function pushValue ( args : RedisCommandArguments , value : Types ) : void {
61
+ args . push (
62
+ typeof value === 'number' ?
63
+ value . toString ( ) :
64
+ value
65
+ ) ;
66
+ }
67
+
49
68
export declare function transformReply ( ) : number ;
0 commit comments