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

Commit

Permalink
use standard
Browse files Browse the repository at this point in the history
  • Loading branch information
gyson committed Oct 30, 2015
1 parent d191520 commit e6628be
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 71 deletions.
28 changes: 14 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
'use strict';
'use strict'

const co = require('co')

module.exports = convert

function convert(mw) {
if (typeof mw !== 'function') {
throw new TypeError(mw + ' is not function')
}
if (mw.constructor.name === 'GeneratorFunction') {
return function (ctx, next) {
return co.call(ctx, mw.call(ctx, createGenerator(next)))
}
} else {
// assume it's Promise-based middleware
return mw
function convert (mw) {
if (typeof mw !== 'function') {
throw new TypeError(mw + ' is not function')
}
if (mw.constructor.name === 'GeneratorFunction') {
return function (ctx, next) {
return co.call(ctx, mw.call(ctx, createGenerator(next)))
}
} else {
// assume it's Promise-based middleware
return mw
}
}

function* createGenerator(next) {
return yield next()
function * createGenerator (next) {
return yield next()
}
11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
"description": "convert koa legacy generator-based middleware to promise-based middleware",
"repository": {
"type": "git",
"url": "https://github.com/gyson/koa-convert.git"
"url": "git+https://github.com/gyson/koa-convert.git"
},
"main": "index.js",
"scripts": {
"test": "mocha test.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/gyson/koa-convert.git"
"test": "standard && mocha test.js"
},
"author": "gyson <[email protected]>",
"license": "MIT",
Expand All @@ -29,6 +25,7 @@
"co": "^4.6.0"
},
"devDependencies": {
"mocha": "^2.3.3"
"mocha": "^2.3.3",
"standard": "^5.3.1"
}
}
102 changes: 52 additions & 50 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,61 +1,63 @@
'use strict';
/* global describe, it */

const co = require('co')
'use strict'

// const co = require('co')
const assert = require('assert')
const convert = require('./index')

describe('Koa Convert', function () {
it('should works', function (done) {
let call = []
let ctx = {}
let mw = convert(function* (next) {
assert.ok(ctx === this)
call.push(1)
})

mw(ctx, function () {
done(new Error('this should not be called'))
}).then(function () {
assert.deepEqual(call, [1])
done()
})
it('should works', function (done) {
let call = []
let ctx = {}
let mw = convert(function * (next) {
assert.ok(ctx === this)
call.push(1)
})

mw(ctx, function () {
done(new Error('this should not be called'))
}).then(function () {
assert.deepEqual(call, [1])
done()
})
})

it('should works with `yield next`', function (done) {
let call = []
let ctx = {}
let mw = convert(function * (next) {
assert.ok(ctx === this)
call.push(1)
yield next
call.push(3)
})

mw(ctx, function () {
call.push(2)
return Promise.resolve()
}).then(function () {
assert.deepEqual(call, [1, 2, 3])
done()
})
})

it('should works with `yield next`', function (done) {
let call = []
let ctx = {}
let mw = convert(function* (next) {
assert.ok(ctx === this)
call.push(1)
yield next
call.push(3)
})

mw(ctx, function () {
call.push(2)
return Promise.resolve()
}).then(function () {
assert.deepEqual(call, [1, 2, 3])
done()
})
it('should works with `yield* next`', function (done) {
let call = []
let ctx = {}
let mw = convert(function * (next) {
assert.ok(ctx === this)
call.push(1)
yield* next
call.push(3)
})

it('should works with `yield* next`', function (done) {
let call = []
let ctx = {}
let mw = convert(function* (next) {
assert.ok(ctx === this)
call.push(1)
yield* next
call.push(3)
})

mw(ctx, function () {
call.push(2)
return Promise.resolve()
}).then(function () {
assert.deepEqual(call, [1, 2, 3])
done()
})
mw(ctx, function () {
call.push(2)
return Promise.resolve()
}).then(function () {
assert.deepEqual(call, [1, 2, 3])
done()
})
})
})

0 comments on commit e6628be

Please sign in to comment.