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

47 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict'
var toString = require('nlcst-to-string')
var modifyChildren = require('unist-util-modify-children')
var expressions = require('../expressions')
module.exports = modifyChildren(mergeAffixSymbol)
// Closing or final punctuation, or terminal markers that should still be
// included in the previous sentence, even though they follow the sentences
// terminal marker.
var affixSymbol = expressions.affixSymbol
// Move certain punctuation following a terminal marker (thus in the next
// sentence) to the previous sentence.
function mergeAffixSymbol(child, index, parent) {
var children = child.children
var first
var second
var prev
if (children && children.length !== 0 && index !== 0) {
first = children[0]
second = children[1]
prev = parent.children[index - 1]
if (
(first.type === 'SymbolNode' || first.type === 'PunctuationNode') &&
affixSymbol.test(toString(first))
) {
prev.children.push(children.shift())
// Update position.
if (first.position && prev.position) {
prev.position.end = first.position.end
}
if (second && second.position && child.position) {
child.position.start = second.position.start
}
// Next, iterate over the previous node again.
return index - 1
}
}
}