Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Merge branch 'release-1.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Jun 18, 2016
2 parents c97d080 + 5e515aa commit ca42235
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 1 deletion.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<a name="1.0.2"></a>
## 1.0.2 (2016-06-18)


### Features

* **markdown:** expose marked renderer and option to wrap toc inside a div([05e1b6b](https://github.com/poppinss/docketjs/commit/05e1b6b))
* **menu:** add methods to pull next and prev childs for a permanlink([ebcf9e7](https://github.com/poppinss/docketjs/commit/ebcf9e7))



<a name="1.0.1"></a>
## 1.0.1 (2016-06-11)

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,26 @@ const routing = menu.getChild('routing')
*/
```

#### getPrevious(tree, peramlink)

Returns previous child for a given `permalink`. It is important to pass a sorted tree to this method, since the previous item inside the unsorted tree can be different from the sorted tree.

```javascript
menu.load('menu.json')
const tree = menu.tree()
const previousChild = menu.getPreviousChild(tree, 'routing')
```

#### getNextChild(tree, peramlink)

Returns next child for a given `permalink`. It is important to pass a sorted tree to this method, since the next item inside the unsorted tree can be different from the sorted tree.

```javascript
menu.load('menu.json')
const tree = menu.tree()
const nextChild = menu.getNextChild(tree, 'routing')
```


## Github Reader

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "docketjs",
"version": "1.0.1",
"version": "1.0.2",
"description": "DocketJs is a battery included Markdown to HTML converter for Node.js. It has everything you need to successfully convert a bunch of markdown files to HTML and generate toc and a menu file.",
"main": "index.js",
"scripts": {
Expand Down
67 changes: 67 additions & 0 deletions src/Menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,73 @@ class Menu {
.first()
}

/**
* returns nearest child from a leaf or a fallback leaf
* in a given direction
*
* @param {Array} childs
* @param {String} permalink
* @param {Array} fallbackChilds
* @param {String} direction [prev, next]
*
* @return {Object|Null}
*
* @private
*/
_getNearestChild (childs, permalink, fallbackChilds, direction) {
const childIndex = _.findIndex(childs, {permalink})
if (childIndex < 0) {
return null
}
if (direction === 'prev' && childIndex === 0) {
return fallbackChilds ? _.last(fallbackChilds) : null
}
if (direction === 'next' && (childIndex + 1) === _.size(childs)) {
return fallbackChilds ? _.first(fallbackChilds) : null
}
return direction === 'prev' ? childs[childIndex - 1] : childs[childIndex + 1]
}

/**
* returns previous child for a given permalink
*
* @param {Object} tree
* @param {String} permalink
*
* @return {Object}
*
* @public
*/
getPreviousChild (tree, permalink) {
const treeLeafs = _.keys(tree)
return _(treeLeafs)
.map((leaf, index) => {
const childs = tree[leaf]
const previousChilds = tree[treeLeafs[index - 1]]
return this._getNearestChild(childs, permalink, previousChilds, 'prev')
}).compact().first() || null
}

/**
* returns next child for a given permalink
*
* @param {Object} tree
* @param {String} permalink
*
* @return {Object}
*
* @public
*/
getNextChild (tree, permalink) {
const treeLeafs = _.keys(tree)
return _(treeLeafs)
.map((leaf, index) => {
const childs = tree[leaf]
const previousChilds = tree[treeLeafs[index + 1]]
return this._getNearestChild(childs, permalink, previousChilds, 'next')
}).compact().first() || null
}

/**
* returns a sorted tree of optionally filtered or
* sorted categories
Expand Down
116 changes: 116 additions & 0 deletions test/menu.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,120 @@ describe('Menu', function () {
const contents = yield Q.nfcall(fsExtra.readJSON, menuFilePath)
expect(contents).deep.equal(menu.items)
})

it('should return previous child to the permalink', function * () {
const menu = new Menu()
const menuTree = {
basics: [
{
permalink: 'introduction'
},
{
permalink: 'routing'
}
]
}
const previous = menu.getPreviousChild(menuTree, 'routing')
expect(previous).deep.equal({permalink: 'introduction'})
})

it('should return child from the previous category when permalink is the first child', function * () {
const menu = new Menu()
const menuTree = {
basics: [
{
permalink: 'introduction'
},
{
permalink: 'routing'
}
],
database: [
{
permalink: 'database-setup'
}
]
}
const previous = menu.getPreviousChild(menuTree, 'database-setup')
expect(previous).deep.equal({permalink: 'routing'})
})

it('should null when first child of the first leaf is passed as permalink', function * () {
const menu = new Menu()
const menuTree = {
basics: [
{
permalink: 'introduction'
},
{
permalink: 'routing'
}
],
database: [
{
permalink: 'database-setup'
}
]
}
const previous = menu.getPreviousChild(menuTree, 'introduction')
expect(previous).to.equal(null)
})

it('should return next child to the permalink', function * () {
const menu = new Menu()
const menuTree = {
basics: [
{
permalink: 'introduction'
},
{
permalink: 'routing'
}
]
}
const next = menu.getNextChild(menuTree, 'introduction')
expect(next).deep.equal({permalink: 'routing'})
})

it('should return child from the next category when permalink is the last child', function * () {
const menu = new Menu()
const menuTree = {
basics: [
{
permalink: 'introduction'
},
{
permalink: 'routing'
}
],
database: [
{
permalink: 'database-setup'
}
]
}
const next = menu.getNextChild(menuTree, 'routing')
expect(next).deep.equal({permalink: 'database-setup'})
})

it('should null when last child of the last leaf is passed as permalink', function * () {
const menu = new Menu()
const menuTree = {
basics: [
{
permalink: 'introduction'
},
{
permalink: 'routing'
}
],
database: [
{
permalink: 'database-setup'
}
]
}
const next = menu.getNextChild(menuTree, 'database-setup')
expect(next).to.equal(null)
})
})

0 comments on commit ca42235

Please sign in to comment.