blob: e2282d113b83832b9bdba99e0f8494ea4adb5a66 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# state-toggle [![Build Status][travis-badge]][travis] [![Coverage Status][codecov-badge]][codecov]
<!--lint disable heading-increment no-duplicate-headings-->
Enter/exit a state.
## Installation
[npm][npm-install]:
```bash
npm install state-toggle
```
## Usage
```javascript
var toggle = require('state-toggle');
var ctx = {on: false};
var enter = toggle('on', ctx.on, ctx);
var exit;
// Entering:
exit = enter();
console.log(ctx.on); // true
// Exiting:
exit();
console.log(ctx.on); // false
```
## API
### `toggle(key, initial[, ctx])`
Create a toggle, which when entering toggles `key` on `ctx` (or `this`,
if `ctx` is not given) to `!initial`, and when exiting, sets `key` on
the context back to the value it had before entering.
###### Returns
`Function` — [`enter`][enter].
### `enter()`
Enter the state.
###### Context
If no `ctx` was given to `toggle`, the context object (`this`) of `enter()`
is used to toggle.
###### Returns
`Function` — [`exit`][exit].
### `exit()`
Exit the state, reverting `key` to the value it had before entering.
## License
[MIT][license] © [Titus Wormer][author]
<!-- Definitions -->
[travis-badge]: https://img.shields.io/travis/wooorm/state-toggle.svg
[travis]: https://travis-ci.org/wooorm/state-toggle
[codecov-badge]: https://img.shields.io/codecov/c/github/wooorm/state-toggle.svg
[codecov]: https://codecov.io/github/wooorm/state-toggle
[npm-install]: https://docs.npmjs.com/cli/install
[license]: LICENSE
[author]: http://wooorm.com
[enter]: #enter
[exit]: #exit
|