Chooses and executes a function for each variant in the Validation structure.
('a is Variant).({ 'b: (Object Any) => 'c }) => 'c
where 'b = 'a[`@@folktale:adt:tag]
Chooses and executes a function for each variant in the Validation structure.
const { Success, Failure } = require('folktale/validation');
Success('a').matchWith({
Failure: ({ value }) => `Failure: ${value}`,
Success: ({ value }) => `Success: ${value}`
});
// ==> 'Success: a'
Failure('a').matchWith({
Failure: ({ value }) => `Failure: ${value}`,
Success: ({ value }) => `Success: ${value}`
});
// ==> 'Failure: a'
matchWith(pattern) {
assertObject(`${typeId}'s ${name}#matchWith`, pattern);
if (name in pattern) {
return pattern[name](this);
} else if (ANY in pattern) {
return pattern[ANY]();
} else {
throw new Error(getMatchWithErrorMessage(pattern, name));
}
}