Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added branchOn combinator #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/method-combinators.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ this.provided =

# ## Extras

# Allows branching between functions depending on the result of a test function

this.branchOn =
(predicate)->
({failure, success})->
->
if predicate.apply(this, arguments)
success?.apply(this, arguments)
else
failure?.apply(this, arguments)

# If the method thows an error, retry it again a certain number of times.
# e.g. `retry(3) -> # doSomething as many as four times`
this.retry =
Expand Down
16 changes: 15 additions & 1 deletion lib/method-combinators.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions spec/method-combinators.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,23 @@ describe "Method Combinators", ->

expect(eg.foo).toBe('foo')

describe "branchOn", ->

it "runs success/fail function on resulting test", ->

decorator = C.branchOn (value)->
value

class BranchOnClazz
getValue:
decorator
success: -> "success"
failure: -> "failure"

eg = new BranchOnClazz()
expect(eg.getValue(true)).toBe('success')
expect(eg.getValue(false)).toBe('failure')

describe "retry", ->

describe 'times < 0', ->
Expand Down