1 | # yn [![Build Status](https://travis-ci.org/sindresorhus/yn.svg?branch=master)](https://travis-ci.org/sindresorhus/yn)
|
2 |
|
3 | > Parse yes/no like values
|
4 |
|
5 | Useful for validating answers of a CLI prompt.
|
6 |
|
7 | ---
|
8 |
|
9 | The following case-insensitive values are recognized:
|
10 |
|
11 | ```js
|
12 | 'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0, 'on', 'off'
|
13 | ```
|
14 |
|
15 | *Enable lenient mode to gracefully handle typos.*
|
16 |
|
17 |
|
18 | ## Install
|
19 |
|
20 | ```
|
21 | $ npm install yn
|
22 | ```
|
23 |
|
24 |
|
25 | ## Usage
|
26 |
|
27 | ```js
|
28 | const yn = require('yn');
|
29 |
|
30 | yn('y');
|
31 | //=> true
|
32 |
|
33 | yn('NO');
|
34 | //=> false
|
35 |
|
36 | yn(true);
|
37 | //=> true
|
38 |
|
39 | yn('abomasum');
|
40 | //=> undefined
|
41 |
|
42 | yn('abomasum', {default: false});
|
43 | //=> false
|
44 |
|
45 | yn('mo', {lenient: true});
|
46 | //=> false
|
47 | ```
|
48 |
|
49 | Unrecognized values return `undefined`.
|
50 |
|
51 |
|
52 | ## API
|
53 |
|
54 | ### yn(input, options?)
|
55 |
|
56 | #### input
|
57 |
|
58 | Type: `unknown`
|
59 |
|
60 | Value that should be converted.
|
61 |
|
62 | #### options
|
63 |
|
64 | Type: `object`
|
65 |
|
66 | ##### lenient
|
67 |
|
68 | Type: `boolean`\
|
69 | Default: `false`
|
70 |
|
71 | Use a key distance-based score to leniently accept typos of `yes` and `no`.
|
72 |
|
73 | ##### default
|
74 |
|
75 | Type: `boolean`\
|
76 | Default: `undefined`
|
77 |
|
78 | Default value if no match was found.
|