Skip to content

Commit

Permalink
Added is.error method
Browse files Browse the repository at this point in the history
  • Loading branch information
normancarcamo committed Feb 6, 2020
1 parent b351d22 commit 8618706
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,23 @@ class Is {
if (this.function(value)) return 'function';
return 'unknown';
}

error(value?: any): boolean {
if (Object.prototype.toString.call(value) === '[object Error]') {
return true;
}

if (value && value.stack && value.message) {
if (
typeof value.stack === 'string' &&
typeof value.message === 'string'
) {
return true;
}
}

return false;
}
}

export default new Is();
37 changes: 37 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,4 +996,41 @@ describe('is', () => {
expect(is.between(true, false, [])).toBe(false);
});
});

describe('error', () => {
it('should be a function', () => {
expect(is.error).toBeFunction();
});
it('should be true when is a valid error', () => {
expect(is.error(new Error('Ouch!'))).toBe(true);
expect(is.error({ stack: 'aa', message: 'sss' })).toBe(true);
});
it('should be false when is not a valid error', () => {
[
'1.3d',
'nw-1',
-1.3,
-0.3,
'1.3',
'0.3',
'43',
'-1',
'-1.3',
'-43',
'65536',
Date.now(),
null,
undefined,
{},
[],
true,
false,
() => {},
function() {},
65536,
'2010-02-03',
'2010-02-03 12:23:21',
].forEach(value => expect(is.error(value)).toBe(false));
});
});
});

0 comments on commit 8618706

Please sign in to comment.