Skip to content

Commit

Permalink
feat: add isFalsy
Browse files Browse the repository at this point in the history
Ref #66
  • Loading branch information
char0n committed Dec 10, 2017
1 parent f09f72e commit 1612941
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,12 @@ declare namespace RamdaAdjunct {
*/
isTruthy(val: any): boolean;

/**
* A falsy value is a value that translates to false when evaluated in a Boolean context.
* Falsy values are `false`, `0`, `""`, `null`, `undefined`, and `NaN`.
*/
isFalsy(val: any): boolean;

/**
* Identity type.
*/
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export { default as isNotPair } from './isNotPair';
export { default as isThenable } from './isThenable';
export { default as isPromise } from './isPromise';
export { default as isTruthy } from './isTruthy';
export { default as isFalsy } from './isFalsy';
// Function
export { default as stubUndefined } from './stubUndefined';
export { default as stubNull } from './stubNull';
Expand Down
29 changes: 29 additions & 0 deletions src/isFalsy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { complement } from 'ramda';

import isTruthy from './isTruthy';


/**
* A falsy value is a value that translates to false when evaluated in a Boolean context.
* Falsy values are `false`, `0`, `""`, `null`, `undefined`, and `NaN`.
*
* @func isFalsy
* @memberOf RA
* @since {@link https://char0n.github.io/ramda-adjunct/2.2.0|v2.2..0}
* @category Type
* @sig * -> Boolean
* @param {*} val The value to test
* @return {Boolean}
* @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Falsy|falsy}, {@link RA.isTruthy|isTruthy}
* @example
*
* RA.isFalsy(false); // => true
* RA.isFalsy(0); // => true
* RA.isFalsy(''); // => true
* RA.isFalsy(null); // => true
* RA.isFalsy(undefined); // => true
* RA.isFalsy(NaN); // => true
*/
const isFalsy = complement(isTruthy);

export default isFalsy;
2 changes: 1 addition & 1 deletion src/isTruthy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { pipe, equals } from 'ramda';
* @sig * -> Boolean
* @param {*} val The value to test
* @return {Boolean}
* @see {@link RA.isFalsy|isFalsy}
* @see {@link https://developer.mozilla.org/en-US/docs/Glossary/Truthy|truthy}, {@link RA.isFalsy|isFalsy}
* @example
*
* RA.isTruthy({}); // => true
Expand Down
31 changes: 31 additions & 0 deletions test/isFalsy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as RA from '../src/index';
import eq from './shared/eq';
import Symbol from './shared/Symbol';
import args from './shared/arguments';


describe('isFalsy', function() {
it('tests a value for `falsy`', function() {
eq(RA.isFalsy(false), true);
eq(RA.isFalsy(0), true);
eq(RA.isFalsy(''), true);
eq(RA.isFalsy(null), true);
eq(RA.isFalsy(undefined), true);
eq(RA.isFalsy(NaN), true);

eq(RA.isFalsy('abc'), false);
eq(RA.isFalsy(Object('abc')), false);
eq(RA.isFalsy(args), false);
eq(RA.isFalsy([1, 2, 3]), false);
eq(RA.isFalsy(new Date()), false);
eq(RA.isFalsy(new Error()), false);
eq(RA.isFalsy(Array.prototype.slice), false);
eq(RA.isFalsy({ 0: 1, length: 1 }), false);
eq(RA.isFalsy(1), false);
eq(RA.isFalsy(/x/), false);
eq(RA.isFalsy(Symbol), RA.isUndefined(Symbol));
eq(RA.isFalsy({}), false);
eq(RA.isFalsy([]), false);
eq(RA.isFalsy(Infinity), false);
});
});

0 comments on commit 1612941

Please sign in to comment.