1 | const Knwl = require('knwl.js');
|
2 | const knwlInstance = new Knwl('english');
|
3 | knwlInstance.register('dates', require('./pt-br-date'));
|
4 |
|
5 | const KeywordUtil = {};
|
6 |
|
7 | KeywordUtil.removeDotsAndComma = function(string) {
|
8 | const regex = /[.,]/g;
|
9 | return string.replace(regex, '');
|
10 | };
|
11 |
|
12 | function str2slug(str) {
|
13 | str = str.toLowerCase();
|
14 |
|
15 |
|
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 |
|
22 |
|
23 | return str;
|
24 | }
|
25 |
|
26 | KeywordUtil.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 |
|
34 | KeywordUtil.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 |
|
51 | KeywordUtil.hasNumber = function(string) {
|
52 | return /\d/.test(string);
|
53 | };
|
54 |
|
55 | KeywordUtil.hasDate = function(string) {
|
56 | knwlInstance.init(string);
|
57 | const dates = knwlInstance.get('dates');
|
58 | return dates && dates.length > 0;
|
59 | };
|
60 |
|
61 | module.exports = KeywordUtil;
|