[Pkg-javascript-commits] [node-universalify] 01/03: New upstream version 0.1.0
Julien Puydt
julien.puydt at laposte.net
Wed Jun 28 06:17:07 UTC 2017
This is an automated email from the git hooks/post-receive script.
jpuydt-guest pushed a commit to branch master
in repository node-universalify.
commit 70886197ee17d4bcd6c616849c1159cdd7dd4f0a
Author: Julien Puydt <julien.puydt at laposte.net>
Date: Wed Jun 28 08:10:13 2017 +0200
New upstream version 0.1.0
---
.gitignore | 44 +++++++++++++++++++++++++++++++
.travis.yml | 5 ++++
LICENSE | 20 ++++++++++++++
README.md | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++
index.js | 25 ++++++++++++++++++
package.json | 30 +++++++++++++++++++++
test/from-callback.js | 61 ++++++++++++++++++++++++++++++++++++++++++
test/from-promise.js | 65 +++++++++++++++++++++++++++++++++++++++++++++
8 files changed, 323 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..bc7fc55
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,44 @@
+# Logs
+logs
+*.log
+npm-debug.log*
+
+# Runtime data
+pids
+*.pid
+*.seed
+*.pid.lock
+
+# Directory for instrumented libs generated by jscoverage/JSCover
+lib-cov
+
+# Coverage directory used by tools like istanbul
+coverage
+
+# nyc test coverage
+.nyc_output
+
+# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
+.grunt
+
+# node-waf configuration
+.lock-wscript
+
+# Compiled binary addons (http://nodejs.org/api/addons.html)
+build/Release
+
+# Dependency directories
+node_modules
+jspm_packages
+
+# Optional npm cache directory
+.npm
+
+# Optional eslint cache
+.eslintcache
+
+# Optional REPL history
+.node_repl_history
+
+# Output of 'npm pack'
+*.tgz
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..b03397f
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,5 @@
+language: node_js
+node_js:
+ - "7"
+ - "6"
+ - "4"
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..514e84e
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,20 @@
+(The MIT License)
+
+Copyright (c) 2017, Ryan Zimmerman <opensrc at ryanzim.com>
+
+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.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..f5b5da6
--- /dev/null
+++ b/README.md
@@ -0,0 +1,73 @@
+# universalify
+
+[![Travis branch](https://img.shields.io/travis/RyanZim/universalify/master.svg)](https://travis-ci.org/RyanZim/universalify)
+
+Make a callback- or promise-based function support both promises and callbacks.
+
+Uses the native promise implementation.
+
+## Installation
+
+```bash
+npm install universalify
+```
+
+## API
+
+### `universalify.fromCallback(fn)`
+
+Takes a callback-based function to universalify, and returns the universalified function.
+
+Function must take a callback as the last parameter that will be called with the signature `(error, result)`. `universalify` does not support calling the callback with more than three arguments, and does not ensure that the callback is only called once.
+
+```js
+function callbackFn (n, cb) {
+ setTimeout(() => cb(null, n), 15)
+}
+
+const fn = universalify.fromCallback(callbackFn)
+
+// Works with Promises:
+fn('Hello World!')
+.then(result => console.log(result)) // -> Hello World!
+.catch(error => console.error(error))
+
+// Works with Callbacks:
+fn('Hi!', (error, result) => {
+ if (error) return console.error(error)
+ console.log(result)
+ // -> Hello World!
+})
+```
+
+### `universalify.fromPromise(fn)`
+
+Takes a promise-based function to universalify, and returns the universalified function.
+
+Function must return a valid JS promise. `universalify` does not ensure that a valid promise is returned.
+
+```js
+function promiseFn (n) {
+ return new Promise(resolve => {
+ setTimeout(() => resolve(n), 15)
+ })
+}
+
+const fn = universalify.fromPromise(promiseFn)
+
+// Works with Promises:
+fn('Hello World!')
+.then(result => console.log(result)) // -> Hello World!
+.catch(error => console.error(error))
+
+// Works with Callbacks:
+fn('Hi!', (error, result) => {
+ if (error) return console.error(error)
+ console.log(result)
+ // -> Hello World!
+})
+```
+
+## License
+
+MIT
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..e4dc180
--- /dev/null
+++ b/index.js
@@ -0,0 +1,25 @@
+'use strict'
+
+exports.fromCallback = function (fn) {
+ return Object.defineProperty(function () {
+ if (typeof arguments[arguments.length - 1] === 'function') fn.apply(this, arguments)
+ else {
+ return new Promise((resolve, reject) => {
+ arguments[arguments.length] = (err, res) => {
+ if (err) return reject(err)
+ resolve(res)
+ }
+ arguments.length++
+ fn.apply(this, arguments)
+ })
+ }
+ }, 'name', { value: fn.name })
+}
+
+exports.fromPromise = function (fn) {
+ return Object.defineProperty(function () {
+ const cb = arguments[arguments.length - 1]
+ if (typeof cb !== 'function') return fn.apply(this, arguments)
+ else fn.apply(this, arguments).then(r => cb(null, r)).catch(cb)
+ }, 'name', { value: fn.name })
+}
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..5c4b211
--- /dev/null
+++ b/package.json
@@ -0,0 +1,30 @@
+{
+ "name": "universalify",
+ "version": "0.1.0",
+ "description": "Make a callback- or promise-based function support both promises and callbacks.",
+ "keywords": [
+ "callback",
+ "native",
+ "promise"
+ ],
+ "homepage": "https://github.com/RyanZim/universalify#readme",
+ "bugs": "https://github.com/RyanZim/universalify/issues",
+ "license": "MIT",
+ "author": "Ryan Zimmerman <opensrc at ryanzim.com>",
+ "files": [
+ "index.js"
+ ],
+ "repository": {
+ "type": "git",
+ "url": "git+https://github.com/RyanZim/universalify.git"
+ },
+ "scripts": {
+ "test": "standard && nyc tape test/*.js | colortape"
+ },
+ "devDependencies": {
+ "colortape": "^0.1.2",
+ "nyc": "^10.2.0",
+ "standard": "^10.0.1",
+ "tape": "^4.6.3"
+ }
+}
diff --git a/test/from-callback.js b/test/from-callback.js
new file mode 100644
index 0000000..d8e908b
--- /dev/null
+++ b/test/from-callback.js
@@ -0,0 +1,61 @@
+'use strict'
+const test = require('tape')
+const universalify = require('..')
+
+const fn = universalify.fromCallback(function (a, b, cb) {
+ setTimeout(() => cb(null, [this, a, b]), 15)
+})
+
+const errFn = universalify.fromCallback(function (cb) {
+ setTimeout(() => cb(new Error('test')), 15)
+})
+
+test('callback function works with callbacks', t => {
+ t.plan(4)
+ fn.call({a: 'a'}, 1, 2, (err, arr) => {
+ t.ifError(err, 'should not error')
+ t.is(arr[0].a, 'a')
+ t.is(arr[1], 1)
+ t.is(arr[2], 2)
+ t.end()
+ })
+})
+
+test('callback function works with promises', t => {
+ t.plan(3)
+ fn.call({a: 'a'}, 1, 2)
+ .then(arr => {
+ t.is(arr[0].a, 'a')
+ t.is(arr[1], 1)
+ t.is(arr[2], 2)
+ t.end()
+ })
+ .catch(t.end)
+})
+
+test('callback function error works with callbacks', t => {
+ t.plan(2)
+ errFn(err => {
+ t.assert(err, 'should error')
+ t.is(err.message, 'test')
+ t.end()
+ })
+})
+
+test('callback function error works with promises', t => {
+ t.plan(2)
+ errFn()
+ .then(arr => t.end('Promise should not resolve'))
+ .catch(err => {
+ t.assert(err, 'should error')
+ t.is(err.message, 'test')
+ t.end()
+ })
+})
+
+test('fromCallback() sets correct .name', t => {
+ t.plan(1)
+ const res = universalify.fromCallback(function hello () {})
+ t.is(res.name, 'hello')
+ t.end()
+})
diff --git a/test/from-promise.js b/test/from-promise.js
new file mode 100644
index 0000000..ede45f4
--- /dev/null
+++ b/test/from-promise.js
@@ -0,0 +1,65 @@
+'use strict'
+const test = require('tape')
+const universalify = require('..')
+
+const fn = universalify.fromPromise(function (a, b, cb) {
+ return new Promise(resolve => {
+ setTimeout(() => resolve([this, a, b]), 15)
+ })
+})
+
+const errFn = universalify.fromPromise(function () {
+ return new Promise((resolve, reject) => {
+ setTimeout(() => reject(new Error('test')), 15)
+ })
+})
+
+test('promise function works with callbacks', t => {
+ t.plan(4)
+ fn.call({a: 'a'}, 1, 2, (err, arr) => {
+ t.ifError(err, 'should not error')
+ t.is(arr[0].a, 'a')
+ t.is(arr[1], 1)
+ t.is(arr[2], 2)
+ t.end()
+ })
+})
+
+test('promise function works with promises', t => {
+ t.plan(3)
+ fn.call({a: 'a'}, 1, 2)
+ .then(arr => {
+ t.is(arr[0].a, 'a')
+ t.is(arr[1], 1)
+ t.is(arr[2], 2)
+ t.end()
+ })
+ .catch(t.end)
+})
+
+test('promise function error works with callbacks', t => {
+ t.plan(2)
+ errFn(err => {
+ t.assert(err, 'should error')
+ t.is(err.message, 'test')
+ t.end()
+ })
+})
+
+test('promise function error works with promises', t => {
+ t.plan(2)
+ errFn()
+ .then(arr => t.end('Promise should not resolve'))
+ .catch(err => {
+ t.assert(err, 'should error')
+ t.is(err.message, 'test')
+ t.end()
+ })
+})
+
+test('fromPromise() sets correct .name', t => {
+ t.plan(1)
+ const res = universalify.fromPromise(function hello () {})
+ t.is(res.name, 'hello')
+ t.end()
+})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-universalify.git
More information about the Pkg-javascript-commits
mailing list