Skip to content

Commit

Permalink
docs: match_chain docs
Browse files Browse the repository at this point in the history
  • Loading branch information
brigand committed Dec 27, 2019
1 parent 220399e commit 4fd351d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
21 changes: 18 additions & 3 deletions README.md
Expand Up @@ -35,16 +35,31 @@ import Eq from 'pattahn/cond/Eq';
import Test from 'pattahn/cond/Test';

const matcher = match()
.with(Eq('foo'), (value) => 'eq foo')
.with(Eq('foo').or(Eq('bar')), (value) => 'eq foo/bar')
.with(
Test((v) => v.length === 0),
'empty',
)
.any((other) => 'other: ' + other);

matcher.exec('foo'); // => 'eq foo'
matcher.exec('foo'); // => 'eq foo/bar'
matcher.exec(''); // => 'empty'
matcher.exec('bar'); // => 'other: bar'
matcher.exec('baz'); // => 'other: baz'
```

You can also match directly by using an object. Conditions can be used with the ES6
dynamic key syntax.

```js
import match from 'pattahn';

const result = match('foo', {
foo: 'eq foo',
[Eq('bar').or(Eq('baz'))]: 'foo or baz',
_: 'default',
});

result; // => 'eq foo'
```

See the [API Documentation] for more details.
Expand Down
7 changes: 7 additions & 0 deletions src/core.js
Expand Up @@ -59,6 +59,13 @@ class Condition {
return And(this, other);
}

/**
* e.g. And(c, d) is c.chain(And, d)
*/
chain(factory, ...args) {
return factory(this, ...args);
}

andThen(other) {
return AndThen(this, other);
}
Expand Down
49 changes: 49 additions & 0 deletions src/match_chain.mdx
@@ -0,0 +1,49 @@
---
name: match (chain)
route: /match/chain
---

# match (chain variant)

When match is called with 0 or 1 argument it returns a chainable instance for defining
patterns to match on.

The main two methods are `.with` to add a branch to the matching, and `.exec` to run
the match operation (which can be called multiple times, if desired).

```js
import match from 'pattahn';

const r1 = match()
.with('foo', 'is foo')
.with('bar', 'is bar')
.exec('foo');

// The value can alternatively be provided to match() as a single argument,
// if preferred for style.
const r2 = match('foo')
.with('foo', 'is foo')
.with('bar', 'is bar')
.exec();

expect(r1).toBe('is foo');
expect(r2).toBe('is foo');
```

The first argument to `.with` is a condition, or a value supported by
[intoCondition](/into-condition).

The second argument is either a literal value, or a function that takes the output of
the condition and returns the value. If the resulting value will be a function, then
you need a function that returns a function.

## Other methods

The `.any` takes the second argument to `.with` and matches on any input (i.e. the
default case for a match). The call `.any(result)` is identical to
`.with(Test(() => true), result)`.

Both `.any` and `.with` return the matcher object to allow chaining.

The `.clone` method takes no arguments and returns a shallow clone of the matcher
(where `.clone().with(...)` won't add a condition to the original).

0 comments on commit 4fd351d

Please sign in to comment.