diff --git a/README.md b/README.md index dbc7f46..eeb7a8e 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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 diff --git a/index.js b/index.js index 666609c..b4faa45 100644 --- a/index.js +++ b/index.js @@ -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 } diff --git a/package.json b/package.json index 5018e0f..48bfaec 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/chdir-promise-spec.js b/test/chdir-promise-spec.js index ac2472c..961f07d 100644 --- a/test/chdir-promise-spec.js +++ b/test/chdir-promise-spec.js @@ -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)) + }) })