diff options
Diffstat (limited to 'lib/parse')
-rw-r--r-- | lib/parse/index.js | 2 | ||||
-rw-r--r-- | lib/parse/progress.js | 38 |
2 files changed, 39 insertions, 1 deletions
diff --git a/lib/parse/index.js b/lib/parse/index.js index f6593ef..951e401 100644 --- a/lib/parse/index.js +++ b/lib/parse/index.js @@ -1,6 +1,6 @@ module.exports = { summary: require('./summary'), page: require('./page'), - + progress: require('./progress'), navigation: require('./navigation'), }; diff --git a/lib/parse/progress.js b/lib/parse/progress.js new file mode 100644 index 0000000..8088aa8 --- /dev/null +++ b/lib/parse/progress.js @@ -0,0 +1,38 @@ +var _ = require("lodash"); + +var calculProgress = function(navigation, current) { + var n = _.size(navigation); + var percent = 0; + var done = true; + + var chapters = _.chain(navigation) + .map(function(nav, path) { + nav.path = path; + return nav; + }) + .sortBy(function(nav) { + return nav.level; + }) + .map(function(nav, i) { + // Calcul percent + nav.percent = (i * 100) / Math.max((n - 1), 1); + + // Is it done + nav.done = done; + if (nav.path == current) { + percent = nav.percent; + done = false; + } + + return nav; + }) + .value(); + + + return { + percent: percent, + chapters: chapters + }; +} + +module.exports = calculProgress;
\ No newline at end of file |