What is yn?
The yn package is a simple yet powerful utility for parsing yes/no like values. It can be used to convert various types of user inputs into boolean values. This is particularly useful for handling configurations, environment variables, or any input that requires a boolean interpretation.
What are yn's main functionalities?
Basic parsing
This demonstrates basic parsing of yes/no like values into booleans. It shows how 'y', 'NO', true, and false are interpreted.
const yn = require('yn');
console.log(yn('y')); // true
console.log(yn('NO')); // false
console.log(yn(true)); // true
console.log(yn(false)); // false
Parsing with options
This shows how to use options to control the parsing behavior, such as setting a default value or enabling lenient parsing for more flexible yes values.
const yn = require('yn');
console.log(yn('y', { default: false })); // true
console.log(yn('abracadabra', { default: false })); // false
console.log(yn(null, { default: true })); // true
console.log(yn('yes', { lenient: true })); // true
Handling numeric truthy/falsy values
Demonstrates yn's ability to interpret '1' as true and '0' as false, which is useful for numeric representations of boolean values.
const yn = require('yn');
console.log(yn('1')); // true
console.log(yn('0')); // false
Other packages similar to yn
boolean
Similar to yn, the boolean package is designed to convert various types of values into booleans. However, it focuses more on a broader range of truthy and falsy values without the specific emphasis on yes/no strings.
boolify-string
This package offers functionality similar to yn by converting strings to boolean values. It differs in its approach to parsing and the range of strings it considers to be true or false.
yn
Parse yes/no like values
Useful for validating answers of a CLI prompt.
The following case-insensitive values are recognized:
'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0
Enable lenient mode to gracefully handle typos.
Install
$ npm install --save yn
Usage
const yn = require('yn');
yn('y');
yn('NO');
yn(true);
yn('abomasum');
yn('abomasum', {default: false});
yn('mo', {lenient: true});
Unrecognized values return null
.
API
yn(input, [options])
input
Type: any
Value that should be converted.
options
Type: object
lenient
Type: boolean
Default: false
Use a key distance-based score to leniently accept typos of yes
and no
.
default
Type: boolean
Default: null
Default value if no match was found.
License
MIT © Sindre Sorhus