error-class
Advanced tools
Comparing version 1.1.0 to 1.1.1
23
index.js
@@ -5,6 +5,3 @@ // Memoize these results, they're basically invariants. | ||
// Invalid JavaScript identifiers. | ||
var invalidName = /[^0-9a-zA-Z_$]/ | ||
module.exports = function errorClass (name, fn) { | ||
@@ -14,8 +11,5 @@ if (!name || typeof name !== 'string') | ||
if (invalidName.test(name)) | ||
throw Error('Argument "name" is an invalid identifier.') | ||
// This is basically `eval`, there's no other way to dynamically define a | ||
// function name. | ||
var error = Function('setupError', 'fn', | ||
var ErrorClass = Function('setupError', 'fn', | ||
'return function ' + name + ' () { ' + | ||
@@ -29,11 +23,12 @@ 'if (!(this instanceof ' + name + ')) ' + | ||
error.prototype = Object.create(Error.prototype, { | ||
constructor: nonEnumerableProperty(error), | ||
ErrorClass.prototype = Object.create(Error.prototype, { | ||
constructor: nonEnumerableProperty(ErrorClass), | ||
name: nonEnumerableProperty(name) | ||
}) | ||
if (hasSetPrototypeOf) Object.setPrototypeOf(error, Error) | ||
else error.__proto__ = Error | ||
// The `setPrototypeOf` method is part of ES6. | ||
if (hasSetPrototypeOf) Object.setPrototypeOf(ErrorClass, Error) | ||
else ErrorClass.__proto__ = Error | ||
return error | ||
return ErrorClass | ||
} | ||
@@ -52,2 +47,4 @@ | ||
// Use the `+` operator with an empty string to implicitly type cast the | ||
// `message` argument into a string. | ||
Object.defineProperty(this, 'message', | ||
@@ -59,5 +56,5 @@ nonEnumerableProperty(message !== undefined ? '' + message : '')) | ||
function nonEnumerableProperty (value) { | ||
// The field `enumerable` is `false` by default. | ||
return { | ||
value: value, | ||
enumerable: false, | ||
writable: true, | ||
@@ -64,0 +61,0 @@ configurable: true |
{ | ||
"name": "error-class", | ||
"description": "Typed errors that closely emulate the native Error class.", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -19,2 +19,4 @@ # Error Class | ||
```js | ||
import errorClass from 'error-class' | ||
const SpecialError = errorClass('SpecialError', function () { | ||
@@ -21,0 +23,0 @@ if (arguments.length > 1) this.specialProperty = arguments[1] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6041
61
47