Skip to content

Commit

Permalink
feat: add isValidDate
Browse files Browse the repository at this point in the history
Ref #46
  • Loading branch information
char0n committed May 20, 2017
1 parent df0cef2 commit 3a2f4ad
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ declare namespace RamdaAdjunct {
*/
isDate(val: any): val is Date;

/**
* Checks if value is valid `Date` object.
*/
isValidDate(val: any): val is Date;

/**
* Checks if value is complement of `Date` object
*/
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import isNotObjLike from './isNotObjLike';
import isPlainObj from './isPlainObj';
import isNotPlainObj from './isNotPlainObj';
import isDate from './isDate';
import isValidDate from './isValidDate';
import isNotDate from './isNotDate';
import isNumber from './isNumber';
import isNotNumber from './isNotNumber';
Expand Down Expand Up @@ -89,6 +90,7 @@ export { default as isPlainObject } from './isPlainObj';
export { default as isNotPlainObj } from './isNotPlainObj';
export { default as isNotPlainObject } from './isNotPlainObj'; // alias of isNotPlainObject
export { default as isDate } from './isDate';
export { default as isValidDate} from './isValidDate';
export { default as isNotDate } from './isNotDate';
export { default as isNumber } from './isNumber';
export { default as isNotNumber } from './isNotNumber';
Expand Down Expand Up @@ -157,6 +159,7 @@ const RA = {
isNotPlainObj,
isNotPlainObject: isNotPlainObj,
isDate,
isValidDate,
isNotDate,
isNumber,
isNotNumber,
Expand Down
24 changes: 24 additions & 0 deletions src/isValidDate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { invoker, both, pipe } from 'ramda';

import isDate from './isDate';
import isNotNaN from './isNotNaN';

/**
* Checks if value is valid `Date` object.
*
* @func isValidDate
* @memberOf RA
* @since {@link https://char0n.github.io/ramda-adjunct/1.8.0|v1.8.0}
* @category Type
* @sig * -> Boolean
* @param {*} val The value to test
* @return {Boolean}
* @see {@link RA.isDate|isDate}
* @example
*
* RA.isValidDate(new Date()); //=> true
* RA.isValidDate(new Date('a')); //=> false
*/
const isValidDate = both(isDate, pipe(invoker(0, 'getTime'), isNotNaN));

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

describe('isValidDate', function() {
it('tests a value for a valid `Date`', function() {
eq(RA.isValidDate(new Date()), true);

eq(RA.isValidDate(new Date('a')), false);
eq(RA.isValidDate(Date.now()), false);
eq(RA.isValidDate(args), false);
eq(RA.isValidDate([1, 2, 3]), false);
eq(RA.isValidDate(Object(false)), false);
eq(RA.isValidDate(new Error()), false);
eq(RA.isValidDate(RA), false);
eq(RA.isValidDate(Array.prototype.slice), false);
eq(RA.isValidDate({ a: 1 }), false);
eq(RA.isValidDate(Object(0)), false);
eq(RA.isValidDate(/x/), false);
eq(RA.isValidDate(Object('a')), false);

if (Symbol !== 'undefined') {
eq(RA.isValidDate(Symbol), false);
}

eq(RA.isValidDate(null), false);
eq(RA.isValidDate(undefined), false);
});
});

0 comments on commit 3a2f4ad

Please sign in to comment.