Skip to content

Commit ff3165c

Browse files
authored
Convert module to TypeScript (#24)
1 parent a57dda3 commit ff3165c

File tree

8 files changed

+110
-131
lines changed

8 files changed

+110
-131
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ node_modules
1616
npm-debug.log
1717

1818
coverage.html
19+
dist

index.d.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

index.js

Lines changed: 0 additions & 114 deletions
This file was deleted.

package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "Error constructor for test and validation frameworks that implements standardized AssertionError specification.",
55
"author": "Jake Luer <jake@qualiancy.com> (http://qualiancy.com)",
66
"license": "MIT",
7-
"types": "./index.d.ts",
7+
"types": "./dist/index.d.ts",
88
"keywords": [
99
"test",
1010
"assertion",
@@ -17,13 +17,18 @@
1717
"engines": {
1818
"node": ">=12"
1919
},
20+
"files": [
21+
"dist"
22+
],
2023
"type": "module",
21-
"module": "./index.js",
22-
"main": "./index.js",
24+
"module": "./dist/index.js",
25+
"main": "./dist/index.js",
2326
"scripts": {
27+
"build": "tsc",
28+
"pretest": "npm run build",
2429
"test": "npm run test:node && npm run test:typescript",
2530
"test:node": "NODE_ENV=test node ./test/index.js",
26-
"test:typescript": "tsc test/typings.ts index.d.ts --noEmit"
31+
"test:typescript": "tsc test/typings.ts dist/index.d.ts --noEmit"
2732
},
2833
"devDependencies": {
2934
"typescript": "^4.4.3"

src/index.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
31+
export default class AssertionError<T> extends Error {
32+
name = 'AssertionError'
33+
showDiff: boolean
34+
[key: string]: any
35+
36+
constructor(message: string, _props?: T, ssf?: Function) {
37+
super()
38+
var extend = exclude('name', 'message', 'stack', 'constructor', 'toJSON')
39+
, props = extend(_props || {});
40+
41+
// default values
42+
this.message = message || 'Unspecified AssertionError';
43+
this.showDiff = false;
44+
45+
// copy from properties
46+
for (var key in props) {
47+
this[key] = props[key];
48+
}
49+
50+
// capture stack trace
51+
ssf = ssf || AssertionError;
52+
if ((Error as any).captureStackTrace) {
53+
(Error as any).captureStackTrace(this, ssf);
54+
} else {
55+
try {
56+
throw new Error();
57+
} catch(e: any) {
58+
this.stack = e.stack;
59+
}
60+
}
61+
}
62+
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);
73+
74+
// include stack if exists and not turned off
75+
if (false !== stack && this.stack) {
76+
props.stack = this.stack;
77+
}
78+
79+
return props;
80+
};
81+
}

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Include lib
33
*/
44

5-
import AssertionError from '../index.js'
5+
import AssertionError from '../dist/index.js'
66

77
/*!
88
* Simple test runner.

test/typings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import AssertionError = require('../index');
1+
import AssertionError from '../src/index'
22

33
const str: string = "";
4-
let e: AssertionError;
4+
let e;
55

66
function foo () { }
77

tsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"module": "esnext",
4+
"target": "es2017",
5+
"lib": ["es2018", "dom"],
6+
"strict": true,
7+
"declaration": true,
8+
"removeComments": true,
9+
"preserveConstEnums": true,
10+
"allowSyntheticDefaultImports": true,
11+
"outDir": "dist"
12+
},
13+
"include": [
14+
"src"
15+
]
16+
}

0 commit comments

Comments
 (0)