Skip to content

Commit ca1f1ae

Browse files
committed
feat(parser): remove URLs from names (ie. try to save them)
1 parent 7c1fc5a commit ca1f1ae

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

stream/parser.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ function parser( precision ){
5353
// each connected road can have one or more names
5454
// we select one name to be the default.
5555
function selectName( names ){
56-
// filter out URLs
57-
// then return the longest name
58-
// @todo: can we improve this logic?
56+
// remove URLs then return the longest name
5957
return names
60-
.filter(name => !/http(s)?:\/\//.test(name))
61-
.reduce((a, b) => a.length > b.length ? a : b, '');
58+
.map(name => name.replace(/(?:https?|ftp):\/\/\S+/g, '').trim())
59+
.sort((a, b) => b.length - a.length)
60+
.at(0);
6261
}
6362

6463
module.exports = parser;

test/stream/parser.js

+20
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,26 @@ module.exports.tests.filter_url = function(test, common) {
132132
stream.write(row);
133133
stream.end();
134134
});
135+
136+
test('parse: URL removal', (t) => {
137+
const stream = parser(6);
138+
const row = [
139+
'i{s~{AqubwJ{TxV{BlDmBnCiGhJgCbCs@dAaCfHmAnCoBpB',
140+
'http://foo.com/bar.pdf',
141+
'Short Example https://foo.com/bar.pdf',
142+
'Longer Example ftp://foo.com/bar.pdf',
143+
].join('\0');
144+
const expected = 'Longer Example';
145+
146+
const assert = ( actual, enc, next ) => {
147+
t.deepEqual( actual.properties.name, expected, 'longest non-URL name selected' );
148+
next();
149+
};
150+
151+
stream.pipe( through.obj( assert, () => t.end() ) );
152+
stream.write(row);
153+
stream.end();
154+
});
135155
};
136156

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

0 commit comments

Comments
 (0)