Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Aug 11, 2016
0 parents commit 79bdd23
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
logs
*.log
npm-debug.log*
pids
*.pid
*.seed
lib-cov
coverage
.nyc_output
.grunt
.lock-wscript
build/Release
node_modules
jspm_packages
.npm
.node_repl_history
*.orig
.idea
.DS_Store

# babel output
index.js
19 changes: 19 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
logs
*.log
npm-debug.log*
pids
*.pid
*.seed
lib-cov
coverage
.nyc_output
.grunt
.lock-wscript
build/Release
node_modules
jspm_packages
.npm
.node_repl_history
*.orig
.idea
.DS_Store
9 changes: 9 additions & 0 deletions ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
1) What version of the module is the issue happening on? Does the issue happen on latest version?

2) What platform and Node.js version? (For example Node.js 0.12 on Mac OS X)

3) Sample source code or steps to reproduce

(Write description of your issue here, stack traces from errors and code that reproduces the issue are helpful)

4) If a pull request, do all tests pass?
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2016 Bojan D.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# promisify-call

Promisify a function call so users can call a function with a callback or get a promise.

## Installation

`npm install promisify-call`

## Usage

```js
function _uppercase(param, fn) {
setTimeout(() => {
return fn(null, param.toUpperCase());
}, 50);
}

function uppercase(param, fn) {
return promisifyCall(this, _uppercase, ...arguments);
}

// now we can call it using callback-style
uppercase('foo', (err, res) => {
console.log(res); // FOO
});

// OR promise style
const res = await uppercase('foo');
console.log(res); // FOO
```

## API Reference

<a name="promisifyCall"></a>

### promisifyCall(ctx, fn, ...args) ⇒ <code>undefined</code> &#124; <code>\*</code> &#124; <code>Promise</code>
Promisifies the call to <code>fn</code> if appropriate given the arguments.
Calls the function <code>fn</code> either using callback style if last argument is a function.
If last argument is not a function, <code>fn</code> is called returning a promise.
This lets you create API that can be called in either fashions.

**Kind**: global function
**Returns**: <code>undefined</code> &#124; <code>\*</code> &#124; <code>Promise</code> - Promise if promisified

| Param | Type | Description |
| --- | --- | --- |
| ctx | <code>Object</code> | context / this |
| fn | <code>function</code> | The function to call |
| ...args | <code>arguments</code> | Arguments |

## License

Copyright 2015 Bojan D.

Licensed under the MIT License.
76 changes: 76 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"name": "promisify-call",
"version": "0.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 test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/bojand/promisify-call.git"
},
"keywords": [
"promise",
"callback",
"promisify"
],
"author": {
"name": "Bojan Djurkovic <[email protected]>"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/bojand/promisify-call/issues"
},
"homepage": "https://github.com/bojand/promisify-call#readme",
"dependencies": {
"bluebird": "^3.4.1"
},
"devDependencies": {
"ava": "^0.16.0",
"babel-cli": "^6.11.4",
"babel-preset-es2015": "^6.13.2",
"xo": "^0.16.0"
},
"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"
},
"ava": {
"files": [
"test/*.test.js"
]
},
"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/**"
]
}
}
28 changes: 28 additions & 0 deletions promisify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Promise from 'bluebird';

/**
* Promisifies the call to <code>fn</code> if appropriate given the arguments.
* Calls the function <code>fn</code> either using callback style if last argument is a function.
* If last argument is not a function, <code>fn</code> is called returning a promise.
* This lets you create API that can be called in either fashions.
* @param {Object} ctx context / this
* @param {Function} fn The function to call
* @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;

if (cb) {
return fn.apply(ctx, args);
}

return Promise.fromCallback(callback => {
args.push(callback);
fn.apply(ctx, args);
});
}

module.exports = promisifyCall;
40 changes: 40 additions & 0 deletions readme.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# promisify-call

Promisify a function call so users can call a function with a callback or get a promise.

## Installation

`npm install promisify-call`

## Usage

```js
function _uppercase(param, fn) {
setTimeout(() => {
return fn(null, param.toUpperCase());
}, 50);
}

function uppercase(param, fn) {
return promisifyCall(this, _uppercase, ...arguments);
}

// now we can call it using callback-style
uppercase('foo', (err, res) => {
console.log(res); // FOO
});

// OR promise style
const res = await uppercase('foo');
console.log(res); // FOO
```

## API Reference

{{>all-docs~}}

## License

Copyright 2015 Bojan D.

Licensed under the MIT License.
58 changes: 58 additions & 0 deletions test/promisify.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import test from 'ava';

import promisifyCall from '../';

function testFn(param, fn) {
setTimeout(() => {
if (param.toLowerCase() === 'error') {
return fn(new Error(param));
}
return fn(null, param.toUpperCase());
}, 50);
}

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();
});
});

test.cb('should properly return errpr value - callback', t => {
uppercase('error', (err, res) => {
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();
});
});

test.cb('should properly return errpr value - promised using then()', t => {
uppercase('error').then(res => {
t.falsy(res);
}).catch(err => {
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');
});

test('should properly return errpr value - promised', t => {
t.throws(uppercase('error'));
});

0 comments on commit 79bdd23

Please sign in to comment.