Skip to content

Commit d65de22

Browse files
committed
Use new version of switch-functional
1 parent 4b982a7 commit d65de22

3 files changed

Lines changed: 7 additions & 23 deletions

File tree

src/condition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import isPlainObj from 'is-plain-obj'
33

44
// Add support for `error.message` and `ErrorClass` condition to
55
// the conditions already supported by `switch-functional`
6-
export const normalizeCondition = (condition) => {
6+
export const mapCondition = (condition) => {
77
if (typeof condition === 'string') {
88
return matchesErrorName.bind(undefined, condition)
99
}

src/effect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import isPlainObj from 'is-plain-obj'
22

33
// Add support for error-specific effects to the ones already supported by
44
// `switch-functional`
5-
export const mapEffects = (ErrorClass, effects) => {
5+
export const mapEffects = (ErrorClass, ...effects) => {
66
validateEffects(effects, ErrorClass)
77
return applyEffects.bind(undefined, effects, ErrorClass)
88
}

src/main.js

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,15 @@
11
import switchFunctional from 'switch-functional'
22

3-
import { normalizeCondition } from './condition.js'
3+
import { mapCondition } from './condition.js'
44
import { mapEffects } from './effect.js'
55

66
// `ErrorClass.switch(value)`
77
// Wrap `switch-functional` to add Error-specific conditions and effects
88
const switchMethod = ({ ErrorClass }, value) =>
9-
customize(normalizeCondition, mapEffects.bind(undefined, ErrorClass))(value)
10-
11-
const customize = (mapConditions, mapReturnValues) => (value) =>
12-
customizeSwitch(switchFunctional(value), mapConditions, mapReturnValues)
13-
14-
const customizeSwitch = (originalSwitch, mapConditions, mapReturnValues) => ({
15-
case: (conditions, ...returnValues) =>
16-
customizeSwitch(
17-
originalSwitch.case(
18-
Array.isArray(conditions)
19-
? conditions.map(mapConditions)
20-
: mapConditions(conditions),
21-
mapReturnValues(returnValues),
22-
),
23-
mapConditions,
24-
mapReturnValues,
25-
),
26-
default: (...returnValues) =>
27-
originalSwitch.default(mapReturnValues(returnValues)),
28-
})
9+
switchFunctional(value, {
10+
mapCondition,
11+
mapReturnValues: mapEffects.bind(undefined, ErrorClass),
12+
})
2913

3014
export default {
3115
name: 'switch',

0 commit comments

Comments
 (0)