diff --git a/README.md b/README.md index ac7b984..e4e6dee 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,23 @@ s.step('query api', function ($, next) { }); ``` +### Stairs#step(title:String, exclude:Boolean, fn:Function) + +Conditionally adds a step. + +```javascript +s.step('query api', options.skipQuery, function ($, next) { + http.get($.url, function (res) { + $.body = ''; + res.on('data', function (chunk) { $.body = $.body + chunk; }); + res.on('end', next); + res.on('error', next); + }); +}); +``` + +The "query api" step will not be added if `options.skipQuery` is true. + ### Stairs.Context#skip(title:String) You can skip to a particular step given the `title` of that step by calling `this.skip('skip to')`. diff --git a/lib/index.js b/lib/index.js index 8d611ff..6dba077 100644 --- a/lib/index.js +++ b/lib/index.js @@ -134,16 +134,27 @@ Stairs.prototype.run = function () { * @param {Function} fn */ -Stairs.prototype.step = function (title, fn) { - if (typeof title === 'function') { +Stairs.prototype.step = function (title, exclude, fn) { + if (arguments.length === 1) { fn = title; + exclude = false; title = 'Untitled Step ' + (this.steps().length); + } else if (arguments.length === 2) { + fn = exclude; + if (typeof title === 'boolean') { + exclude = title; + title = 'Untitled Step ' + (this.steps().length); + } else { + exclude = false; + } } if (typeof fn !== 'function') { throw new TypeError('fn must be a function'); } - fn.title = title; - this.steps().push(fn); + if (!exclude) { + fn.title = title; + this.steps().push(fn); + } return this; }; diff --git a/package.json b/package.json index 7a6fdc0..d4fbd6e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "stairs", - "version": "0.2.0", + "version": "0.3.0", "description": "Organize your application's steps into stairs.", "main": "index.js", "scripts": { diff --git a/spec/lib/index-spec.coffee b/spec/lib/index-spec.coffee index ae3add6..49496f0 100644 --- a/spec/lib/index-spec.coffee +++ b/spec/lib/index-spec.coffee @@ -77,6 +77,18 @@ describe 'lib', -> When -> @res = @stairs.step @title, @fn Then -> expect(@fn.title).toBe @title And -> expect(@steps[0]).toBe @fn + + describe '#step(title:String,exclude:Boolean,fn:Function)', -> + + Given -> @title1 = 'Step 1' + Given -> @fn1 = -> + Given -> @title2 = 'Step 2' + Given -> @fn2 = -> + Given -> @steps = [] + Given -> spyOn(@stairs, 'steps').andReturn @steps + When -> @res = @stairs.step(@title1, true, @fn1).step(@title2, @fn2) + Then -> expect(@fn2.title).toBe @title2 + And -> expect(@steps[0]).toBe @fn2 describe '#steps', ->