Skip to content

Commit 95cb66a

Browse files
authored
Simplify project a bit (#25)
* Simplify project by removing `exclude` function * Remove `test:typescript` script The test script runs `tsc` as part of the `pretest` script anyway * Make sure to now overwrite properties by checking for them * Simplify more * Change condition
1 parent ff3165c commit 95cb66a

File tree

3 files changed

+13
-51
lines changed

3 files changed

+13
-51
lines changed

package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
"scripts": {
2727
"build": "tsc",
2828
"pretest": "npm run build",
29-
"test": "npm run test:node && npm run test:typescript",
30-
"test:node": "NODE_ENV=test node ./test/index.js",
31-
"test:typescript": "tsc test/typings.ts dist/index.d.ts --noEmit"
29+
"test": "NODE_ENV=test node ./test/index.js"
3230
},
3331
"devDependencies": {
3432
"typescript": "^4.4.3"

src/index.ts

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,26 @@
1-
/**
2-
* Return a function that will copy properties from
3-
* one object to another excluding any originally
4-
* listed. Returned function will create a new `{}`.
5-
*
6-
* @param {String} excluded properties ...
7-
* @return {Function}
8-
*/
9-
function exclude(...args: string[]): (a: {}, b?: {}) => Record<string, unknown> {
10-
var excludes = args
11-
12-
function excludeProps (res: Record<string, unknown>, obj: Record<string, unknown>) {
13-
Object.keys(obj).forEach(function (key) {
14-
if (!~excludes.indexOf(key)) res[key] = obj[key];
15-
});
16-
}
17-
18-
return function extendExclude () {
19-
var args = [].slice.call(arguments)
20-
, i = 0
21-
, res = {};
22-
23-
for (; i < args.length; i++) {
24-
excludeProps(res, args[i]);
25-
}
26-
27-
return res;
28-
};
29-
};
30-
311
export default class AssertionError<T> extends Error {
322
name = 'AssertionError'
333
showDiff: boolean
344
[key: string]: any
355

36-
constructor(message: string, _props?: T, ssf?: Function) {
37-
super()
38-
var extend = exclude('name', 'message', 'stack', 'constructor', 'toJSON')
39-
, props = extend(_props || {});
6+
constructor(message: string, props?: T, ssf?: Function) {
7+
super(message)
408

419
// default values
4210
this.message = message || 'Unspecified AssertionError';
4311
this.showDiff = false;
4412

4513
// copy from properties
4614
for (var key in props) {
47-
this[key] = props[key];
15+
if (!(key in this)) {
16+
// @ts-ignore
17+
this[key] = props[key];
18+
}
4819
}
4920

5021
// capture stack trace
51-
ssf = ssf || AssertionError;
5222
if ((Error as any).captureStackTrace) {
53-
(Error as any).captureStackTrace(this, ssf);
23+
(Error as any).captureStackTrace(this, ssf || AssertionError);
5424
} else {
5525
try {
5626
throw new Error();
@@ -60,21 +30,15 @@ export default class AssertionError<T> extends Error {
6030
}
6131
}
6232

63-
/**
64-
* Allow errors to be converted to JSON for static transfer.
65-
*
66-
* @param {Boolean} include stack (default: `true`)
67-
* @return {Object} object that can be `JSON.stringify`
68-
*/
69-
70-
toJSON(stack: boolean) {
71-
var extend = exclude('constructor', 'toJSON', 'stack')
72-
, props = extend({ name: this.name }, this);
33+
// Allow errors to be converted to JSON for static transfer.
34+
toJSON(stack: boolean): Record<string, unknown> {
35+
const {...props} = this
7336

7437
// include stack if exists and not turned off
7538
if (false !== stack && this.stack) {
7639
props.stack = this.stack;
7740
}
41+
props.message = this.message
7842

7943
return props;
8044
};

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
},
1313
"include": [
1414
"src"
15-
]
15+
]
1616
}

0 commit comments

Comments
 (0)