Skip to content

Commit

Permalink
implement 🐈
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Sep 29, 2015
1 parent 5ee32ab commit 1276819
Show file tree
Hide file tree
Showing 10 changed files with 417 additions and 4 deletions.
26 changes: 26 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# .editorconfig <https://github.com/tunnckoCore/dotfiles>
#
# Copyright (c) 2015 Charlike Mike Reagent, contributors.
# Released under the MIT license.
#

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space

[*.js]
insert_final_newline = true
trim_trailing_whitespace = true

[*.php]
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
insert_final_newline = false
trim_trailing_whitespace = false
57 changes: 57 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# .gitignore <https://github.com/tunnckoCore/dotfiles>
#
# Copyright (c) 2015 Charlike Mike Reagent, contributors.
# Released under the MIT license.
#

# Always-ignore dirs #
# ####################
_gh_pages
node_modules
bower_components
components
vendor
build
dest
dist
src
lib-cov
coverage
nbproject
cache
temp
tmp
redolent

# Packages #
# ##########
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip

# OS, Logs and databases #
# #########################
*.pid
*.dat
*.log
*.sql
*.sqlite
*~
~*

# Another files #
# ###############
Icon?
.DS_Store*
Thumbs.db
ehthumbs.db
Desktop.ini
npm-debug.log
.directory
._*
lcov.info
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
language: "node_js"
sudo: false

node_js:
- "0.10"
- "0.12"
- "4"

notifications:
email:
on_success: never
on_failure: never

before_script:
- npm install standard
- standard

script:
- npm install istanbul-harmony
- node --harmony node_modules/.bin/istanbul cover test.js

after_success:
- npm install coveralls
- cat coverage/lcov.info | coveralls
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


## 0.0.0 - 2015-09-28
- Initial commit
32 changes: 32 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Contributing Guidelines

Contributions are always welcome!

**Before spending lots of time on something, ask for feedback on your idea first!**

Please search issues and pull requests before adding something new to avoid duplicating efforts and conversations.


## Installing

Fork and clone the repo, then `npm install` to install all dependencies and `npm test` to ensure all is okey before you start anything.


## Testing

