UNPKG

concepts-parser

Version:
75 lines (74 loc) 2.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const utils = require("../utils"); const concept_1 = require("../concept"); class BaseParser { constructor(options) { options = options || {}; options = Object.assign({ acceptConceptWords: [], acceptConnectChars: ["&", "-", "'", ".", "’", "`"], acceptStartQuotes: ['"', "“", "”", "„", "«"], acceptEndQuotes: ['"', "“", "”", "„", "»"], acceptPrefixes: [] }, options); if (options.acceptConceptWords) { options.acceptConceptWords.sort(function (a, b) { return b.length - a.length; }); } options.acceptConceptWordsRegex = new RegExp("^(" + options.acceptConceptWords.join("|") + ") "); options.acceptConceptWordsRegex2 = new RegExp("^[ ](" + options.acceptConceptWords.join("|") + ")[ ]$"); this.options = options; } isIn(name, value) { var options = this.options; return options[name].indexOf(value) >= 0; } isInConnectChars(value) { return this.isIn("acceptConnectChars", value); } isInStartQuotes(value) { return this.isIn("acceptStartQuotes", value); } isInEndQuotes(value) { return this.isIn("acceptEndQuotes", value); } isInPrefixes(value) { return this.isIn("acceptPrefixes", value); } isInConceptWords(value) { return this.isIn("acceptConceptWords", value); } getStartConceptWord(value) { let result = this.options.acceptConceptWordsRegex.exec(value); if (result) { return result[1]; } return null; } formatConcept(context, input, i, start) { let text = input.substr(start, i - start - 1); return new concept_1.Concept({ value: text, index: start, lang: context.lang }); } isValidWordChar(c) { if (utils.isLetterOrDigit(c)) { return true; } return this.options.acceptConnectChars.indexOf(c) > -1; } isValidStartWordChar(c) { return utils.isLetterOrDigit(c); } isWordsSeparatorChar(c) { return /[\s]/.test(c); } isLowerStartUpperWord(text, index) { let t = text.substr(index).trim(); for (let i = 0; i < t.length; i++) { let c = t[i]; if (!this.isValidWordChar(c)) { return false; } if (utils.isUpper(c)) { return true; } } return false; } } exports.BaseParser = BaseParser;