Keep a closing bracket in the word when it closes one inside it

This commit is contained in:
dualfroz committed 2026-10-03 01:05:44 +02:00
1 parent ef0f63b489
commit db2bcfe31f
2 files changed
+34

No files matched your search

@@ -6,6 +6,21 @@ const isNumber = /[\p{Number}\p{Currency_Symbol}]/u
const hasAcronym = /^[a-z]\.([a-z]\.)+/i
const chillin = /[sn]['’]$/
const isFullNumber = /^[(+\-]?\d+(th|st|nd|rd)?[)+\-]?$/
const openers = { ')': '(', ']': '[', '}': '{' }
// does this closing bracket close one inside the word, like 'foo(bar)'
const closesInside = function (chars, start, end) {
const close = chars[end - 1]
let depth = 0
for (let i = start; i < end - 1; i += 1) {
if (chars[i] === openers[close]) {
depth += 1
} else if (chars[i] === close) {
depth -= 1
}
}
return depth > 0
}
const normalizePunctuation = function (str, model) {
// quick lookup for allowed pre/post punctuation
@@ -68,6 +83,9 @@ const normalizePunctuation = function (str, model) {
if ((c === '+' || c === ')') && isFullNumber.test(str.trim())) {
break//done
}
if (Object.hasOwn(openers, c) && closesInside(chars, start, end)) {
break//keep it
}
// punctuation
end--
}