Skip to content

Commit aedc57a

Browse files
committed
feat: 🎸 add err2cb util
1 parent bb9a28f commit aedc57a

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const { isNil, mergeArray, inheritPrototype } = require('./utils')
1+
const { isNil, mergeArray, inheritPrototype, err2cb } = require('./utils')
2+
exports.err2cb = err2cb
23

34
/* ----- General purpose utils ----- */
45

test/utils.test.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const test = require('./config')
2-
const { isNil, mergeArray, err2cb } = require('../utils')
2+
const { err2cb } = require('..')
3+
const { isNil, mergeArray } = require('../utils')
34

45
test('isNil is true for null or undefined', t=>{
56
t.is(isNil(undefined), true)
@@ -22,26 +23,34 @@ test('merge longer array into shorter', t => {
2223
[1,2,5]
2324
)
2425
})
25-
2626
test('merge into empty array', t => {
2727
t.deepEqual(mergeArray([], [1,2]), [1,2])
2828
})
29-
3029
test('no change for arrays of equal length', t => {
3130
t.deepEqual(mergeArray([1], [2]), [1])
3231
})
33-
3432
test('take the 1st array when both are of equal length', t => {
3533
t.deepEqual(mergeArray([1], [2]), [1])
3634
})
37-
3835
test('take the 1st array when the 2nd has smaller length', t => {
3936
t.deepEqual(mergeArray([1,3], [2]), [1,3])
4037
})
4138

4239

4340
test('err2cb preserves cps function without errors', t=>{
44-
err2cb(c=>c(2))(t.cis(2))
41+
t.plan(5)
42+
const cpsF1 = c=>c(2)
43+
const cpsF2 = (c1,c2)=>{c1(2);c2(4)}
44+
err2cb(cpsF1)(t.cis(2), _=>notCalled)
45+
err2cb(cpsF1,1)(t.cis(2))
46+
err2cb(cpsF1,2)(t.cis(2), _=>notCalled)
47+
err2cb(cpsF2)(t.cis(2),t.cis(4))
48+
})
49+
test('err2cb outputs error to 2nd callback by default', t=>{
50+
t.plan(2)
51+
const cpsF1 = c => bad
52+
t.throws(cpsF1)
53+
err2cb(cpsF1)(_=>notCalled, e => {t.is(typeof e, 'object')})
4554
})
4655

4756

0 commit comments

Comments
 (0)