Skip to content

Commit

Permalink
Require Node.js 8, add TypeScript definition (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
BendingBender authored and sindresorhus committed Mar 11, 2019
1 parent eabf443 commit 53f2dca
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
[*.yml]
indent_style = space
indent_size = 2
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
yarn.lock
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language: node_js
node_js:
- '6'
- '4'
- '10'
- '8'
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Check if a value is an error constructor.
*/
export default function isErrorConstructor(
value: unknown
): value is ErrorConstructor;
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
'use strict';
module.exports = ctor => (ctor === Error || ctor.prototype instanceof Error);

const isErrorConstructor = constructor =>
constructor === Error || constructor.prototype instanceof Error;

module.exports = isErrorConstructor;
module.exports.default = isErrorConstructor;
8 changes: 8 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {expectType} from 'tsd-check';
import isErrorConstructor from '.';

const foo = 'foo';

if (isErrorConstructor(foo)) {
expectType<ErrorConstructor>(foo);
}
71 changes: 35 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,37 @@
{
"name": "is-error-constructor",
"version": "1.0.1",
"description": "Check if a value is an error constructor",
"license": "MIT",
"repository": "sindresorhus/is-error-constructor",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"is",
"error",
"constructor",
"err",
"ctor",
"check",
"detect",
"instanceof"
],
"devDependencies": {
"ava": "*",
"xo": "*"
},
"xo": {
"esnext": true
}
"name": "is-error-constructor",
"version": "1.0.1",
"description": "Check if a value is an error constructor",
"license": "MIT",
"repository": "sindresorhus/is-error-constructor",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd-check"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"is",
"error",
"constructor",
"err",
"ctor",
"check",
"detect",
"instanceof"
],
"devDependencies": {
"ava": "^1.3.1",
"tsd-check": "^0.3.0",
"xo": "^0.24.0"
}
}
26 changes: 13 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from 'assert';
import test from 'ava';
import m from './';
import isErrorConstructor from '.';

class CustomError extends RangeError {
constructor() {
Expand All @@ -9,20 +9,20 @@ class CustomError extends RangeError {
}
}

test(t => {
t.true(m(Error));
t.true(m(RangeError));
t.true(m(SyntaxError));
t.true(m(assert.AssertionError));
t.true(m(CustomError));
t.false(m(() => {}));
t.false(m({}));
t.false(m(''));
t.false(m(new Error()));
t.false(m(new assert.AssertionError({
test('isErrorConstructor', t => {
t.true(isErrorConstructor(Error));
t.true(isErrorConstructor(RangeError));
t.true(isErrorConstructor(SyntaxError));
t.true(isErrorConstructor(assert.AssertionError));
t.true(isErrorConstructor(CustomError));
t.false(isErrorConstructor(() => {}));
t.false(isErrorConstructor({}));
t.false(isErrorConstructor(''));
t.false(isErrorConstructor(new Error()));
t.false(isErrorConstructor(new assert.AssertionError({
actual: '',
expected: '',
operator: ''
})));
t.false(m(new CustomError()));
t.false(isErrorConstructor(new CustomError()));
});

0 comments on commit 53f2dca

Please sign in to comment.