1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- import { PLUS_CHARS } from '../constants.js'
- import { limit } from './util.js'
- import {
- isLatinLetter,
- isInvalidPunctuationSymbol
- } from './utf-8.js'
- const OPENING_PARENS = '(\\[\uFF08\uFF3B'
- const CLOSING_PARENS = ')\\]\uFF09\uFF3D'
- const NON_PARENS = `[^${OPENING_PARENS}${CLOSING_PARENS}]`
- export const LEAD_CLASS = `[${OPENING_PARENS}${PLUS_CHARS}]`
- const LEAD_CLASS_LEADING = new RegExp('^' + LEAD_CLASS)
- const BRACKET_PAIR_LIMIT = limit(0, 3)
- const MATCHING_BRACKETS_ENTIRE = new RegExp
- (
- '^'
- + "(?:[" + OPENING_PARENS + "])?" + "(?:" + NON_PARENS + "+" + "[" + CLOSING_PARENS + "])?"
- + NON_PARENS + "+"
- + "(?:[" + OPENING_PARENS + "]" + NON_PARENS + "+[" + CLOSING_PARENS + "])" + BRACKET_PAIR_LIMIT
- + NON_PARENS + "*"
- + '$'
- )
- const PUB_PAGES = /\d{1,5}-+\d{1,5}\s{0,4}\(\d{1,4}/
- export default function isValidCandidate(candidate, offset, text, leniency)
- {
-
-
- if (!MATCHING_BRACKETS_ENTIRE.test(candidate) || PUB_PAGES.test(candidate)) {
- return
- }
-
-
- if (leniency !== 'POSSIBLE')
- {
-
-
-
- if (offset > 0 && !LEAD_CLASS_LEADING.test(candidate))
- {
- const previousChar = text[offset - 1]
-
- if (isInvalidPunctuationSymbol(previousChar) || isLatinLetter(previousChar)) {
- return false
- }
- }
- const lastCharIndex = offset + candidate.length
- if (lastCharIndex < text.length)
- {
- const nextChar = text[lastCharIndex]
- if (isInvalidPunctuationSymbol(nextChar) || isLatinLetter(nextChar)) {
- return false
- }
- }
- }
- return true
- }
|