Tests are run with `npm test`. Please ensure all tests are passing before submitting a pull request (unless you're creating a failing test to increase test coverage or show a problem).

## Code Style

[![standard][standard-image]][standard-url]

This repository uses [`standard`][standard-url] to maintain code style and consistency, and to avoid style arguments. You are encouraged to install it globally. `npm test` runs `standard` so you don't have to!

```
npm i standard -g
```

It is intentional to don't have `standard`, `istanbul` and `coveralls` in the devDependencies. Travis will handle all that stuffs. That approach will save bandwidth also installing and development time.

[standard-image]: https://cdn.rawgit.com/feross/standard/master/badge.svg
[standard-url]: https://github.com/feross/standard
3 changes: 1 addition & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ 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.

SOFTWARE.
109 changes: 107 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,107 @@
# redolent
Simple promisify a callback-style function with sane defaults and options.
# [redolent][author-www-url] [![npmjs.com][npmjs-img]][npmjs-url] [![The MIT License][license-img]][license-url]

> Simple promisify a callback-style function with sane defaults.
[![code climate][codeclimate-img]][codeclimate-url] [![standard code style][standard-img]][standard-url] [![travis build status][travis-img]][travis-url] [![coverage status][coveralls-img]][coveralls-url] [![dependency status][david-img]][david-url]


## Install
```
npm i redolent --save
```


## Usage
> For more use-cases see the [tests](./test.js)
```js
const redolent = require('redolent')
```

### [redolent](./index.js#L38)
> Will try to promisify `fn` with native Promise, otherwise will use `Bluebird`
or you can give different promise module as `Prome`, for example `pinkie`.

- `<fn>` **{Function}** callback-style function to promisify
- `[Prome]` **{Function}** custom Promise module to use, e.g. `Q`
- `return` **{Function}** promisified function

**Example**

```js
const fs = require('fs')
const request = require('request')
const redolent = require('redolent')

redolent(fs.readFile)('package.json', 'utf-8').then(data => {
console.log(JSON.parse(data).name)
})

// handles multiple arguments by default
redolent(request)('http://www.tunnckocore.tk/').then(result => {
const [httpResponse, body] = result
})
```


## Related
- [always-done](https://github.com/tunnckoCore/always-done): Handles completion and errors of anything!
- [always-promise](https://github.com/tunnckoCore/always-promise): Promisify basically **everything**.
- [always-thunk](https://github.com/tunnckoCore/always-thunk): Create thunk from **anything**, works like `thunkify`. Transforms anything (callbacks, streams, promises, observables, child processes, sync and generator functions) to thunk.
- [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
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/tunnckoCore/redolent/issues/new).
But before doing anything, please read the [CONTRIBUTING.md](./CONTRIBUTING.md) guidelines.


## [Charlike Make Reagent](http://j.mp/1stW47C) [![new message to charlike][new-message-img]][new-message-url] [![freenode #charlike][freenode-img]][freenode-url]

[![tunnckocore.tk][author-www-img]][author-www-url] [![keybase tunnckocore][keybase-img]][keybase-url] [![tunnckoCore npm][author-npm-img]][author-npm-url] [![tunnckoCore twitter][author-twitter-img]][author-twitter-url] [![tunnckoCore github][author-github-img]][author-github-url]


[npmjs-url]: https://www.npmjs.com/package/redolent
[npmjs-img]: https://img.shields.io/npm/v/redolent.svg?label=redolent

[license-url]: https://github.com/tunnckoCore/redolent/blob/master/LICENSE.md
[license-img]: https://img.shields.io/badge/license-MIT-blue.svg


[codeclimate-url]: https://codeclimate.com/github/tunnckoCore/redolent
[codeclimate-img]: https://img.shields.io/codeclimate/github/tunnckoCore/redolent.svg

[travis-url]: https://travis-ci.org/tunnckoCore/redolent
[travis-img]: https://img.shields.io/travis/tunnckoCore/redolent.svg

[coveralls-url]: https://coveralls.io/r/tunnckoCore/redolent
[coveralls-img]: https://img.shields.io/coveralls/tunnckoCore/redolent.svg

[david-url]: https://david-dm.org/tunnckoCore/redolent
[david-img]: https://img.shields.io/david/tunnckoCore/redolent.svg

[standard-url]: https://github.com/feross/standard
[standard-img]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg


[author-www-url]: http://www.tunnckocore.tk
[author-www-img]: https://img.shields.io/badge/www-tunnckocore.tk-fe7d37.svg

[keybase-url]: https://keybase.io/tunnckocore
[keybase-img]: https://img.shields.io/badge/keybase-tunnckocore-8a7967.svg

[author-npm-url]: https://www.npmjs.com/~tunnckocore
[author-npm-img]: https://img.shields.io/badge/npm-~tunnckocore-cb3837.svg

[author-twitter-url]: https://twitter.com/tunnckoCore
[author-twitter-img]: https://img.shields.io/badge/twitter-@tunnckoCore-55acee.svg

[author-github-url]: https://github.com/tunnckoCore
[author-github-img]: https://img.shields.io/badge/github-@tunnckoCore-4183c4.svg

[freenode-url]: http://webchat.freenode.net/?channels=charlike
[freenode-img]: https://img.shields.io/badge/freenode-%23charlike-5654a4.svg

[new-message-url]: https://github.com/tunnckoCore/ama
[new-message-img]: https://img.shields.io/badge/ask%20me-anything-green.svg
63 changes: 63 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*!
* redolent <https://github.com/tunnckoCore/redolent>
*
* Copyright (c) 2015 Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)
* Released under the MIT license.
*/

'use strict'

/**
* Will try to promisify `fn` with native Promise,
* otherwise will use `Bluebird` or you can give
* different promise module as `Prome`, for example `pinkie`.
*
* **Example**
*
* ```js
* const fs = require('fs')
* const request = require('request')
* const redolent = require('redolent')
*
* redolent(fs.readFile)('package.json', 'utf-8').then(data => {
* console.log(JSON.parse(data).name)
* })
*
* // handles multiple arguments by default
* redolent(request)('http://www.tunnckocore.tk/').then(result => {
* const [httpResponse, body] = result
* })
* ```
*
* @name redolent
* @param {Function} `<fn>` callback-style function to promisify
* @param {Function} `[Prome]` custom Promise module to use, e.g. `Q`
* @return {Function} promisified function
* @api public
*/
module.exports = function redolent (fn, Prome) {
if (typeof fn !== 'function') {
throw new TypeError('redolent expect a function')
}

var self = this
return function promisify () {
var sliced = require('sliced')
var handle = require('handle-arguments')
var argz = handle(arguments)
var ctx = self || this

Prome = require('native-or-another')(Prome)
var promise = new Prome(function prome (resolve, reject) {
fn.apply(ctx, argz.args.concat(function cb (err, res) {
if (err) return reject(err)
if (arguments.length > 2) res = sliced(arguments, 1)
resolve(res)
}))
})

promise.___customPromise = Prome.___customPromise
promise.___bluebirdPromise = Prome.___bluebirdPromise
return promise
}
}
39 changes: 39 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"name": "redolent",
"version": "0.0.0",
"description": "Simple promisify a callback-style function with sane defaults.",
"repository": "tunnckoCore/redolent",
"author": "Charlike Mike Reagent <@tunnckoCore> (http://www.tunnckocore.tk)",
"main": "index.js",
"license": "MIT",
"scripts": {
"test": "standard && node test.js"
},
"dependencies": {
"handle-arguments": "^3.0.4",
"native-or-another": "^3.0.1",
"sliced": "^1.0.1"
},
"devDependencies": {
"assertit": "^0.1.0",
"is-buffer": "^1.1.0",
"isarray": "0.0.1",
"pinkie": "^1.0.0",
"semver": "^5.0.3"
},
"files": [
"index.js"
],
"keywords": [
"anything",
"bluebird",
"callback",
"defaults",
"everywhere",
"native",
"native-promise",
"promise",
"promisify",
"simple"
]
}
Loading

0 comments on commit 1276819

Please sign in to comment.