Skip to content

Commit

Permalink
added the "skip" method
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanGRomano committed Jul 28, 2014
1 parent 55770c3 commit cbdf3e3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 19 deletions.
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ var extractData = stairs('extract data')
.step('parse json', function ($) {
try { $.data = JSON.parse($.body); }
catch(e) { return next(e) };
finally { this.next() }
finally { this.skip('skip to') }
})
.step('skip to', function ($, next) {
this.next();
})
.step('grab element', function ($, next) {
$.extracted = $.data.some.field;
Expand Down Expand Up @@ -160,15 +163,25 @@ s.step('query api', function ($, next) {
});
```

### Stairs.Context#next()
### Stairs.Context#skip(title:String)

You can invoke `next` from the callback parameter `next` or `this.next()`.
You can skip to a particular step given the `title` of that step by calling `this.skip('skip to')`.

```javascript
s.step('parse json', function ($, next) {
try { $.data = JSON.parse($.body); }
catch(e) { return next(e) };
finally { this.next() }
finally { this.skip('skip to') }
});
```

### Stairs.Context#next()

You can invoke `next` from the callback parameter `next` or `this.next()`.

```javascript
s.step('skip to', function ($, next) {
this.next();
});
```

Expand Down
5 changes: 4 additions & 1 deletion examples/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ var extractData = stairs('extract data')
console.log('the body \'%s\'', $.body);
try { $.data = JSON.parse($.body); }
catch(e) { return next(e) }
finally { this.next() }
finally { this.skip('skip to') }
})
.step('skip to', function ($, next) {
next();
})
.step('grab element', function ($, next) {
console.log('the data %j', $.data);
Expand Down
23 changes: 22 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Stairs.prototype.run = function () {
* @return void
*/

next: function next(err) {
next: function (err) {
process.nextTick(function () {
context.state.i++;
debug('next %s / %s', context.state.i + 1, context.state.steps.length);
Expand All @@ -96,6 +96,27 @@ Stairs.prototype.run = function () {
context.state.instance.emit('step', step.title, context.state.i + 1, context.state.steps.length);
step.apply(context.interface, context.state.scope.concat(context.interface.next));
});
},

/**
* Skips to a step given a name even if it is a previous step
*
* @api public
* @param {String} title The title of the step to skip
* @return void
*/

skip: function (title) {
var i = -1;
do {
if ('function' === typeof context.state.steps[i + 1] &&
context.state.steps[i + 1].title === title) {
context.state.i = i;
this.next();
break;
}
}
while (++i < context.state.steps.length);
}
}
};
Expand Down
43 changes: 30 additions & 13 deletions spec/lib/index-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,34 @@ describe 'lib', ->
And -> expect(@scope.b).toBe 1
And -> expect(@scope.c).toBe 2

describe '#end', ->

Given -> @a = (scope, next) -> scope.a = 1; next()
Given -> @b = (scope, next) -> scope.b = 1; @end(); next()
Given -> @c = (scope, next) -> scope.c = 2; next()
Given -> @stairs.step 'a', @a
Given -> @stairs.step 'b', @b
Given -> @stairs.step 'c', @c
Given -> @scope = {}
When (done) -> @stairs.run @scope, done
Then -> expect(@scope.a).toBe 1
And -> expect(@scope.b).toBe 1
And -> expect(@scope.c).toBe undefined
describe '.Context', ->

describe '#end', ->

Given -> @a = (scope, next) -> scope.a = 1; next()
Given -> @b = (scope, next) -> scope.b = 1; @end(); next()
Given -> @c = (scope, next) -> scope.c = 2; next()
Given -> @stairs.step 'a', @a
Given -> @stairs.step 'b', @b
Given -> @stairs.step 'c', @c
Given -> @scope = {}
When (done) -> @stairs.run @scope, done
Then -> expect(@scope.a).toBe 1
And -> expect(@scope.b).toBe 1
And -> expect(@scope.c).toBe undefined

describe '#skip', ->

Given -> @a = (scope, next) -> scope.a = 1; @skip 'c'
Given -> @b = (scope, next) -> scope.b = 1; next()
Given -> @c = (scope, next) -> scope.c = 2; next()
Given -> @stairs.step 'a', @a
Given -> @stairs.step 'b', @b
Given -> @stairs.step 'c', @c
Given -> @scope = {}
When (done) -> @stairs.run @scope, done
Then -> expect(@scope.a).toBe 1
And -> expect(@scope.b).toBe undefined
And -> expect(@scope.c).toBe 2


0 comments on commit cbdf3e3

Please sign in to comment.