Skip to content

Commit 714f9a3

Browse files
committed
fix regex search
1 parent ee4a9f4 commit 714f9a3

4 files changed

Lines changed: 39 additions & 36 deletions

File tree

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"react-search-input.js",
55
"react-search-input.css"
66
],
7-
"version": "0.2.1",
7+
"version": "0.2.2",
88
"homepage": "https://github.com/enki-com/react-search-input",
99
"description": "Simple react.js component for a search input, providing a filter function.",
1010
"keywords": [

example/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
render() {
4848
var mails = [{
4949
user: {
50-
name: 'Mathieu',
50+
name: 'java',
5151
job: 'Software Engineer',
5252
company: 'Enki'
5353
},
@@ -66,7 +66,7 @@
6666
]
6767
}, {
6868
user: {
69-
name: 'foo'
69+
name: 'javascript'
7070
},
7171
subject: 'bar',
7272
dest: [

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-search-input",
3-
"version": "0.2.1",
3+
"version": "0.2.2",
44
"description": "Simple react.js component for a search input, providing a filter function.",
55
"main": "react-search-input.js",
66
"dependencies": {

react-search-input.js

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,7 @@
6060
return function(item) {
6161
if (term === '') {return true;}
6262
// escape special symbols to ensure `term` is a valid regex
63-
term = term.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
64-
65-
var _getValuesForKey = function(key, _item) {
66-
var keys = key.split('.');
67-
var results = [_item];
68-
keys.forEach(function(_key) {
69-
var tmp = [];
70-
results.forEach(function(result) {
71-
if (result) {
72-
if(result instanceof Array) {
73-
result.forEach(function(res) {
74-
tmp.push(res[_key]);
75-
});
76-
} else {
77-
tmp.push(result[_key]);
78-
}
79-
}
80-
});
81-
results = tmp;
82-
});
83-
return results.map(function(result) {
84-
if (typeof result === 'string') {
85-
return result.toLowerCase();
86-
} else {
87-
return null;
88-
}
89-
});
90-
};
63+
var escapedTerm = term.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
9164

9265
if (keys) {
9366
if( typeof keys === 'string' ) {
@@ -97,8 +70,11 @@
9770
var values = _getValuesForKey(keys[i], item);
9871
var found = false;
9972
values.forEach(function(value) {
100-
if (value && value.search(term) !== -1) {
101-
found = true;
73+
try {
74+
if (value && value.search(term) !== -1) {
75+
found = true;
76+
}
77+
} catch(e) {
10278
}
10379
});
10480
if (found) {
@@ -107,10 +83,10 @@
10783
}
10884
return false;
10985
} else {
110-
if( typeof item === 'string' ) {
86+
try {
11187
var stringValue = item.toLowerCase();
11288
return (stringValue.search(term) !== -1);
113-
} else {
89+
} catch(e) {
11490
return false;
11591
}
11692
}
@@ -119,5 +95,32 @@
11995
}
12096
});
12197

98+
var _getValuesForKey = function(key, _item) {
99+
var keys = key.split('.');
100+
var results = [_item];
101+
keys.forEach(function(_key) {
102+
var tmp = [];
103+
results.forEach(function(result) {
104+
if (result) {
105+
if(result instanceof Array) {
106+
result.forEach(function(res) {
107+
tmp.push(res[_key]);
108+
});
109+
} else {
110+
tmp.push(result[_key]);
111+
}
112+
}
113+
});
114+
results = tmp;
115+
});
116+
return results.map(function(result) {
117+
if (typeof result === 'string') {
118+
return result.toLowerCase();
119+
} else {
120+
return null;
121+
}
122+
});
123+
};
124+
122125
return Search;
123126
});

0 commit comments

Comments
 (0)