Giter Site home page Giter Site logo

jonschlinkert / falsey Goto Github PK

View Code? Open in Web Editor NEW
7.0 4.0 6.0 25 KB

Like other checkers, returns true if `value` is falsey. Strings, arrays and `arguments` objects with a length of `0`, and objects with no own enumerable properties are considered falsey. Will also check against user-defined "special cases".

Home Page: https://github.com/jonschlinkert

License: MIT License

JavaScript 100.00%
false falsey javascript

falsey's Introduction

falsey NPM version NPM monthly downloads NPM total downloads Linux Build Status Windows Build Status

Returns true if the given is falsey (according to JavaScript) or matches a 'falsey' keyword.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install

Install with npm:

$ npm install --save falsey

What is this?

This is intended to be a fun way of accepting alternatives to false or "no" in CLI prompts, web forms, etc. For example, you might want to allow users to define nil or nope to disable something.

Usage

const falsey = require('falsey');

console.log(falsey());       //=> true
console.log(falsey(false));  //=> true
console.log(falsey('nil'));  //=> true
console.log(falsey('nope')); //=> true
console.log(falsey('yes'));  //=> false

Examples

Any value that is not falsey (according to JavaScript) and is not in the list of falsey keywords will return false:

falsey('abc');
falsey(true);
falsey(1);
falsey('1');
falsey({});
falsey([]);

Any value that is falsey (according to JavaScript) or is in the list of falsey keywords will return true:

falsey();          //=> true
falsey('');        //=> true
falsey(0);         //=> true
falsey(false);     //=> true
falsey(NaN);       //=> true
falsey(null);      //=> true
falsey(undefined); //=> true
falsey(void 0);    //=> true

Falsey keywords

If a value matches one of the built-in "falsey" keywords (all strings) it will return true:

  • 0
  • false
  • nada
  • nil
  • nay
  • nah
  • negative
  • no
  • none
  • nope
  • nul
  • null
  • nix
  • nyet
  • uh-uh
  • veto
  • zero

Customize falsey keywords

Pass an array of custom keywords that should return true when evaluated as falsey:

falsey('zilch', ['no', 'nope', 'nada', 'zilch']); //=> true

Disable built-in keywords by passing an empty array:

falsey('nil', []); //=> false

Extend built-in keywords

Built-in keywords are exposed on the .keywords property so that you may extend them with your own keywords:

falsey('zilch', falsey.keywords.concat(['zilch'])); //=> true

Release history

v1.0

Breaking changes

  • objects will now always returns false
  • more words were added to the built-in list of falsey keywords

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

Contributors

Commits Contributor
12 jonschlinkert
9 doowb
3 jesstelford

Author

Jon Schlinkert

License

Copyright © 2018, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on March 18, 2018.

falsey's People

Contributors

doowb avatar jesstelford avatar jonschlinkert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

falsey's Issues

No falsey non empty strings checking

Right know isFalse(' ') returns false, IMHO that's wrong, as falsey might be used for simple data validation, this may be kind of unwated behaviour, opinions?

CLI

Easy way to find if something is falsey through through command line... Exits with error when not falsey.

undefined string as falsey by default

Noticed it is not falsey by default, I think it makes sense and would save some time in different projects. So proposing to add that too, what do you think?

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.