Skip to content

Commit

Permalink
feat: add nextTo method, close #5
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Oct 28, 2017
1 parent 6ce2e49 commit bc509f5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
27 changes: 22 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ chdir.to('foo/bar/folder')
// resolved with value 'foo'
```

Implemented using [q][q], [spots][spots], [lazy-ass][lazy-ass] and
[check-more-types][check-more-types].

**Advice**

Put `chdir.back` into `.finally` callback to make sure it is always executed, even if there is an exception.
Expand All @@ -43,8 +40,28 @@ chdir.to('foo/bar/folder')
.finally(chdir.back); // called
```

[q]: https://www.npmjs.com/package/q
[spots]: https://www.npmjs.com/package/spots
### nextTo

If you have several hops you can schedule next hop using `chdir.nextTo` which allows
you to save extra empty function.

```js
// second hop is deferred
chdir.to('first/folder')
.then(chdir.back)
.to(() => chdir.to('second/folder'))
```

```js
// equivalent
chdir.to('first/folder')
.then(chdir.back)
.to(chdir.nextTo('second/folder'))
```

Implemented using [bluebird][https://github.com/petkaantonov/bluebird],
[lazy-ass][lazy-ass] and [check-more-types][check-more-types].

[lazy-ass]: https://www.npmjs.com/package/lazy-ass
[check-more-types]: https://www.npmjs.com/package/check-more-types

Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ const chdirTo = folderName => {
return Promise.try(() => _to(folderName))
}

const nextTo = folderName => () => chdirTo(folderName)

module.exports = {
to: chdirTo,
back: comeBack,
from: comeBack
from: comeBack,
nextTo: nextTo
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"main": "index.js",
"scripts": {
"test": "echo running unit tests",
"posttest": "mocha test/*-spec.js",
"posttest": "npm run unit",
"unit": "mocha test/*-spec.js",
"demo": "DEBUG=chdir-promise node test/demo.js",
"deps": "deps-ok && dependency-check --no-dev .",
"semantic-release": "semantic-release",
Expand Down
27 changes: 27 additions & 0 deletions test/chdir-promise-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,31 @@ describe('chdir-promise', () => {
})
})
})

context('nextTo', () => {
it('is a function', () => {
la(is.fn(chdir.nextTo))
})

it('helps hop twice', () =>
chdir
.to(__dirname)
.then(here)
.then(chdir.back)
.then(inOriginal)
.then(chdir.nextTo(__dirname))
.then(here)
.then(chdir.back)
.then(inOriginal))

it('helps hop twice and twice back', () =>
chdir
.to(__dirname)
.then(chdir.nextTo(originalFolder))
.then(inOriginal)
.then(chdir.back)
.then(here)
.then(chdir.back)
.then(inOriginal))
})
})

0 comments on commit bc509f5

Please sign in to comment.