-
Notifications
You must be signed in to change notification settings - Fork 0
/
Search.js
149 lines (141 loc) · 5.22 KB
/
Search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*=======================================================================*
* Needed for correct adding problem by icd10 code
*========================================================================*/
function icd10Normalize(icd10){
if(icd10.length > 3) var result = (icd10.slice(0,3)+"."+icd10.slice(3));
else var result = (icd10);
return result;
};
/*=======================================================================*
* Parse and strip user string
*========================================================================*/
function stripStr(str){
return (str).toLowerCase().replace(/[^a-z0-9 ]/g, "");
};
/*=======================================================================*
* Gets All permutations of provided string
*========================================================================*/
function getAllPermutations(arr,permArr,used){
var i, j, permArr=permArr?permArr:[], used=used?used:[];
for(i=0; i<arr.length; i++){
j = arr.splice(i,1)[0];
used.push(j);
if(arr.length==0)permArr.push(used.join(" ")); // save a for loop each permutation
getAllPermutations(arr,permArr,used);
arr.splice(i,0,j);
used.pop();
}
return permArr
};
/*=======================================================================*
* Needed to bypass words. exp search str = "cow blue", "The cow is blue"
* bypasses the and is to find a match.
*========================================================================*/
function addSlashes(str){
return (str + '').replace(/[\\"'\(\)]/g,'\\$&').replace(/\u0000/g,'\\0');
};
/*=======================================================================*
* Search by ICD10 Code
*========================================================================*/
function searchICD10(str, jsonFile){
try{
var result;
str = addSlashes(str).replace(' ','[^"]+');
var jsonStr = JSON.stringify(jsonFile, null,' ');
var found = jsonStr.match(
new RegExp(
'{\\s+"icd10":\\s".*'+ str +'.*",'+
'\\s+"description":\\s".*",'+
'\\s+"weight":\\s[\\d\\.]+\\s+}'
,'gi')
);
if(found){
return (JSON.parse(JSON.stringify(found)
.replace(/"{/g,"{")
.replace(/\\n/g,"")
.replace(/\\/g,"")
.replace(/}"/g,"}")));
}
}catch(e){ console.log(e); }
};
/*=======================================================================*
* Search by Description
*========================================================================*/
function searchDesc(str, jsonFile){
try{
var result;
str = addSlashes(str).replace(' ','[^"]+');
var jsonStr = JSON.stringify(jsonFile, null,' ');
var found = jsonStr.match(
new RegExp(
'{\\s+"icd10":\\s"\\w+",'+
'\\s+"description":\\s.*'+ str +'.*",'+
'\\s+"weight":\\s[\\d\\.]+\\s+}'
,'gi')
);
if(found){
return (JSON.parse(JSON.stringify(found)
.replace(/"{/g,"{")
.replace(/\\n/g,"")
.replace(/\\/g,"")
.replace(/}"/g,"}")));
}
}catch(e){ console.log(e); }
};
/*=======================================================================*
* Joins by ICD10 codes, creating a unique array.
*========================================================================*/
function joinArrayUnique(arr){
var result = arr.concat();
for(var i=0; i<result.length; ++i) {
for(var j=i+1; j<result.length; ++j) {
if(result[i].ICD10 === result[j].ICD10) result.splice(j--, 1);
}
}
return result;
};
/*=======================================================================*
* returns a full searched list from users text - Jakes method
*========================================================================*/
function search(usrstr, jsonFile){
var arr = [];
var searchText = stripStr(usrstr).split(" ");
for(var i=0; i<searchText.length; i++){
for(var j=0; j<searchText.length; j++){
if(i != j && searchText[i] == searchText[j]) searchText.splice(i--, 1);
}
}
var s1 = searchICD10(searchText, jsonFile);
if(s1) return s1;
for (var i=0; i<searchText.length; i++){
if(!i)var s2 = searchDesc(searchText[i], jsonFile)
else var s2 = searchDesc(searchText[i], arr);
if(!s2) return [];
else arr = JSON.parse(JSON.stringify(s2));
}
return arr;
};
/*=======================================================================*
* returns a full searched list from users text - Permutation method
*========================================================================*/
function search2(usrstr, jsonFile){
var arr1 = [], arr2 = [];
var searchText = stripStr(usrstr);
var permutations = getAllPermutations(searchText.split(" "));
/* Search by icd10 */
var s1 = searchICD10(searchText, jsonFile);
if(s1) arr1.push(s1);
/* Search Json data with all permutations */
for (var i=0; i<permutations.length; i++){
var s2 = searchDesc(permutations[i], jsonFile);
if(s2) arr1.push(s2);
}
/* Combine all Arrays into one unique array */
if(arr1.length == 1) arr2.push(arr1.pop());
else{
for(var i=0; i<arr1.length-1;i++){
arr2.push(joinArrayUnique(arr1[i].concat(arr1[i+1])));
}
}
return arr2.pop();
};