UNPKG

asksuite-core

Version:
62 lines (46 loc) 1.48 kB
const Knwl = require('knwl.js'); const knwlInstance = new Knwl('english'); knwlInstance.register('dates', require('./pt-br-date')); const KeywordUtil = {}; KeywordUtil.removeDotsAndComma = function(string) { const regex = /[.,]/g; return string.replace(regex, ''); }; function str2slug(str) { str = str.toLowerCase(); // remove accents, swap ñ for n, etc const from = 'àáäãâèéëêìíïîòóöôõùúüûñç'; const to = 'aaaaaeeeeiiiiooooouuuunc'; for (let i = 0, l = from.length; i < l; i++) { str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i)); } // remove invalid chars return str; } KeywordUtil.removeSpecial = function(string) { const newString = string.replace(/[&#,+()$~%.'":*?<>{}]/g, ' '); let newStr = newString.replace(/\s+/g, ' '); newStr = str2slug(newStr); return newStr; }; KeywordUtil.hasWordInString = function(string, words) { let value = false; if (words && words.length > 0) { string = KeywordUtil.removeDotsAndComma(string); const re = new RegExp('(\\s|^)(?:' + words.join('|') + ')(?=\\s|$)'); const valueTemp = string.match(re); if (valueTemp) { value = valueTemp.length > 0; } } return value; }; KeywordUtil.hasNumber = function(string) { return /\d/.test(string); }; KeywordUtil.hasDate = function(string) { knwlInstance.init(string); const dates = knwlInstance.get('dates'); return dates && dates.length > 0; }; module.exports = KeywordUtil;