Created
January 24, 2017 00:33
-
-
Save vjeux/627317f6d52afcae4dd536a4b7b0f0f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/doc-utils.js b/src/doc-utils.js | |
index 4d5149c..e2a9e0e 100644 | |
--- a/src/doc-utils.js | |
+++ b/src/doc-utils.js | |
@@ -1,77 +1,83 @@ | |
"use strict"; | |
+ | |
function traverseDoc(doc, onEnter, onExit) { | |
- if(onEnter) { | |
- onEnter(doc); | |
+ var hasStopped = false; | |
+ function traverseDocRec(doc) { | |
+ if (onEnter) { | |
+ hasStopped = hasStopped || onEnter(doc) === false; | |
+ } | |
+ if (hasStopped) { | |
+ return; | |
} | |
if (doc.type === "concat") { | |
for (var i = 0; i < doc.parts.length; i++) { | |
- traverseDoc(doc.parts[i], onEnter, onExit); | |
+ traverseDocRec(doc.parts[i]); | |
} | |
} else if (doc.type === "if-break") { | |
if (doc.breakContents) { | |
- traverseDoc(doc.breakContents, onEnter, onExit); | |
+ traverseDocRec(doc.breakContents); | |
} | |
if (doc.flatContents) { | |
- traverseDoc(doc.flatContents, onEnter, onExit); | |
+ traverseDocRec(doc.flatContents); | |
} | |
} else if (doc.contents) { | |
- traverseDoc(doc.contents, onEnter, onExit); | |
+ traverseDocRec(doc.contents); | |
} | |
- if(onExit) { | |
+ if (onExit) { | |
onExit(doc); | |
} | |
+ } | |
+ | |
+ traverseDocRec(doc); | |
+} | |
+ | |
+function findInDoc(doc, fn, defaultValue) { | |
+ var result = defaultValue; | |
+ traverseDoc(doc, function(doc) { | |
+ var maybeResult = fn(doc); | |
+ if (maybeResult !== undefined) { | |
+ result = maybeResult; | |
+ return false; | |
+ } | |
+ }); | |
+ return result; | |
} | |
+ | |
function isEmpty(n) { | |
return typeof n === "string" && n.length === 0; | |
} | |
function getFirstString(doc) { | |
- let firstString = null; | |
- traverseDoc(doc, doc => { | |
- if ( | |
- typeof doc === "string" && doc.trim().length !== 0 && firstString === null | |
- ) { | |
- firstString = doc; | |
+ return findInDoc(doc, doc => { | |
+ if (typeof doc === "string" && doc.trim().length !== 0) { | |
+ return doc; | |
} | |
- }); | |
- return firstString; | |
+ }, null); | |
} | |
function isLineNext(doc) { | |
- let flag = null; | |
- traverseDoc(doc, doc => { | |
- if(flag === null) { | |
+ return findInDoc(doc, doc => { | |
if (typeof doc === "string") { | |
- flag = false; | |
+ return false; | |
} | |
if (doc.type === "line") { | |
- flag = true; | |
+ return true; | |
} | |
- } | |
- }); | |
- return !!flag; | |
+ }, false); | |
} | |
function willBreak(doc) { | |
- let willBreak = false; | |
- traverseDoc(doc, doc => { | |
- switch (doc.type) { | |
- case "group": | |
- if(doc.break) { | |
- willBreak = true; | |
+ return findInDoc(doc, doc => { | |
+ if (doc.type === "group" && doc.break) { | |
+ return true; | |
} | |
- case "line": | |
- if (doc.hard) { | |
- willBreak = true; | |
+ if (doc.type === "line" && doc.hard) { | |
+ return true; | |
} | |
- | |
- break; | |
- } | |
- }); | |
- return willBreak; | |
+ }, false); | |
} | |
function breakParentGroup(groupStack) { | |
@@ -101,7 +107,7 @@ function propagateBreaks(doc) { | |
doc => { | |
if (doc.type === "group") { | |
const group = groupStack.pop(); | |
- if(group.break) { | |
+ if (group.break) { | |
breakParentGroup(groupStack); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment