Skip to content

Commit 5df06dc

Browse files
Use switch statements when handling ops (#18)
* Use switch statements when handling ops * Change in to of * Add test for ordering of actions * Simplify ordering test * Update with changes from main --------- Signed-off-by: Anthony Fuller <24512050+AnthonyFuller@users.noreply.github.com>
1 parent feda0cd commit 5df06dc

3 files changed

Lines changed: 94 additions & 63 deletions

File tree

src/index.ts

Lines changed: 63 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/handleEvent.data.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,5 +216,22 @@
216216
"Event_metricvalue": "Painting_A"
217217
}
218218
}
219+
},
220+
"ordering": {
221+
"Definition": {
222+
"Context": {
223+
"Var1": 1,
224+
"Var2": 10
225+
},
226+
"States": {
227+
"Start": {
228+
"-": {
229+
"$inc": "Var1",
230+
"$mul": ["Var2", "$.Var1"],
231+
"$dec": "Var1"
232+
}
233+
}
234+
}
235+
}
219236
}
220237
}

tests/handleEvent.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,18 @@ describe("handleEvent api", () => {
192192

193193
assert.strictEqual(result.state, "Success", "Transition did not happen")
194194
})
195+
196+
it("can perform actions respecting the order of them", () => {
197+
const { Definition } = suites.ordering
198+
const context: Required<typeof Definition.Context> = Definition.Context
199+
200+
const result = handleEvent(Definition, context, null, {
201+
currentState: "Start",
202+
eventName: "-",
203+
timestamp: 0,
204+
})
205+
206+
assert.strictEqual(result.context.Var1, 1)
207+
assert.strictEqual(result.context.Var2, 20)
208+
})
195209
})

0 commit comments

Comments
 (0)