@@ -308,11 +308,6 @@ export function handleActions<Context>(
308308 return context
309309 }
310310
311- const has = ( key : string ) =>
312- Object . prototype . hasOwnProperty . call ( input , key )
313-
314- // TODO: Refactor this into a switch statement using the object keys instead of hasOwn.
315-
316311 const addOrDec = ( op : string ) => {
317312 if ( typeof input [ op ] === "string" ) {
318313 const variableValue = findNamedChild ( input [ op ] , context , true )
@@ -340,33 +335,6 @@ export function handleActions<Context>(
340335 }
341336 }
342337
343- if ( has ( "$inc" ) ) {
344- addOrDec ( "$inc" )
345- }
346-
347- if ( has ( "$dec" ) ) {
348- addOrDec ( "$dec" )
349- }
350-
351- if ( has ( "$mul" ) ) {
352- // $mul can have 2 or 3 operands, 2 means multiply the context variable (1st operand) by the 2nd operand
353- let reference = input [ "$mul" ] [ input [ "$mul" ] . length === 3 ? 2 : 0 ]
354-
355- // Therefore the 1st operand might get written to, but the 2nd one is purely a read.
356- const variableValue1 = findNamedChild ( input [ "$mul" ] [ 0 ] , context , true )
357- const variableValue2 = findNamedChild ( input [ "$mul" ] [ 1 ] , context , false )
358-
359- set ( context , reference , variableValue1 * variableValue2 )
360- }
361-
362- if ( has ( "$set" ) ) {
363- let reference = input . $set [ 0 ]
364-
365- const value = findNamedChild ( input . $set [ 1 ] , context , false )
366-
367- set ( context , reference , value )
368- }
369-
370338 const push = ( unique : boolean ) : void => {
371339 const op = unique ? "$pushunique" : "$push"
372340 let reference = input [ op ] [ 0 ]
@@ -393,38 +361,70 @@ export function handleActions<Context>(
393361 set ( context , reference , array )
394362 }
395363
396- if ( has ( "$push" ) ) {
397- push ( false )
398- }
399-
400- if ( has ( "$pushunique" ) ) {
401- push ( true )
402- }
403-
404- if ( has ( "$remove" ) ) {
405- let reference = input . $remove [ 0 ]
406-
407- if ( reference . startsWith ( "$" ) ) {
408- reference = reference . substring ( 1 )
364+ for ( const key of Object . keys ( input ) ) {
365+ switch ( key ) {
366+ case "$inc" : {
367+ addOrDec ( "$inc" )
368+ break
369+ }
370+ case "$dec" : {
371+ addOrDec ( "$dec" )
372+ break
373+ }
374+ case "$mul" : {
375+ // $mul can have 2 or 3 operands, 2 means multiply the context variable (1st operand) by the 2nd operand
376+ let reference = input [ "$mul" ] [ input [ "$mul" ] . length === 3 ? 2 : 0 ]
377+
378+ // Therefore the 1st operand might get written to, but the 2nd one is purely a read.
379+ const variableValue1 = findNamedChild ( input [ "$mul" ] [ 0 ] , context , true )
380+ const variableValue2 = findNamedChild ( input [ "$mul" ] [ 1 ] , context , false )
381+
382+ set ( context , reference , variableValue1 * variableValue2 )
383+ break
384+ }
385+ case "$set" : {
386+ let reference = input . $set [ 0 ]
387+
388+ const value = findNamedChild ( input . $set [ 1 ] , context , false )
389+
390+ set ( context , reference , value )
391+ break
392+ }
393+ case "$push" : {
394+ push ( false )
395+ break
396+ }
397+ case "$pushunique" : {
398+ push ( true )
399+ break
400+ }
401+ case "$remove" : {
402+ let reference = input . $remove [ 0 ]
403+
404+ if ( reference . startsWith ( "$" ) ) {
405+ reference = reference . substring ( 1 )
406+ }
407+
408+ const value = findNamedChild ( input . $remove [ 1 ] , context , false )
409+
410+ // clone the thing
411+ let array : unknown [ ] = deepClone (
412+ findNamedChild ( reference , context , true )
413+ )
414+
415+ array = array . filter ( ( item ) => item !== value )
416+
417+ set ( context , reference , array )
418+ break
419+ }
420+ case "$reset" : {
421+ let reference = input . $reset
422+ const value = findNamedChild ( reference , options . originalContext , true )
423+
424+ set ( context , reference , value )
425+ break
426+ }
409427 }
410-
411- const value = findNamedChild ( input . $remove [ 1 ] , context , false )
412-
413- // clone the thing
414- let array : unknown [ ] = deepClone (
415- findNamedChild ( reference , context , true )
416- )
417-
418- array = array . filter ( ( item ) => item !== value )
419-
420- set ( context , reference , array )
421- }
422-
423- if ( has ( "$reset" ) ) {
424- let reference = input . $reset
425- const value = findNamedChild ( reference , options . originalContext , true )
426-
427- set ( context , reference , value )
428428 }
429429
430430 return context
0 commit comments