split
Choose one of cases by given conditions. It "splits" source unit into several events, which fires when payload matches their conditions. Works like pattern matching for payload values and external stores
Concepts#
Case mode#
Mode in which target case is selected by name of it's field. Case could be selected from data in source by case function or from external case store which keept current case name. After selection data from source will be sent to corresponding cases[fieldName] (if there is one), if none of the fields matches, then the data will be sent to cases.__ (if there is one)
See also:
Matching mode#
Mode in which each case sequentially matched by stores and functions in fields of match object.
If one of the fields got true from store value or return of function, then the data from source will be sent to corresponding cases[fieldName] (if there is one), if none of the fields matches, then the data will be sent to cases.__ (if there is one)
See also:
Case store#
Store with string which will be used to choose case by it's name. Placed directly in match field
Case function#
String-returning function which will be called with value from source to choose case by it's name. Placed directly in match field, should be pure
Matcher store#
Boolean store which indicates whether to choose particular case or try next one. Placed in fields of match object, might be mixed with matcher functions
Matcher function#
Boolean-returning function which indicates whether to choose particular case or try next one. Placed in fields of match object, might be mixed with matcher stores, should be pure
note
Case store, case funtion and matcher store are supported since effector 21.8.0
split with cases#
Arguments
source: Unit which will trigger computation insplitmatch: Single store with string, single function which returns string or object with boolean stores and functions which returns booleancases: Object with units to which data will be passed fromsourceafter case selection
Returns
since
effector 21.0.0
Example 1#
Example 2#
You can match directly to store api as well:
split without explicit cases#
Arguments
source: Unit which will trigger computation insplitmatch(Object): Schema of cases, which uses names of resulting events as keys, and matching function((value) => Boolean)
Returns
(Object) - Object, having keys, defined in match argument, plus __(two underscores) - which stands for default (no matches met) case.
since
effector 20.0.0
Example 1#
note
Only the first met match will trigger resulting event