Files
30-seconds-of-code/node_modules/parse-latin/lib/plugin/merge-non-word-sentences.js
2019-08-20 15:52:05 +02:00

53 lines
1.1 KiB
JavaScript

'use strict'
var modifyChildren = require('unist-util-modify-children')
module.exports = modifyChildren(mergeNonWordSentences)
// Merge a sentence into the following sentence, when the sentence does not
// contain word tokens.
function mergeNonWordSentences(child, index, parent) {
var children = child.children
var position = -1
var prev
var next
while (children[++position]) {
if (children[position].type === 'WordNode') {
return
}
}
prev = parent.children[index - 1]
if (prev) {
prev.children = prev.children.concat(children)
// Remove the child.
parent.children.splice(index, 1)
// Patch position.
if (prev.position && child.position) {
prev.position.end = child.position.end
}
// Next, iterate over the node *now* at the current position (which was the
// next node).
return index
}
next = parent.children[index + 1]
if (next) {
next.children = children.concat(next.children)
// Patch position.
if (next.position && child.position) {
next.position.start = child.position.start
}
// Remove the child.
parent.children.splice(index, 1)
}
}