Skip to content

Commit

Permalink
fix(helpers): add isDate check, enhance isPromise
Browse files Browse the repository at this point in the history
* helpers, isDate, enhance isPromise, async promise-like check
  • Loading branch information
cdcabrera committed Jul 30, 2020
1 parent 611df87 commit 82d823b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/common/__tests__/__snapshots__/helpers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Object {
"UI_WINDOW_ID": "curiosity",
"browserExpose": [Function],
"generateId": [Function],
"isDate": [Function],
"isPromise": [Function],
"lorem": "ipsum",
"noop": [Function],
Expand Down Expand Up @@ -54,6 +55,7 @@ Object {
"UI_WINDOW_ID": "curiosity",
"browserExpose": [Function],
"generateId": [Function],
"isDate": [Function],
"isPromise": [Function],
"lorem": "ipsum",
"noop": [Function],
Expand Down Expand Up @@ -85,6 +87,7 @@ Object {
"UI_WINDOW_ID": "curiosity",
"browserExpose": [Function],
"generateId": [Function],
"isDate": [Function],
"isPromise": [Function],
"noop": [Function],
"noopPromise": Promise {},
Expand Down
8 changes: 8 additions & 0 deletions src/common/__tests__/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ describe('Helpers', () => {
expect(helpers.generateId('lorem')).toBe('lorem-');
});

it('should determine a date', () => {
expect(helpers.isDate(new Date('broken'))).toBe(true);
expect(helpers.isDate(Date)).toBe(false);
expect(helpers.isDate(1)).toBe(false);
expect(helpers.isDate('lorem')).toBe(false);
});

it('should determine a promise', () => {
expect(helpers.isPromise(Promise.resolve())).toBe(true);
expect(helpers.isPromise(async () => {})).toBe(true);
expect(helpers.isPromise(() => 'lorem')).toBe(false);
});

Expand Down
14 changes: 11 additions & 3 deletions src/common/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,21 @@
const generateId = prefix =>
`${prefix || 'generatedid'}-${(process.env.REACT_APP_ENV !== 'test' && Math.ceil(1e5 * Math.random())) || ''}`;

// ToDo: expand to include "async" check in scenarios where async/await are utilized.
/**
* Check if "is a Promise".
* Check if "is a Date"
*
* @param {Date|*} date
* @returns {boolean}
*/
const isDate = date => Object.prototype.toString.call(date) === '[object Date]';

/**
* Check if "is a Promise", "Promise like".
*
* @param {Promise|*} obj
* @returns {boolean}
*/
const isPromise = obj => Object.prototype.toString.call(obj) === '[object Promise]';
const isPromise = obj => /^\[object (Promise|Async|AsyncFunction)]/.test(Object.prototype.toString.call(obj));

/**
* An empty function.
Expand Down Expand Up @@ -210,6 +217,7 @@ const browserExpose = (obj = {}, options) => {
const helpers = {
browserExpose,
generateId,
isDate,
isPromise,
noop,
noopPromise,
Expand Down

0 comments on commit 82d823b

Please sign in to comment.