diff --git a/.babelrc b/.babelrc deleted file mode 100644 index c13c5f6..0000000 --- a/.babelrc +++ /dev/null @@ -1,3 +0,0 @@ -{ - "presets": ["es2015"] -} diff --git a/.gitignore b/.gitignore index b8ecb93..76796a4 100644 --- a/.gitignore +++ b/.gitignore @@ -17,6 +17,3 @@ jspm_packages *.orig .idea .DS_Store - -# babel output -index.js diff --git a/README.md b/README.md index a9d3aa7..ee40208 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # promisify-call Promisify a function call so users can call a function with a callback or get a promise. +* **2.0** works only with native `Promise` so requires Node >= 4.0. +* **1.0** uses [Bluebird](www.bluebirdjs.com) and should work with older Node.js ## Installation diff --git a/promisify.js b/index.js similarity index 64% rename from promisify.js rename to index.js index b2da47c..93d83a1 100644 --- a/promisify.js +++ b/index.js @@ -1,4 +1,4 @@ -import Promise from 'bluebird'; +const wc = require('with-callback') /** * Promisifies the call to fn if appropriate given the arguments. @@ -10,19 +10,19 @@ import Promise from 'bluebird'; * @param {arguments} args Arguments * @return {undefined|*|Promise} Promise if promisified */ -function promisifyCall(ctx, fn, ...args) { - const lastIndex = args.length - 1; - const lastArg = args && args.length > 0 ? args[lastIndex] : null; - const cb = typeof lastArg === 'function' ? lastArg : null; +function promisifyCall (ctx, fn, ...args) { + const lastIndex = args.length - 1 + const lastArg = args && args.length > 0 ? args[lastIndex] : null + const cb = typeof lastArg === 'function' ? lastArg : null if (cb) { - return fn.apply(ctx, args); + return fn.apply(ctx, args) } - return Promise.fromCallback(callback => { - args.push(callback); - fn.apply(ctx, args); - }); + return wc(callback => { + args.push(callback) + fn.apply(ctx, args) + }) } -module.exports = promisifyCall; +module.exports = promisifyCall diff --git a/package.json b/package.json index e7f30d2..a71d879 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,11 @@ { "name": "promisify-call", - "version": "1.0.0", + "version": "2.0.0", "description": "Promisify a function call so users can call a function with a callback or get a promise", "main": "index.js", "scripts": { - "test": "npm run dist && ava -v", - "dist": "node ./node_modules/babel-cli/bin/babel.js promisify.js --out-file index.js", - "docs": "jsdoc2md \"promisify.js\" --heading-depth 3 --template readme.hbs > README.md", - "prepublish": "npm run dist" + "test": "ava -v", + "docs": "jsdoc2md \"index.js\" --heading-depth 3 --template readme.hbs > README.md" }, "repository": { "type": "git", @@ -27,13 +25,16 @@ }, "homepage": "https://github.com/bojand/promisify-call#readme", "dependencies": { - "bluebird": "^3.4.1" + "with-callback": "^1.0.2" }, "devDependencies": { - "ava": "^0.16.0", - "babel-cli": "^6.11.4", - "babel-preset-es2015": "^6.13.2", - "xo": "^0.16.0" + "ava": "^0.17.0", + "babel-eslint": "^7.1.1", + "jsdoc-to-markdown": "^2.0.1", + "standard": "^8.5.0" + }, + "engines": { + "node": ">=4" }, "ava": { "files": [ @@ -43,32 +44,7 @@ "directories": { "test": "test" }, - "xo": { - "envs": [ - "node", - "es6", - "mocha" - ], - "esnext": true, - "space": true, - "rules": { - "babel/arrow-parens": 0, - "babel/object-curly-spacing": 0, - "babel/object-shorthand": 1, - "brace-style": [ - 0, - "1tbs", - { - "allowSingleLine": false - } - ], - "no-else-return": 1, - "no-unused-vars": 1, - "no-prototype-builtins": 0, - "import/namespace": 0 - }, - "ignores": [ - "test/**" - ] + "standard": { + "parser": "babel-eslint" } } diff --git a/readme.hbs b/readme.hbs index 27cd37f..1d3ad53 100644 --- a/readme.hbs +++ b/readme.hbs @@ -1,6 +1,8 @@ # promisify-call Promisify a function call so users can call a function with a callback or get a promise. +* **2.0** works only with native `Promise` so requires Node >= 4.0. +* **1.0** uses [Bluebird](www.bluebirdjs.com) and should work with older Node.js ## Installation diff --git a/test/promisify.test.js b/test/promisify.test.js index 6193178..d854269 100644 --- a/test/promisify.test.js +++ b/test/promisify.test.js @@ -1,58 +1,58 @@ -import test from 'ava'; +import test from 'ava' -import promisifyCall from '../'; +const promisifyCall = require('../') -function testFn(param, fn) { +function testFn (param, fn) { setTimeout(() => { if (param.toLowerCase() === 'error') { - return fn(new Error(param)); + return fn(new Error(param)) } - return fn(null, param.toUpperCase()); - }, 50); + return fn(null, param.toUpperCase()) + }, 50) } -function uppercase(param, fn) { - return promisifyCall(this, testFn, ...arguments); +function uppercase (param, fn) { + return promisifyCall(this, testFn, ...arguments) } test.cb('should properly return success value - callback', t => { uppercase('foo', (err, res) => { - t.ifError(err); - t.is(res, 'FOO'); - t.end(); - }); -}); + t.ifError(err) + t.is(res, 'FOO') + t.end() + }) +}) test.cb('should properly return error value - callback', t => { uppercase('error', (err, res) => { - t.truthy(err); - t.falsy(res); - t.end(); - }); -}); + t.truthy(err) + t.falsy(res) + t.end() + }) +}) test.cb('should properly return success value - promised using then()', t => { uppercase('foo').then(res => { - t.is(res, 'FOO'); - t.end(); - }); -}); + t.is(res, 'FOO') + t.end() + }) +}) test.cb('should properly return error value - promised using then()', t => { uppercase('error').then(res => { - t.falsy(res); + t.falsy(res) }).catch(err => { - t.truthy(err); - t.end(); - }); -}); + t.truthy(err) + t.end() + }) +}) test('should properly return success value - promised', async t => { - t.plan(1); - const res = await uppercase('foo'); - t.is(res, 'FOO'); -}); + t.plan(1) + const res = await uppercase('foo') + t.is(res, 'FOO') +}) test('should properly return error value - promised', async t => { - t.throws(uppercase('error')); -}); + t.throws(uppercase('error')) +})