From 95cb66af78af3e65e3004b72152bb6f25d289daf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Oddsson?= Date: Mon, 27 Sep 2021 10:19:26 +0100 Subject: [PATCH] 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 --- package.json | 4 +--- src/index.ts | 58 ++++++++++----------------------------------------- tsconfig.json | 2 +- 3 files changed, 13 insertions(+), 51 deletions(-) diff --git a/package.json b/package.json index 5cc99bb..a7776f1 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/index.ts b/src/index.ts index 6f0a2e3..ad88fb0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,42 +1,10 @@ -/** - * 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 { - var excludes = args - - function excludeProps (res: Record, obj: Record) { - 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 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'; @@ -44,13 +12,15 @@ export default class AssertionError extends Error { // 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(); @@ -60,21 +30,15 @@ export default class AssertionError 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 { + 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; }; diff --git a/tsconfig.json b/tsconfig.json index 39b187b..49d78d3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,5 +12,5 @@ }, "include": [ "src" -] + ] }