Skip to content

Commit 7c1fc5a

Browse files
committed
feat(parser): remove URL regex caret anchor
1 parent 4c99ce3 commit 7c1fc5a

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

stream/parser.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,9 @@ function selectName( names ){
5656
// filter out URLs
5757
// then return the longest name
5858
// @todo: can we improve this logic?
59-
return names.filter( function ( name) {
60-
return !name.match(/^http(s)?:\/\//);
61-
}).reduce( function( a, b ){
62-
return a.length > b.length ? a : b;
63-
}, '');
59+
return names
60+
.filter(name => !/http(s)?:\/\//.test(name))
61+
.reduce((a, b) => a.length > b.length ? a : b, '');
6462
}
6563

6664
module.exports = parser;

test/stream/parser.js

+21
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,27 @@ module.exports.tests.filter_url = function(test, common) {
111111
stream.write(row);
112112
stream.end();
113113
});
114+
115+
// real-world example where the URL was included with a valid name
116+
// (ie. was preceeded by a space rather than a NULL character).
117+
test('parse: filter URL within name', (t) => {
118+
const stream = parser(6);
119+
const row = [
120+
'i{s~{AqubwJ{TxV{BlDmBnCiGhJgCbCs@dAaCfHmAnCoBpB',
121+
'Sentier des Chasupes',
122+
'Mairie Bouxières http://www.mairie-bouxieres-aux-dames.fr/wp-content/uploads/2005/01/Les-sentiers-de-Bouxi%C3%A8res-aux-Dames.pdf',
123+
].join('\0');
124+
const expected = 'Sentier des Chasupes';
125+
126+
const assert = ( actual, enc, next ) => {
127+
t.deepEqual( actual.properties.name, expected, 'longest non-URL name selected' );
128+
next();
129+
};
130+
131+
stream.pipe( through.obj( assert, () => t.end() ) );
132+
stream.write(row);
133+
stream.end();
134+
});
114135
};
115136

116137
module.exports.tests.filter_only_url = function(test, common) {

0 commit comments

Comments
 (0)