UNPKG

1.48 kBJavaScriptView Raw
1const Knwl = require('knwl.js');
2const knwlInstance = new Knwl('english');
3knwlInstance.register('dates', require('./pt-br-date'));
4
5const KeywordUtil = {};
6
7KeywordUtil.removeDotsAndComma = function(string) {
8 const regex = /[.,]/g;
9 return string.replace(regex, '');
10};
11
12function str2slug(str) {
13 str = str.toLowerCase();
14
15 // remove accents, swap ñ for n, etc
16 const from = 'àáäãâèéëêìíïîòóöôõùúüûñç';
17 const to = 'aaaaaeeeeiiiiooooouuuunc';
18 for (let i = 0, l = from.length; i < l; i++) {
19 str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
20 }
21 // remove invalid chars
22
23 return str;
24}
25
26KeywordUtil.removeSpecial = function(string) {
27 const newString = string.replace(/[&#,+()$~%.'":*?<>{}]/g, ' ');
28 let newStr = newString.replace(/\s+/g, ' ');
29 newStr = str2slug(newStr);
30
31 return newStr;
32};
33
34KeywordUtil.hasWordInString = function(string, words) {
35 let value = false;
36
37 if (words && words.length > 0) {
38 string = KeywordUtil.removeDotsAndComma(string);
39 const re = new RegExp('(\\s|^)(?:' + words.join('|') + ')(?=\\s|$)');
40
41 const valueTemp = string.match(re);
42
43 if (valueTemp) {
44 value = valueTemp.length > 0;
45 }
46 }
47
48 return value;
49};
50
51KeywordUtil.hasNumber = function(string) {
52 return /\d/.test(string);
53};
54
55KeywordUtil.hasDate = function(string) {
56 knwlInstance.init(string);
57 const dates = knwlInstance.get('dates');
58 return dates && dates.length > 0;
59};
60
61module.exports = KeywordUtil;