Giter Site home page Giter Site logo

robloach / falsey Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jonschlinkert/falsey

0.0 3.0 0.0 151 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%

falsey's Introduction

falsey NPM version Build Status

Returns true if value is falsey. Works for strings, arrays and arguments objects with a length of 0, and objects with no own enumerable properties are considered falsey.

Install with npm

npm i falsey --save

What makes this lib unique is the option to pass an array of values that should always evuate as "falsey". For example, you might want to allow users to define nil or nope to disable something.

Usage

var isFalsey = require('falsey');
console.log(isFalsey('');
//=> `true`

Examples

All of the following return true

isFalsey(undefined);
isFalsey(null);
isFalsey(false);
!isFalsey(true);
isFalsey(0);
!isFalsey(1);
isFalsey('');
!isFalsey('1');
isFalsey(NaN);
isFalsey({});
!isFalsey({a: 'b'});
isFalsey([]);
!isFalsey([0]);

Special cases

There are only four "special cases", ['false', 'none', 'nil', 'null], and these are easily overridden by passing a value as a second parameter, e.g.

isFalsey(foo, []);

// other examples...
isFalsey('false');
!isFalsey('false', []); // override special cases
!isFalsey('true');
isFalsey('nil');
!isFalsey('nil', []); // override special cases
isFalsey('none');
!isFalsey('none', []); // override special cases
isFalsey('null');
!isFalsey('null', []); // override special cases

Or, pass an array of values that should return true when evaluated as falsey:

isFalsey(foo, ['no', 'nope', 'nada', 'zilch']);
// you get the point

Related

  • is-primitive: Returns true if the value is a primitive.
  • is-number: Returns true if the value is a number. comprehensive tests.
  • isobject: Returns true if the value is an object and not an array or null.
  • kind-of: Get the native type of a value.

Contributing

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

Running tests

Install dev dependencies.

npm i -d && npm test

Author

Jon Schlinkert

License

Copyright (c) 2015 Jon Schlinkert
Released under the MIT license


This file was generated by verb-cli on March 07, 2015.

falsey's People

Contributors

jonschlinkert avatar

Watchers

Rob Loach avatar James Cloos avatar  avatar

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.