GithubHelp home page GithubHelp logo

yn's Introduction

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, 'on', 'off'

Enable lenient mode to gracefully handle typos.

Install

$ npm install yn

Usage

import yn from 'yn';

yn('y');
//=> true

yn('NO');
//=> false

yn(true);
//=> true

yn('abomasum');
//=> undefined

yn('abomasum', {default: false});
//=> false

yn('mo', {lenient: true});
//=> false

Unrecognized values return undefined.

API

yn(input, options?)

input

Type: unknown

The 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: undefined

The default value if no match was found.

yn's People

Contributors

ahuff44 avatar bendingbender avatar haroenv avatar justinwoo avatar leon19 avatar markstos avatar richienb avatar samverschueren avatar sindresorhus avatar sm2017 avatar williamnharvey avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yn's Issues

Override `null` fallback

It's a minor feature, but I'd like to be able to pass an option to set the default null result to something else. This could be handy when you want to fall back on true, e.g. yn(req.query.something, { default: true }).

Typos?

What about supporting typos like "ues" and "mo"?

Question about YN 4.0 Breaking change

I had a question about why this decision was chosen:
Only use a CommonJS export for the TypeScript definition You need to change import yn from 'yn'; to import yn = require('yn');
Is the best practice now to disallow ECMASCRIPT imports as you have done in version 4 by not exposing import?

And what causes the import yn = syntax to work. Shouldn't it be const yn = if it's true common-js?

yn(2)

Why is yn(2) null?

Can't use with require

const yn = require('yn');

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /root/app/node_modules/yn/index.js
require() of ES modules is not supported.

Missing properties in package.json causing errors in monorepo

Error:

  • Package subpath './package.json' is not defined by "exports" in /<app-path>/node_modules/yn/package.json.
  • In addition, author URL value is not a valid URI string. URI with a scheme is expected.

Solution:

Adopt this into package.json

"author": {
    ...
    "url": "https://sindresorhus.com"
},
"exports": {
    "./package.json": "./package.json",
},

Yeah/Yup

Are they valid? Should Yn identify these also?

test(function (t) {
t.assert(yn('yup', {lenient: true}) === true);
t.end();
});

Using Object.assign

Object.assign isn't compatible with IE (surprise surprise)... If you're using yn on the front end might run into some issues where the package wont work

Issue arose on our end from using it in combination with Next js and using publicruntime config import config to the front end to feature switch certain actions and renders.

There are several ways round it (moving yn to the next build config etc etc) but thought I'd let you know!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.