Skip to content

Commit

Permalink
Simplify project a bit (#25)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
koddsson authored Sep 27, 2021
1 parent ff3165c commit 95cb66a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 51 deletions.
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
"scripts": {
"build": "tsc",
"pretest": "npm run build",
"test": "npm run test:node && npm run test:typescript",
"test:node": "NODE_ENV=test node ./test/index.js",
"test:typescript": "tsc test/typings.ts dist/index.d.ts --noEmit"
"test": "NODE_ENV=test node ./test/index.js"
},
"devDependencies": {
"typescript": "^4.4.3"
Expand Down
58 changes: 11 additions & 47 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,26 @@
/**
* Return a function that will copy properties from
* one object to another excluding any originally
* listed. Returned function will create a new `{}`.
*
* @param {String} excluded properties ...
* @return {Function}
*/
function exclude(...args: string[]): (a: {}, b?: {}) => Record<string, unknown> {
var excludes = args

function excludeProps (res: Record<string, unknown>, obj: Record<string, unknown>) {
Object.keys(obj).forEach(function (key) {
if (!~excludes.indexOf(key)) res[key] = obj[key];
});
}

return function extendExclude () {
var args = [].slice.call(arguments)
, i = 0
, res = {};

for (; i < args.length; i++) {
excludeProps(res, args[i]);
}

return res;
};
};

export default class AssertionError<T> extends Error {
name = 'AssertionError'
showDiff: boolean
[key: string]: any

constructor(message: string, _props?: T, ssf?: Function) {
super()
var extend = exclude('name', 'message', 'stack', 'constructor', 'toJSON')
, props = extend(_props || {});
constructor(message: string, props?: T, ssf?: Function) {
super(message)

// default values
this.message = message || 'Unspecified AssertionError';
this.showDiff = false;

// copy from properties
for (var key in props) {
this[key] = props[key];
if (!(key in this)) {
// @ts-ignore
this[key] = props[key];
}
}

// capture stack trace
ssf = ssf || AssertionError;
if ((Error as any).captureStackTrace) {
(Error as any).captureStackTrace(this, ssf);
(Error as any).captureStackTrace(this, ssf || AssertionError);
} else {
try {
throw new Error();
Expand All @@ -60,21 +30,15 @@ export default class AssertionError<T> extends Error {
}
}

/**
* Allow errors to be converted to JSON for static transfer.
*
* @param {Boolean} include stack (default: `true`)
* @return {Object} object that can be `JSON.stringify`
*/

toJSON(stack: boolean) {
var extend = exclude('constructor', 'toJSON', 'stack')
, props = extend({ name: this.name }, this);
// Allow errors to be converted to JSON for static transfer.
toJSON(stack: boolean): Record<string, unknown> {
const {...props} = this

// include stack if exists and not turned off
if (false !== stack && this.stack) {
props.stack = this.stack;
}
props.message = this.message

return props;
};
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
},
"include": [
"src"
]
]
}

0 comments on commit 95cb66a

Please sign in to comment.