https://github.com/wilsonsilva/yn
Parse yes/no like values
https://github.com/wilsonsilva/yn
cli command-line npm ruby sindresorhus yes-no
Last synced: 2 months ago
JSON representation
Parse yes/no like values
- Host: GitHub
- URL: https://github.com/wilsonsilva/yn
- Owner: wilsonsilva
- License: mit
- Created: 2019-12-01T15:00:50.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-12-01T21:56:54.000Z (over 6 years ago)
- Last Synced: 2025-12-21T12:23:23.253Z (6 months ago)
- Topics: cli, command-line, npm, ruby, sindresorhus, yes-no
- Language: Ruby
- Size: 286 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Yn
[](https://badge.fury.io/rb/yn)
[](https://travis-ci.org/wilsonsilva/yn)
[](https://codeclimate.com/github/wilsonsilva/yn/maintainability)
[](https://codeclimate.com/github/wilsonsilva/yn/test_coverage)
[](https://hakiri.io/github/wilsonsilva/yn/master)
[](http://inch-ci.org/github/wilsonsilva/yn)
> Parse yes/no like values
Useful for validating answers of a CLI prompt.
---
The following case-insensitive values are recognized:
```ruby
'y', 'yes', 'true', true, '1', 1, 'n', 'no', 'false', false, '0', 0, 'on', 'off'
```
*Enable lenient mode to gracefully handle typos.*
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'yn'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install yn
## Usage
```ruby
require 'yn'
Yn.parse('y')
# => true
Yn.parse('NO')
# => false
Yn.parse(true)
# => true
Yn.parse('abomasum')
# => nil
Yn.parse('abomasum', default: false)
# => false
Yn.parse('mo', lenient: true)
# => false
```
Unrecognized values return `nil`.
## API
### Yn.parse(input, options?)
#### input
Type: `unknown`
Value that should be converted.
#### options
Type: `hash`
##### lenient
Type: `boolean`\
Default: `false`
Use a key distance-based score to leniently accept typos of `yes` and `no`.
##### default
Type: `boolean`\
Default: `nil`
Default value if no match was found.
## Development
After checking out the repo, run `bin/setup` to install dependencies, configure git hooks and create support files.
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
The health and maintainability of the codebase is ensured through a set of
Rake tasks to test, lint and audit the gem for security vulnerabilities and documentation:
```
rake bundle:audit # Checks for vulnerable versions of gems
rake qa # Test, lint and perform security and documentation audits
rake rubocop # Lint the codebase with RuboCop
rake rubocop:auto_correct # Auto-correct RuboCop offenses
rake spec # Run RSpec code examples
rake verify_measurements # Verify that yardstick coverage is at least 100%
rake yard # Generate YARD Documentation
rake yard:junk # Check the junk in your YARD Documentation
rake yardstick_measure # Measure docs in lib/**/*.rb with yardstick
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/wilsonsilva/yn.
This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to
adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Credits
This is a port of [Sindre Sorhus](https://github.com/sindresorhus) JavaScript's [yn](https://github.com/sindresorhus/yn).
I'm a big fan of his work!
## Code of Conduct
Everyone interacting in the Yn project’s codebases, issue trackers, chat rooms and mailing lists
is expected to follow the [code of conduct](https://github.com/wilsonsilva/yn/blob/master/CODE_OF_CONDUCT.md).