@@ -863,8 +863,16 @@ RedisClient.prototype.end = function () {
863863function Multi ( client , args ) {
864864 this . _client = client ;
865865 this . queue = [ [ "multi" ] ] ;
866+ var command , tmp_args ;
866867 if ( Array . isArray ( args ) ) {
867- this . queue = this . queue . concat ( args ) ;
868+ while ( tmp_args = args . shift ( ) ) {
869+ command = tmp_args . shift ( ) ;
870+ if ( Array . isArray ( command ) ) {
871+ this [ command [ 0 ] ] . apply ( this , command . slice ( 1 ) . concat ( tmp_args ) ) ;
872+ } else {
873+ this [ command ] . apply ( this , tmp_args ) ;
874+ }
875+ }
868876 }
869877}
870878
@@ -878,16 +886,32 @@ commands.forEach(function (fullCommand) {
878886 return ;
879887 }
880888
881- RedisClient . prototype [ command ] = function ( args , callback ) {
882- if ( Array . isArray ( args ) ) {
883- return this . send_command ( command , args , callback ) ;
889+ RedisClient . prototype [ command ] = function ( key , arg , callback ) {
890+ if ( Array . isArray ( key ) ) {
891+ return this . send_command ( command , key , arg ) ;
892+ }
893+ if ( Array . isArray ( arg ) ) {
894+ arg . unshift ( key ) ;
895+ return this . send_command ( command , arg , callback ) ;
884896 }
885897 return this . send_command ( command , to_array ( arguments ) ) ;
886898 } ;
887899 RedisClient . prototype [ command . toUpperCase ( ) ] = RedisClient . prototype [ command ] ;
888900
889- Multi . prototype [ command ] = function ( ) {
890- this . queue . push ( [ command ] . concat ( to_array ( arguments ) ) ) ;
901+ Multi . prototype [ command ] = function ( key , arg , callback ) {
902+ if ( Array . isArray ( key ) ) {
903+ if ( arg ) {
904+ key . push ( arg ) ;
905+ }
906+ this . queue . push ( [ command ] . concat ( key ) ) ;
907+ } else if ( Array . isArray ( arg ) ) {
908+ if ( callback ) {
909+ arg . push ( callback ) ;
910+ }
911+ this . queue . push ( [ command , key ] . concat ( arg ) ) ;
912+ } else {
913+ this . queue . push ( [ command ] . concat ( to_array ( arguments ) ) ) ;
914+ }
891915 return this ;
892916 } ;
893917 Multi . prototype [ command . toUpperCase ( ) ] = Multi . prototype [ command ] ;
@@ -919,70 +943,63 @@ RedisClient.prototype.auth = RedisClient.prototype.AUTH = function (pass, callba
919943 }
920944} ;
921945
922- RedisClient . prototype . hmget = RedisClient . prototype . HMGET = function ( arg1 , arg2 , arg3 ) {
923- if ( Array . isArray ( arg2 ) && typeof arg3 === "function" ) {
924- return this . send_command ( "hmget" , [ arg1 ] . concat ( arg2 ) , arg3 ) ;
925- } else if ( Array . isArray ( arg1 ) && typeof arg2 === "function" ) {
926- return this . send_command ( "hmget" , arg1 , arg2 ) ;
927- } else {
928- return this . send_command ( "hmget" , to_array ( arguments ) ) ;
946+ RedisClient . prototype . hmset = RedisClient . prototype . HMSET = function ( key , args , callback ) {
947+ var field , tmp_args ;
948+ if ( Array . isArray ( key ) ) {
949+ return this . send_command ( "hmset" , key , args ) ;
929950 }
930- } ;
931-
932- RedisClient . prototype . hmset = RedisClient . prototype . HMSET = function ( args , callback ) {
933- var tmp_args , tmp_keys , i , il , key ;
934-
935951 if ( Array . isArray ( args ) ) {
936- return this . send_command ( "hmset" , args , callback ) ;
937- }
938-
939- args = to_array ( arguments ) ;
940- if ( typeof args [ args . length - 1 ] === "function" ) {
941- callback = args [ args . length - 1 ] ;
942- args . length -= 1 ;
943- } else {
944- callback = null ;
952+ return this . send_command ( "hmset" , [ key ] . concat ( args ) , callback ) ;
945953 }
946-
947- if ( args . length === 2 && ( typeof args [ 0 ] === "string" || typeof args [ 0 ] === "number" ) && typeof args [ 1 ] === "object" ) {
954+ if ( typeof args === "object" ) {
948955 // User does: client.hmset(key, {key1: val1, key2: val2})
949956 // assuming key is a string, i.e. email address
950957
951958 // if key is a number, i.e. timestamp, convert to string
952- if ( typeof args [ 0 ] === "number" ) {
953- args [ 0 ] = args [ 0 ] . toString ( ) ;
959+ // TODO: This seems random and no other command get's the key converted => either all or none should behave like this
960+ if ( typeof key !== "string" ) {
961+ key = key . toString ( ) ;
954962 }
955-
956- tmp_args = [ args [ 0 ] ] ;
957- tmp_keys = Object . keys ( args [ 1 ] ) ;
958- for ( i = 0 , il = tmp_keys . length ; i < il ; i ++ ) {
959- key = tmp_keys [ i ] ;
960- tmp_args . push ( key ) ;
961- tmp_args . push ( args [ 1 ] [ key ] ) ;
963+ tmp_args = [ key ] ;
964+ var fields = Object . keys ( args ) ;
965+ while ( field = fields . shift ( ) ) {
966+ tmp_args . push ( field , args [ field ] ) ;
962967 }
963- args = tmp_args ;
968+ return this . send_command ( "hmset" , tmp_args , callback ) ;
964969 }
965-
966- return this . send_command ( "hmset" , args , callback ) ;
970+ return this . send_command ( "hmset" , to_array ( arguments ) ) ;
967971} ;
968972
969- Multi . prototype . hmset = Multi . prototype . HMSET = function ( ) {
970- var args = to_array ( arguments ) , tmp_args ;
971- if ( args . length >= 2 && typeof args [ 0 ] === "string" && typeof args [ 1 ] === "object" ) {
972- tmp_args = [ "hmset" , args [ 0 ] ] ;
973- Object . keys ( args [ 1 ] ) . map ( function ( key ) {
974- tmp_args . push ( key ) ;
975- tmp_args . push ( args [ 1 ] [ key ] ) ;
976- } ) ;
977- if ( args [ 2 ] ) {
978- tmp_args . push ( args [ 2 ] ) ;
973+ Multi . prototype . hmset = Multi . prototype . HMSET = function ( key , args , callback ) {
974+ var tmp_args , field ;
975+ if ( Array . isArray ( key ) ) {
976+ if ( args ) {
977+ key . push ( args ) ;
978+ }
979+ tmp_args = [ 'hmset' ] . concat ( key ) ;
980+ } else if ( Array . isArray ( args ) ) {
981+ if ( callback ) {
982+ args . push ( callback ) ;
983+ }
984+ tmp_args = [ 'hmset' , key ] . concat ( args ) ;
985+ } else if ( typeof args === "object" ) {
986+ tmp_args = [ "hmset" , key ] ;
987+ if ( typeof key !== "string" ) {
988+ key = key . toString ( ) ;
989+ }
990+ var fields = Object . keys ( args ) ;
991+ while ( field = fields . shift ( ) ) {
992+ tmp_args . push ( field ) ;
993+ tmp_args . push ( args [ field ] ) ;
994+ }
995+ if ( callback ) {
996+ tmp_args . push ( callback ) ;
979997 }
980- args = tmp_args ;
981998 } else {
982- args . unshift ( "hmset" ) ;
999+ tmp_args = to_array ( arguments ) ;
1000+ tmp_args . unshift ( "hmset" ) ;
9831001 }
984-
985- this . queue . push ( args ) ;
1002+ this . queue . push ( tmp_args ) ;
9861003 return this ;
9871004} ;
9881005
0 commit comments