matchWith

Chooses and executes a function for each variant in the Validation structure.

Signature

matchWith(pattern)
('a is Variant).({ 'b: (Object Any) => 'c }) => 'c
where 'b = 'a[`@@folktale:adt:tag]

Documentation

Chooses and executes a function for each variant in the Validation structure.

Example:

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'

Properties

Source Code

Defined in source/adt/union/union.js at line 81, column 42
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));
        } 
      }
Stability
stable
Licence
MIT
Authors
Copyright
(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS
Authors
  • Quildreen Motta
Maintainers
  • Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)