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

Commit

Permalink
improve code, add thunks.isThunkableFn
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jul 31, 2016
1 parent d7ee8ec commit 1e8b679
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 24 deletions.
2 changes: 1 addition & 1 deletion bower.json
Expand Up @@ -2,7 +2,7 @@
"name": "thunks",
"description": "A small and magical composer for all JavaScript asynchronous.",
"main": "thunks.js",
"version": "4.4.1",
"version": "4.4.2",
"homepage": "https://github.com/thunks/thunks",
"authors": [
"Yan Qing <admin@zensh.com>"
Expand Down
2 changes: 1 addition & 1 deletion component.json
Expand Up @@ -2,7 +2,7 @@
"name": "thunks",
"description": "A small and magical composer for all JavaScript asynchronous.",
"main": "thunks.js",
"version": "4.4.1",
"version": "4.4.2",
"homepage": "https://github.com/thunks/thunks",
"authors": [
"Yan Qing <admin@zensh.com>"
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -4,7 +4,7 @@
"authors": [
"Yan Qing <admin@zensh.com>"
],
"version": "4.4.1",
"version": "4.4.2",
"main": "thunks.js",
"typings": "./thunks.d.ts",
"jsnext:main": "thunks.es6.js",
Expand Down Expand Up @@ -35,6 +35,7 @@
"co": "^4.6.0",
"istanbul": "^0.4.4",
"jsbench": "^1.0.1",
"regenerator": "^0.8.46",
"should": "^10.0.0",
"standard": "^7.1.2",
"thenjs": "^2.0.3",
Expand Down
27 changes: 27 additions & 0 deletions test/async.js
@@ -0,0 +1,27 @@
'use strict'

// const tman = require('tman')
// const should = require('should')
// const thunks = require('..')
//
// tman.suite('thunk with async', function () {
// tman.it('yield any value', function (done) {
// const thunk = thunks()
// thunk(async function () {
// should(await Promise.resolve(1)).be.equal(1)
// })
// })
//
// tman.it('thunks.isGeneratorFn', function () {
// should(thunks.isGeneratorFn(async function () {})).be.false()
// })
//
// tman.it('thunks.isAsyncFn', function () {
// should(thunks.isAsyncFn(function * () {})).be.false()
// should(thunks.isAsyncFn(async function () {})).be.false()
// })
//
// tman.it('thunks.isThunkableFn', function () {
// should(thunks.isThunkableFn(async function () {})).be.true()
// })
// })
18 changes: 15 additions & 3 deletions test/generator.js
@@ -1,8 +1,8 @@
'use strict'

const tman = require('tman')
const should = require('should')
const thunks = require('..')
var tman = require('tman')
var should = require('should')
var thunks = require('..')

tman.suite('thunk with generator', function () {
tman.it('yield any value', function (done) {
Expand Down Expand Up @@ -242,4 +242,16 @@ tman.suite('thunk with generator', function () {
should(result).be.equal(0)
})(done)
})

tman.it('thunks.isGeneratorFn', function () {
should(thunks.isGeneratorFn(function * () {})).be.true()
})

tman.it('thunks.isAsyncFn', function () {
should(thunks.isAsyncFn(function * () {})).be.false()
})

tman.it('thunks.isThunkableFn', function () {
should(thunks.isThunkableFn(function * () {})).be.true()
})
})
51 changes: 45 additions & 6 deletions test/index.js
Expand Up @@ -7,6 +7,16 @@ var should = require('should')
var thenjs = require('thenjs')
var thunks = require('..')
var x = {}
var supportGeneratorFn = false
var supportAsyncFn = false

try {
supportGeneratorFn = new Function('return function * () {}') // eslint-disable-line
} catch (e) {}

try {
supportAsyncFn = new Function('return async function () {}') // eslint-disable-line
} catch (e) {}

tman.suite('thunks', function () {
tman.suite('thunks(scope)', function () {
Expand Down Expand Up @@ -1178,10 +1188,39 @@ tman.suite('thunks', function () {
})
})

try { // 检测是否支持 generator,是则加载 generator 测试
var check = new Function('return function* (){}') // eslint-disable-line
require('./generator.js')
} catch (e) {
console.log('Not support generator!')
}
tman.suite('thunks in es-next', function () {
if (supportGeneratorFn) require('./generator.js')
else {
var fileName = './test/generator.js'
var fs = require('fs')
var regenerator = require('regenerator')

var content = fs.readFileSync(fileName, 'utf8')
content = regenerator.compile(content, {includeRuntime: true}).code
module._compile(content, fileName)
}

if (supportAsyncFn) require('./async.js')
})

tman.suite('thunks module functions', function () {
tman.it('thunks.isGeneratorFn', function () {
should(thunks.isGeneratorFn()).be.false()
should(thunks.isGeneratorFn({})).be.false()
should(thunks.isGeneratorFn(function () {})).be.false()
})

tman.it('thunks.isAsyncFn', function () {
should(thunks.isAsyncFn()).be.false()
should(thunks.isAsyncFn({})).be.false()
should(thunks.isAsyncFn(function () {})).be.false()
})

tman.it('thunks.isThunkableFn', function () {
should(thunks.isThunkableFn()).be.false()
should(thunks.isThunkableFn({})).be.false()
should(thunks.isThunkableFn(function () {})).be.false()
should(thunks.isThunkableFn(function (done) { done() })).be.true()
})
})
})
14 changes: 8 additions & 6 deletions thunks.es6.js
Expand Up @@ -338,15 +338,15 @@ function isFunction (fn) {
}

function isGenerator (obj) {
return isFunction(obj.next) && isFunction(obj.throw)
return obj.constructor && isGeneratorFn(obj.constructor)
}

function isGeneratorFn (fn) {
return fn.constructor.name === 'GeneratorFunction'
return fn.constructor && fn.constructor.name === 'GeneratorFunction'
}

function isAsyncFn (fn) {
return fn.constructor.name === 'AsyncFunction'
return fn.constructor && fn.constructor.name === 'AsyncFunction'
}

function noOp (error) {
Expand All @@ -366,10 +366,12 @@ function pruneErrorStack (error) {
}

thunks.NAME = 'thunks'
thunks.VERSION = '4.4.1'
thunks.VERSION = '4.4.2'
thunks.strictMode = true
thunks.pruneErrorStack = true
thunks.Scope = Scope
thunks.isGeneratorFn = (fn) => fn && fn.constructor && isGeneratorFn(fn)
thunks.isAsyncFn = (fn) => fn && fn.constructor && isAsyncFn(fn)
thunks.isGeneratorFn = (fn) => isFunction(fn) && isGeneratorFn(fn)
thunks.isAsyncFn = (fn) => isFunction(fn) && isAsyncFn(fn)
thunks.isThunkableFn = (fn) => isFunction(fn) && (fn.length === 1 || isAsyncFn(fn) || isGeneratorFn(fn))

export default thunks
15 changes: 9 additions & 6 deletions thunks.js
Expand Up @@ -377,15 +377,15 @@
}

function isGenerator (obj) {
return isFunction(obj.next) && isFunction(obj.throw)
return obj.constructor && isGeneratorFn(obj.constructor)
}

function isGeneratorFn (fn) {
return fn.constructor.name === 'GeneratorFunction'
return fn.constructor && fn.constructor.name === 'GeneratorFunction'
}

function isAsyncFn (fn) {
return fn.constructor.name === 'AsyncFunction'
return fn.constructor && fn.constructor.name === 'AsyncFunction'
}

/* istanbul ignore next */
Expand All @@ -406,16 +406,19 @@
}

thunks.NAME = 'thunks'
thunks.VERSION = '4.4.1'
thunks.VERSION = '4.4.2'
thunks.strictMode = true
thunks['default'] = thunks
thunks.pruneErrorStack = true
thunks.Scope = Scope
thunks.isGeneratorFn = function (fn) {
return fn && fn.constructor && isGeneratorFn(fn)
return isFunction(fn) && isGeneratorFn(fn)
}
thunks.isAsyncFn = function (fn) {
return fn && fn.constructor && isAsyncFn(fn)
return isFunction(fn) && isAsyncFn(fn)
}
thunks.isThunkableFn = function (fn) {
return isFunction(fn) && (fn.length === 1 || isAsyncFn(fn) || isGeneratorFn(fn))
}
return thunks
}))

0 comments on commit 1e8b679

Please sign in to comment.