Skip to content
This repository was archived by the owner on Aug 31, 2024. It is now read-only.

Commit a32f440

Browse files
committedOct 31, 2015
add convert.compose()
1 parent 27e50c7 commit a32f440

File tree

3 files changed

+51
-7
lines changed

3 files changed

+51
-7
lines changed
 

Diff for: ‎index.js

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict'
22

33
const co = require('co')
4+
const compose = require('koa-compose')
45

56
module.exports = convert
67

@@ -18,6 +19,15 @@ function convert (mw) {
1819
}
1920
}
2021

22+
// convert.compose(mw, mw, mw)
23+
// convert.compose([mw, mw, mw])
24+
convert.compose = function (arr) {
25+
if (!Array.isArray(arr)) {
26+
arr = Array.from(arguments)
27+
}
28+
return compose(arr.map(convert))
29+
}
30+
2131
function * createGenerator (next) {
2232
return yield next()
2333
}

Diff for: ‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
},
2323
"homepage": "https://github.com/gyson/koa-convert#readme",
2424
"dependencies": {
25-
"co": "^4.6.0"
25+
"co": "^4.6.0",
26+
"koa-compose": "^3.0.0"
2627
},
2728
"devDependencies": {
28-
"koa-compose": "^3.0.0",
2929
"mocha": "^2.3.3",
3030
"standard": "^5.3.1"
3131
},

Diff for: ‎test.js

+39-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
const co = require('co')
66
const assert = require('assert')
77
const convert = require('./index')
8-
const compose = require('koa-compose')
98

10-
describe('Koa Convert', () => {
9+
describe('convert()', () => {
1110
it('should works', () => {
1211
let call = []
1312
let ctx = {}
@@ -58,12 +57,14 @@ describe('Koa Convert', () => {
5857
assert.deepEqual(call, [1, 2, 3])
5958
})
6059
})
60+
})
6161

62-
it('should works with koa-compose', () => {
62+
describe('convert.compose()', () => {
63+
it('should works', () => {
6364
let call = []
6465
let context = {}
6566
let _context
66-
let mw = compose([
67+
let mw = convert.compose([
6768
function * name (next) {
6869
call.push(1)
6970
yield next
@@ -98,11 +99,44 @@ describe('Koa Convert', () => {
9899
call.push(6)
99100
throw new Error()
100101
}
101-
].map(convert))
102+
])
102103

103104
return mw(context).then(() => {
104105
assert.equal(context, _context)
105106
assert.deepEqual(call, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
106107
})
107108
})
109+
110+
it('should works too', () => {
111+
let call = []
112+
let context = {}
113+
let _context
114+
let mw = convert.compose(
115+
(ctx, next) => {
116+
call.push(1)
117+
return next().catch(() => {
118+
call.push(4)
119+
})
120+
},
121+
function * (next) {
122+
call.push(2)
123+
yield next
124+
call.push(-1) // should not call this
125+
},
126+
function * (next) {
127+
call.push(3)
128+
yield* next
129+
call.push(-1) // should not call this
130+
},
131+
(ctx, next) => {
132+
_context = ctx
133+
return Promise.reject(new Error())
134+
}
135+
)
136+
137+
return mw(context).then(() => {
138+
assert.equal(context, _context)
139+
assert.deepEqual(call, [1, 2, 3, 4])
140+
})
141+
})
108142
})

0 commit comments

Comments
 (0)
This repository has been archived.