refactoring
This commit is contained in:
19
src/SmartRegexpFactory.py
Normal file
19
src/SmartRegexpFactory.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
# adapted from function _fnFilterCreateSearch() defined in https://github.com/DataTables/DataTablesSrc/blob/master/js/core/core.filter.js
|
||||||
|
class SmartRegexpFactory:
|
||||||
|
|
||||||
|
def createSmartRegexp(self, searchTerm):
|
||||||
|
return re.compile(
|
||||||
|
rf'^{self.assertContainsWords(self.getWords(searchTerm))}.*$',
|
||||||
|
flags=re.IGNORECASE)
|
||||||
|
|
||||||
|
def getWords(self, searchTerm):
|
||||||
|
return re.split(r'\s+', searchTerm)
|
||||||
|
|
||||||
|
def assertContainsWords(self, words):
|
||||||
|
return ''.join([self.assertContainsWord(word) for word in words])
|
||||||
|
|
||||||
|
def assertContainsWord(self, word):
|
||||||
|
return f'(?=.*?{word})'
|
||||||
@@ -1,25 +1,11 @@
|
|||||||
import re
|
import re
|
||||||
|
from SmartRegexpFactory import SmartRegexpFactory
|
||||||
|
|
||||||
|
|
||||||
class SmartSearch:
|
class SmartSearch:
|
||||||
|
|
||||||
def __init__(self, searchTerm):
|
def __init__(self, searchTerm):
|
||||||
self.regexp = _SmartRegexpFactory().createSmartRegexp(searchTerm)
|
self.smartRegexp = SmartRegexpFactory().createSmartRegexp(searchTerm)
|
||||||
|
|
||||||
def matches(self, str):
|
def matches(self, str):
|
||||||
return True if re.match(self.regexp, str, flags=re.IGNORECASE) else False
|
return True if self.smartRegexp.match(str) else False
|
||||||
|
|
||||||
|
|
||||||
class _SmartRegexpFactory:
|
|
||||||
|
|
||||||
def createSmartRegexp(self, searchTerm):
|
|
||||||
return rf'^{self.assertContainsWords(self.getWords(searchTerm))}.*$'
|
|
||||||
|
|
||||||
def getWords(self, searchTerm):
|
|
||||||
return re.split(r'\s+', searchTerm)
|
|
||||||
|
|
||||||
def assertContainsWords(self, words):
|
|
||||||
return ''.join([self.assertContainsWord(word) for word in words])
|
|
||||||
|
|
||||||
def assertContainsWord(self, word):
|
|
||||||
return f'(?=.*?{word})'
|
|
||||||
|
|||||||
Reference in New Issue
Block a user