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-
311export 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 } ;
0 commit comments