summaryrefslogtreecommitdiffstats
path: root/lib/parse/progress.js
diff options
context:
space:
mode:
authorSamy Pessé <samypesse@gmail.com>2014-04-01 01:21:42 -0700
committerSamy Pessé <samypesse@gmail.com>2014-04-01 01:21:44 -0700
commit256d1ba35a74777b6bdd9d937809798a0cfe3c20 (patch)
tree7be827ab14a85c307afc40d6ff0a176b14116173 /lib/parse/progress.js
parentcc340db0faa412edec47a7d37ce591501ab454fb (diff)
downloadgitbook-256d1ba35a74777b6bdd9d937809798a0cfe3c20.zip
gitbook-256d1ba35a74777b6bdd9d937809798a0cfe3c20.tar.gz
gitbook-256d1ba35a74777b6bdd9d937809798a0cfe3c20.tar.bz2
Add progress calcul
Diffstat (limited to 'lib/parse/progress.js')
-rw-r--r--lib/parse/progress.js38
1 files changed, 38 insertions, 0 deletions
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