Comparing version 0.0.0 to 1.0.0
## 1.0.0 - 2016-01-15 | ||
- Release v1.0.0 / npm@v1.0.0 | ||
- docs | ||
- update duplication | ||
- implement :star: | ||
## 0.0.0 - 2016-01-15 | ||
- Initial commit |
63
index.js
@@ -10,4 +10,63 @@ /*! | ||
module.exports = function relike () { | ||
// body | ||
var utils = require('./utils') | ||
/** | ||
* Will try to promisify `fn` with native Promise, | ||
* otherwise will use `Bluebird` or you can give | ||
* different promise module to `relike.promise`, for example `pinkie`. | ||
* | ||
* **Example** | ||
* | ||
* ```js | ||
* const fs = require('fs') | ||
* const request = require('request') | ||
* const relike = require('relike') | ||
* | ||
* relike(fs.readFile, 'package.json', 'utf-8').then(data => { | ||
* console.log(JSON.parse(data).name) | ||
* }) | ||
* | ||
* // handles multiple arguments by default (comes from `request`) | ||
* relike(request, 'http://www.tunnckocore.tk/').then(result => { | ||
* const [httpResponse, body] = result | ||
* }) | ||
* ``` | ||
* | ||
* @name relike | ||
* @param {Function} `<fn>` callback-style or synchronous function to promisify | ||
* @return {Promise} promise | ||
* @api public | ||
*/ | ||
module.exports = function relike (fn) { | ||
var Prome = utils.nativeOrAnother(relike.promise) | ||
if (typeof fn !== 'function') { | ||
return Prome.reject(new TypeError('relike expect a function')) | ||
} | ||
var argz = utils.handleArguments(arguments) | ||
var self = this | ||
argz.args = argz.args.slice(1) | ||
if (argz.callback && !utils.isAsyncFunction(argz.callback)) { | ||
argz.args = argz.args.concat(argz.callback) | ||
} | ||
var promise = new Prome(function prome (resolve, reject) { | ||
var isAsync = utils.isAsyncFunction(fn) | ||
if (isAsync) { | ||
argz.args = argz.args.concat(function cb (err, res) { | ||
if (err) return reject(err) | ||
if (arguments.length > 2) res = utils.sliced(arguments, 1) | ||
resolve(res) | ||
}) | ||
} | ||
var syncResult = fn.apply(self, argz.args) | ||
if (!isAsync) { | ||
resolve(syncResult) | ||
} | ||
}) | ||
promise.Prome = Prome | ||
promise.___customPromise = Prome.___customPromise | ||
promise.___bluebirdPromise = Prome.___bluebirdPromise | ||
return promise | ||
} |
{ | ||
"name": "relike", | ||
"version": "0.0.0", | ||
"version": "1.0.0", | ||
"description": "Simple promisify a callback-style function with sane defaults. Support promisify-ing sync functions.", | ||
@@ -12,12 +12,32 @@ "repository": "hybridables/relike", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"handle-arguments": "^3.0.4", | ||
"is-async-function": "^1.1.0", | ||
"lazy-cache": "^1.0.3", | ||
"native-or-another": "^3.0.1", | ||
"sliced": "^1.0.1" | ||
}, | ||
"devDependencies": { | ||
"assertit": "^0.1.0" | ||
"assertit": "^0.1.0", | ||
"is-buffer": "^1.1.0", | ||
"isarray": "1.0.0", | ||
"pinkie": "^2.0.1", | ||
"semver": "^5.0.3" | ||
}, | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"utils.js" | ||
], | ||
"keywords": [ | ||
"relike" | ||
"anything", | ||
"bluebird", | ||
"callback", | ||
"defaults", | ||
"everywhere", | ||
"native", | ||
"native-promise", | ||
"promise", | ||
"promisify", | ||
"simple" | ||
] | ||
} |
@@ -21,3 +21,87 @@ # [relike][author-www-url] [![npmjs.com][npmjs-img]][npmjs-url] [![The MIT License][license-img]][license-url] | ||
### [relike](./index.js#L38) | ||
> Will try to promisify `fn` with native Promise, otherwise will use `Bluebird` | ||
or you can give different promise module to `relike.promise`, for example `pinkie`. | ||
- `<fn>` **{Function}** callback-style or synchronous function to promisify | ||
- `return` **{Promise}** promise | ||
**Example** | ||
```js | ||
const fs = require('fs') | ||
const request = require('request') | ||
const relike = require('relike') | ||
relike(fs.readFile, 'package.json', 'utf-8').then(data => { | ||
console.log(JSON.parse(data).name) | ||
}) | ||
// promisify sync function | ||
relike(fs.readFileSync, 'package.json', 'utf-8') | ||
.then(JSON.parse) | ||
.then(res => { | ||
console.log(res.name) | ||
}) | ||
// handles multiple arguments by default (comes from `request`) | ||
relike(request, 'http://www.tunnckocore.tk/').then(result => { | ||
const [httpResponse, body] = result | ||
}) | ||
``` | ||
### relike.promise | ||
> Static property on which you can pass custom Promise module to use, e.g. `Q` constructor. | ||
**Example** | ||
```js | ||
const fs = require('fs') | ||
const relike = require('relike') | ||
// `q` promise will be used if not native promise available | ||
// but only in node <= 0.11.12 | ||
relike.promise = require('q') | ||
relike(fs.readFile, 'package.json', 'utf-8').then(data => { | ||
console.log(JSON.parse(data).name) | ||
}) | ||
``` | ||
### Access Promise constructor | ||
> You can access the used Promise constructor for promisify-ing from `promise.Prome` | ||
**Example** | ||
```js | ||
const fs = require('fs') | ||
const relike = require('relike') | ||
// use `pinkie` promise if not native promise available | ||
// but only in node <= 0.11.12 | ||
relike.promise = require('pinkie') | ||
const promise = relike(fs.readFile, 'package.json', 'utf8') | ||
console.log(promise.Prome) | ||
//=> will be `pinkie` promise constructor (only in node <= 0.11.12) | ||
console.log(promise.Prome.___customPromise) //=> true (only on node <= 0.11.12) | ||
console.log(promise.___customPromise) //=> true (only on node <= 0.11.12) | ||
promise | ||
.then(JSON.parse) | ||
.then(data => { | ||
console.log(data.name) //=> `relike` | ||
}) | ||
``` | ||
## Related | ||
- [always-done](https://github.com/hybridables/always-done): Handles completion and errors of anything! | ||
- [always-promise](https://github.com/hybridables/always-promise): Promisify, basically, **everything**. Generator function, callback-style or synchronous function; sync function that returns child process, stream or observable; directly passed promise, stream or child process. | ||
- [always-thunk](https://github.com/hybridables/always-thunk): Thunkify, basically, **everything**. Generator function, callback-style or synchronous function; sync function that returns child process, stream or observable; directly passed promise, stream or child process. | ||
- [always-generator](https://github.com/hybridables/always-generator): Generatorify, basically, **everything**. Async, callback-style or synchronous function; sync function that returns child process, stream or observable; directly passed promise, stream or child process. | ||
- [native-or-another](https://github.com/tunnckoCore/native-or-another): Always will expose native `Promise` if available, otherwise `Bluebird` but only if you don't give another promise module like `q` or `promise` or what you want. | ||
- [native-promise](https://github.com/tunnckoCore/native-promise): Get native `Promise` or falsey value if not available. | ||
## Contributing | ||
@@ -24,0 +108,0 @@ Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/hybridables/relike/issues/new). |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11471
6
97
1
158
0
5
5
2
+ Addedhandle-arguments@^3.0.4
+ Addedis-async-function@^1.1.0
+ Addedlazy-cache@^1.0.3
+ Addednative-or-another@^3.0.1
+ Addedsliced@^1.0.1
+ Addedarr-includes@2.3.8(transitive)
+ Addedarrify@2.0.1(transitive)
+ Addedbluebird@3.7.2(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedcommon-callback-names@1.0.22.0.1(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedfn-name@2.0.1(transitive)
+ Addedfunction-arguments@1.0.9(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-fn-name@1.0.0(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhandle-arguments@3.1.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedin-array@0.1.2(transitive)
+ Addedis-arguments@1.1.1(transitive)
+ Addedis-async-function@1.3.0(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedis-callback-function@1.0.1(transitive)
+ Addedis-number@2.1.0(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedkind-of@3.2.2(transitive)
+ Addedlazy-cache@1.0.42.0.2(transitive)
+ Addednative-or-another@3.0.2(transitive)
+ Addednative-promise@1.0.1(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedset-getter@0.1.1(transitive)
+ Addedsliced@1.0.1(transitive)
+ Addedto-object-path@0.3.0(transitive)