Comparing version 0.1.0 to 1.0.0
// breakable.js | ||
// MIT licensed, see LICENSE file | ||
// Copyright (c) 2013 Olov Lassus <olov.lassus@gmail.com> | ||
// Copyright (c) 2013-2014 Olov Lassus <olov.lassus@gmail.com> | ||
@@ -8,15 +8,19 @@ var breakable = (function() { | ||
function Val(val) { | ||
function Val(val, brk) { | ||
this.val = val; | ||
this.brk = brk; | ||
} | ||
function brk(val) { | ||
throw new Val(val); | ||
function make_brk() { | ||
return function brk(val) { | ||
throw new Val(val, brk); | ||
}; | ||
} | ||
function breakable(fn) { | ||
var brk = make_brk(); | ||
try { | ||
return fn(brk); | ||
} catch (e) { | ||
if (e instanceof Val) { | ||
if (e instanceof Val && e.brk === brk) { | ||
return e.val; | ||
@@ -28,6 +32,2 @@ } | ||
breakable.fn = function breakablefn(fn) { | ||
return breakable.bind(null, fn); | ||
}; | ||
return breakable; | ||
@@ -34,0 +34,0 @@ })(); |
{ | ||
"name": "breakable", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "Break out of functions, recursive or not, in a more composable way than by using exceptions explicitly. Non-local return.", | ||
@@ -5,0 +5,0 @@ "main": "breakable.js", |
@@ -35,6 +35,6 @@ "use strict"; | ||
test("simple-throws", function(t) { | ||
t.throws(breakable.fn(function(brk) { | ||
t.throws(breakable.bind(null, function(brk) { | ||
throw 1; | ||
})); | ||
t.doesNotThrow(breakable.fn(function(brk) { | ||
t.doesNotThrow(breakable.bind(null, function(brk) { | ||
brk(1); | ||
@@ -98,1 +98,23 @@ throw 2; | ||
}); | ||
test("nested-breakables-1", function(t) { | ||
var res = breakable(function(brk1) { | ||
breakable(function(brk2) { | ||
brk1(13); | ||
}); | ||
}); | ||
t.equal(res, 13); | ||
t.end(); | ||
}); | ||
test("nested-breakables-2", function(t) { | ||
t.plan(2); | ||
var res1 = breakable(function(brk1) { | ||
var res2 = breakable(function(brk2) { | ||
brk2(13); | ||
}); | ||
t.equal(res2, 13); | ||
}); | ||
t.equal(res1, undefined); | ||
t.end(); | ||
}); |
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
7911
166
1