In the function takeNormal()
below, there is a line that I don't understand at all. It is:
var end = reduce(Math.min, text.length, map(indexOrEnd, ["*", "{"]));
Specifically, in this function reduce, it has all these different arguments (are they arguments?). How are they processed by reduce? Are they handled one after the other? Or, for example, does Math.min
do anything to text.length
? How do the different parts in reduce interact and what does reduce ultimately do to them?
function splitParagraph(text) {
function indexOrEnd(character) {
var index = text.indexOf(character);
return index == -1 ? text.length : index;
}
function takeNormal() {
var end = reduce(Math.min, text.length,
map(indexOrEnd, ["*", "{"]));
var part = text.slice(0, end);
text = text.slice(end);
return part;
}
function takeUpTo(character) {
var end = text.indexOf(character, 1);
if (end == -1)
throw new Error("Missing closing '" + character + "'");
var part = text.slice(1, end);
text = text.slice(end + 1);
return part;
}
var fragments = [];
while (text != "") {
if (text.charAt(0) == "*")
fragments.push({type: "emphasised",
content: takeUpTo("*")});
else if (text.charAt(0) == "{")
fragments.push({type: "footnote",
content: takeUpTo("}")});
else
fragments.push({type: "normal",
content: takeNormal()});
}
return fragments;
}