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 sed #12

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
10 changes: 10 additions & 0 deletions lib/method-combinators.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ this.postcondition =
(predicate = throwable) and (throwable = 'Failed postcondition') unless predicate
this.after -> throw throwable unless predicate.apply(this, arguments)

# Edits parameters
this.sed =
(transform)->
(fromIndex = 0, toIndex)->
(base)->
(args...)->
toIndex ||= fromIndex
args[toIndex] = transform.call this, args[fromIndex]
base.apply this, args

# ## Asynchronous Method Combinators

this.async = do (async = undefined) ->
Expand Down
19 changes: 18 additions & 1 deletion lib/method-combinators.js

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

42 changes: 42 additions & 0 deletions spec/method-combinators.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,48 @@ describe "Method Combinators", ->
expect(-> sane.setSanity(true)).not.toThrow 'Failed precondition'
expect(-> sane.setSanity(false)).not.toThrow 'Failed precondition'

describe "sed", ->
it "should transform parameter at input index", ->
decorator = C.sed (username)->
username.toUpperCase()
class SedClazz
announce:
decorator(1) \
(place, username)->
"Welcome to #{place} #{username}!"

eg = new SedClazz()
announcement = eg.announce "Home", "Joe"
expect(announcement).toBe("Welcome to Home JOE!")

it "should default to 0", ->
decorator = C.sed -> @capitalize arguments[0]
class SedClazz
capitalize: (username)->
username.toUpperCase()
announce:
decorator() \
(username)->
"Welcome #{username}!"

eg = new SedClazz()
announcement = eg.announce "Joe"
expect(announcement).toBe("Welcome JOE!")

it "should allow param population of different index", ->
decorator = C.sed -> @capitalize arguments[0]
class SedClazz
capitalize: (username)->
username.toUpperCase()
details:
decorator(0,1) \
(username, password)->
"username:#{username}|password:#{password}"

eg = new SedClazz()
announcement = eg.details "joe"
expect(announcement).toBe("username:joe|password:JOE")

describe "Asynchronous Method Combinators", ->

a = undefined
Expand Down