UNPKG

1.05 kBMarkdownView Raw
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
5Useful for validating answers of a CLI prompt.
6
7---
8
9The 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
28const yn = require('yn');
29
30yn('y');
31//=> true
32
33yn('NO');
34//=> false
35
36yn(true);
37//=> true
38
39yn('abomasum');
40//=> undefined
41
42yn('abomasum', {default: false});
43//=> false
44
45yn('mo', {lenient: true});
46//=> false
47```
48
49Unrecognized values return `undefined`.
50
51
52## API
53
54### yn(input, options?)
55
56#### input
57
58Type: `unknown`
59
60Value that should be converted.
61
62#### options
63
64Type: `object`
65
66##### lenient
67
68Type: `boolean`\
69Default: `false`
70
71Use a key distance-based score to leniently accept typos of `yes` and `no`.
72
73##### default
74
75Type: `boolean`\
76Default: `undefined`
77
78Default value if no match was found.