Skip to content

Commit a1f6d43

Browse files
committed
feature: try-catch: migrate to ESM
1 parent d6f6cf9 commit a1f6d43

7 files changed

Lines changed: 76 additions & 18 deletions

File tree

.madrun.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict';
1+
import {run} from 'madrun';
22

3-
const {run} = require('madrun');
4-
5-
module.exports = {
6-
'test': () => `tape 'test/*.js'`,
3+
export default {
4+
'test': () => `tape 'test/*.{js,cjs}'`,
75
'lint': () => 'putout .',
86
'fix:lint': () => run('lint', '--fix'),
97
'coverage': () => 'c8 npm test',

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ npm i try-catch
2020
## Example
2121

2222
```js
23-
const tryCatch = require('try-catch');
23+
import tryCatch from 'try-catch';
24+
2425
const {parse} = JSON;
2526
const [error, result] = tryCatch(parse, 'hello');
2627

lib/try-catch.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const {tryCatch} = require('./try-catch.js');
4+
5+
module.exports = tryCatch;
6+
module.exports.tryCatch = tryCatch;
7+

lib/try-catch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
'use strict';
2-
3-
module.exports = (fn, ...args) => {
1+
export const tryCatch = (fn, ...args) => {
42
try {
53
return [null, fn(...args)];
64
} catch(e) {
75
return [e];
86
}
97
};
8+
9+
export default tryCatch;

package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
{
22
"name": "try-catch",
33
"version": "3.0.1",
4-
"type": "commonjs",
4+
"type": "module",
55
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
66
"description": "functional try-catch wrapper",
77
"homepage": "http://github.com/coderaiser/try-catch",
88
"repository": {
99
"type": "git",
1010
"url": "git+https://github.com/coderaiser/try-catch.git"
1111
},
12+
"exports": {
13+
".": {
14+
"node": {
15+
"require": "./lib/try-catch.cjs",
16+
"import": "./lib/try-catch.js"
17+
}
18+
}
19+
},
1220
"scripts": {
1321
"test": "madrun test",
1422
"lint": "madrun lint",
@@ -28,7 +36,6 @@
2836
},
2937
"license": "MIT",
3038
"engines": {
31-
"node": ">=6"
32-
},
33-
"main": "lib/try-catch.js"
39+
"node": ">=20"
40+
}
3441
}

test/try-catch.cjs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
const test = require('supertape');
4+
const tryCatch = require('try-catch');
5+
6+
test('try-catch: cjs: error', (t) => {
7+
const fn = () => hello;
8+
const [e] = tryCatch(fn);
9+
const message = 'hello is not defined';
10+
11+
t.equal(e.message, message);
12+
t.end();
13+
});
14+
15+
test('try-catch: cjs: result', (t) => {
16+
const fn = () => 'hello';
17+
const [, data] = tryCatch(fn);
18+
19+
t.equal(data, 'hello');
20+
t.end();
21+
});
22+
23+
test('try-catch: cjs: args: result', (t) => {
24+
const [, data] = tryCatch(JSON.stringify, {
25+
a: 'b',
26+
});
27+
28+
t.equal(data, '{"a":"b"}');
29+
t.end();
30+
});
31+
32+
test('try-catch: cjs: named export', (t) => {
33+
const [, data] = tryCatch.tryCatch(JSON.stringify, {
34+
a: 'b',
35+
});
36+
37+
t.equal(data, '{"a":"b"}');
38+
t.end();
39+
});

test/try-catch.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
'use strict';
2-
3-
const test = require('supertape');
4-
const tryCatch = require('..');
1+
import test from 'supertape';
2+
import tryCatch, {tryCatch as _tryCatch} from 'try-catch';
53

64
test('try-catch: error', (t) => {
7-
/*eslint no-undef: 0*/
85
const fn = () => hello;
96
const [e] = tryCatch(fn);
107
const message = 'hello is not defined';
@@ -29,3 +26,12 @@ test('try-catch: args: result', (t) => {
2926
t.equal(data, '{"a":"b"}');
3027
t.end();
3128
});
29+
30+
test('try-catch: named export', (t) => {
31+
const [, data] = _tryCatch(JSON.stringify, {
32+
a: 'b',
33+
});
34+
35+
t.equal(data, '{"a":"b"}');
36+
t.end();
37+
});

0 commit comments

Comments
 (0)