[Pkg-javascript-commits] [node-iferr] 01/03: Import Upstream version 0.1.5

Saravanan Palanisamy saravanan30erd-guest at moszumanska.debian.org
Sat Jul 1 18:27:41 UTC 2017


This is an automated email from the git hooks/post-receive script.

saravanan30erd-guest pushed a commit to branch master
in repository node-iferr.

commit f7eef930f4dee01b1e6d39a09e53c375dc975ccc
Author: saravanan30erd <saravanan30erd at gmail.com>
Date:   Sat Jul 1 14:07:45 2017 -0400

    Import Upstream version 0.1.5
---
 .npmignore        |  1 +
 LICENSE           | 21 +++++++++++++++++++++
 README.md         | 40 ++++++++++++++++++++++++++++++++++++++++
 index.coffee      | 24 ++++++++++++++++++++++++
 index.js          | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 package.json      | 28 ++++++++++++++++++++++++++++
 test/index.coffee | 42 ++++++++++++++++++++++++++++++++++++++++++
 test/mocha.opts   |  2 ++
 8 files changed, 207 insertions(+)

diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1 @@
+node_modules
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..19d5f4b
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2014 Nadav Ivgi
+
+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.
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..0940763
--- /dev/null
+++ b/README.md
@@ -0,0 +1,40 @@
+# iferr
+
+Higher-order functions for easier error handling.
+
+`if (err) return cb(err);` be gone!
+
+## Install
+```bash
+npm install iferr
+```
+
+## Use
+
+### JavaScript example
+```js
+var iferr = require('iferr');
+
+function get_friends_count(id, cb) {
+  User.load_user(id, iferr(cb, function(user) {
+    user.load_friends(iferr(cb, function(friends) {
+      cb(null, friends.length);
+    }));
+  }));
+}
+```
+
+### CoffeeScript example
+```coffee
+iferr = require 'iferr'
+
+get_friends_count = (id, cb) ->
+  User.load_user id, iferr cb, (user) ->
+    user.load_friends iferr cb, (friends) ->
+      cb null, friends.length
+```
+
+(TODO: document tiferr, throwerr and printerr)
+
+## License
+MIT
diff --git a/index.coffee b/index.coffee
new file mode 100644
index 0000000..da6d007
--- /dev/null
+++ b/index.coffee
@@ -0,0 +1,24 @@
+# Delegates to `succ` on sucecss or to `fail` on error
+# ex: Thing.load 123, iferr cb, (thing) -> ...
+iferr = (fail, succ) -> (err, a...) ->
+  if err? then fail err
+  else succ? a...
+
+# Like iferr, but also catches errors thrown from `succ` and passes to `fail`
+tiferr = (fail, succ) -> iferr fail, (a...) ->
+  try succ a...
+  catch err then fail err
+
+# Delegate to the success function on success, or throw the error otherwise
+# ex: Thing.load 123, throwerr (thing) -> ...
+throwerr = iferr.bind null, (err) -> throw err
+
+# Prints errors when one is passed, or does nothing otherwise
+# ex: thing.save printerr
+printerr = iferr (err) -> console.error err.stack or err
+
+module.exports = exports = iferr
+exports.iferr = iferr
+exports.tiferr = tiferr
+exports.throwerr = throwerr
+exports.printerr = printerr
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..78fce3d
--- /dev/null
+++ b/index.js
@@ -0,0 +1,49 @@
+// Generated by CoffeeScript 1.7.1
+(function() {
+  var exports, iferr, printerr, throwerr, tiferr,
+    __slice = [].slice;
+
+  iferr = function(fail, succ) {
+    return function() {
+      var a, err;
+      err = arguments[0], a = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
+      if (err != null) {
+        return fail(err);
+      } else {
+        return typeof succ === "function" ? succ.apply(null, a) : void 0;
+      }
+    };
+  };
+
+  tiferr = function(fail, succ) {
+    return iferr(fail, function() {
+      var a, err;
+      a = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
+      try {
+        return succ.apply(null, a);
+      } catch (_error) {
+        err = _error;
+        return fail(err);
+      }
+    });
+  };
+
+  throwerr = iferr.bind(null, function(err) {
+    throw err;
+  });
+
+  printerr = iferr(function(err) {
+    return console.error(err.stack || err);
+  });
+
+  module.exports = exports = iferr;
+
+  exports.iferr = iferr;
+
+  exports.tiferr = tiferr;
+
+  exports.throwerr = throwerr;
+
+  exports.printerr = printerr;
+
+}).call(this);
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..9653654
--- /dev/null
+++ b/package.json
@@ -0,0 +1,28 @@
+{
+  "name": "iferr",
+  "version": "0.1.5",
+  "description": "Higher-order functions for easier error handling",
+  "main": "index.js",
+  "scripts": {
+    "test": "mocha",
+    "prepublish": "coffee -c index.coffee"
+  },
+  "repository": {
+    "type": "git",
+    "url": "https://github.com/shesek/iferr"
+  },
+  "keywords": [
+    "error",
+    "errors"
+  ],
+  "author": "Nadav Ivgi",
+  "license": "MIT",
+  "bugs": {
+    "url": "https://github.com/shesek/iferr/issues"
+  },
+  "homepage": "https://github.com/shesek/iferr",
+  "devDependencies": {
+    "coffee-script": "^1.7.1",
+    "mocha": "^1.18.2"
+  }
+}
diff --git a/test/index.coffee b/test/index.coffee
new file mode 100644
index 0000000..be0bc56
--- /dev/null
+++ b/test/index.coffee
@@ -0,0 +1,42 @@
+{ iferr, tiferr, throwerr } = require '../index.coffee'
+{ equal: eq, throws } = require 'assert'
+
+invoke_fail = (cb) -> cb new Error 'callback error'
+invoke_succ = (cb) -> cb null
+throw_error = -> throw new Error 'thrown'
+
+describe 'iferr', ->
+  it 'calls the error callback on errors', (done) ->
+    invoke_fail iferr(
+      (err) ->
+        eq err.message, 'callback error'
+        do done
+      ->
+        done new Error 'shouldn\'t call the success callback'
+    )
+
+  it 'calls the success callback on success', (done) ->
+    invoke_succ iferr(
+      -> done new Error 'shouldn\'t call the error callback'
+      done
+    )
+
+describe 'tiferr', ->
+  it 'catches errors in the success callback', (done) ->
+    invoke_succ tiferr(
+      (err) ->
+        eq err.message, 'thrown'
+        do done
+      throw_error
+    )
+
+describe 'throwerr', ->
+  it 'throws errors passed to the callback', (done)->
+    try invoke_fail throwerr ->
+      done 'shouldn\'t call the success callback'
+    catch err
+      eq err.message, 'callback error'
+      do done
+
+  it 'delegates to the success callback otherwise', (done) ->
+    invoke_succ throwerr done
diff --git a/test/mocha.opts b/test/mocha.opts
new file mode 100644
index 0000000..019defc
--- /dev/null
+++ b/test/mocha.opts
@@ -0,0 +1,2 @@
+--compilers coffee:coffee-script/register
+--reporter spec

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-iferr.git



More information about the Pkg-javascript-commits mailing list