Skip to content

Commit

Permalink
feat: add isTruthy
Browse files Browse the repository at this point in the history
Ref #66
  • Loading branch information
char0n committed Dec 9, 2017
1 parent 1c332e4 commit 3b45079
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,13 @@ declare namespace RamdaAdjunct {
*/
isPromise(val: any): val is Promise<any>;

/**
* In JavaScript, a `truthy` value is a value that is considered true
* when evaluated in a Boolean context. All values are truthy unless
* they are defined as falsy (i.e., except for `false`, `0`, `""`, `null`, `undefined`, and `NaN`).
*/
isTruthy(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 @@ -59,6 +59,7 @@ export { default as isPair } from './isPair';
export { default as isNotPair } from './isNotPair';
export { default as isThenable } from './isThenable';
export { default as isPromise } from './isPromise';
export { default as isTruthy } from './isTruthy';
// Function
export { default as stubUndefined } from './stubUndefined';
export { default as stubNull } from './stubNull';
Expand Down
29 changes: 29 additions & 0 deletions src/isTruthy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { pipe, equals } from 'ramda';


/**
* In JavaScript, a `truthy` value is a value that is considered true
* when evaluated in a Boolean context. All values are truthy unless
* they are defined as falsy (i.e., except for `false`, `0`, `""`, `null`, `undefined`, and `NaN`).
*
* @func isTruthy
* @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 RA.isFalsy|isFalsy}
* @example
*
* RA.isTruthy({}); // => true
* RA.isTruthy([]); // => true
* RA.isTruthy(42); // => true
* RA.isTruthy(3.14); // => true
* RA.isTruthy('foo'); // => true
* RA.isTruthy(new Date()); // => true
* RA.isTruthy(Infinity); // => true
*/
const isTruthy = pipe(Boolean, equals(true));

export default isTruthy;
32 changes: 32 additions & 0 deletions test/isTruthy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import * as RA from '../src/index';
import eq from './shared/eq';
import Symbol from './shared/Symbol';
import args from './shared/arguments';


describe('isTruthy', function() {
it('tests a value for `truthy`', function() {
eq(RA.isTruthy('abc'), true);
eq(RA.isTruthy(Object('abc')), true);
eq(RA.isTruthy(args), true);
eq(RA.isTruthy([1, 2, 3]), true);
eq(RA.isTruthy(true), true);
eq(RA.isTruthy(new Date()), true);
eq(RA.isTruthy(new Error()), true);
eq(RA.isTruthy(Array.prototype.slice), true);
eq(RA.isTruthy({ 0: 1, length: 1 }), true);
eq(RA.isTruthy(1), true);
eq(RA.isTruthy(/x/), true);
eq(RA.isTruthy(Symbol), true);
eq(RA.isTruthy({}), true);
eq(RA.isTruthy([]), true);
eq(RA.isTruthy(Infinity), true);

eq(RA.isTruthy(false), false);
eq(RA.isTruthy(0), false);
eq(RA.isTruthy(''), false);
eq(RA.isTruthy(null), false);
eq(RA.isTruthy(undefined), false);
eq(RA.isTruthy(NaN), false);
});
});

0 comments on commit 3b45079

Please sign in to comment.