Skip to content

Commit

Permalink
feat(Document): strip URLs from name/address
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink committed Dec 13, 2024
1 parent bdbcac5 commit 1cfe496
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
12 changes: 8 additions & 4 deletions Document.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ Document.prototype.setName = function( prop, value ){

validate.type('string', value);
validate.truthy(value);
validate.regex.nomatch(value, /https?:\/\//);
value = transform.stripURLs(value);
if (!value.length) { return; }

if( Array.isArray( this.name[ prop ] ) ){
this.name[ prop ][ 0 ] = value;
Expand All @@ -257,7 +258,8 @@ Document.prototype.setNameAlias = function( prop, value ){

validate.type('string', value);
validate.truthy(value);
validate.regex.nomatch(value, /https?:\/\//);
value = transform.stripURLs(value);
if (!value.length) { return; }

// is this the first time setting this prop? ensure it's an array
if( !this.hasName( prop ) ){
Expand Down Expand Up @@ -405,7 +407,8 @@ Document.prototype.setAddress = function( prop, value ){
validate.type('string', value);
validate.truthy(value);
validate.property(addressFields, prop);
validate.regex.nomatch(value, /https?:\/\//);
value = transform.stripURLs(value);
if (!value.length) { return; }

if( Array.isArray( this.address_parts[ prop ] ) ){
this.address_parts[ prop ][ 0 ] = value;
Expand All @@ -421,7 +424,8 @@ Document.prototype.setAddressAlias = function( prop, value ){
validate.type('string', value);
validate.truthy(value);
validate.property(addressFields, prop);
validate.regex.nomatch(value, /https?:\/\//);
value = transform.stripURLs(value);
if (!value.length) { return; }

// is this the first time setting this prop? ensure it's an array
if( !this.hasAddress( prop ) ){
Expand Down
12 changes: 8 additions & 4 deletions test/document/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ module.exports.tests.setAddress = function(test) {
});
test('setAddress - http regex', function (t) {
var doc = new Document('mysource', 'mylayer', 'myid');
t.throws(doc.setAddress.bind(doc, 'number', 'http://www.pelias.io'), /invalid regex/, 'regex failure');
t.throws(doc.setAddress.bind(doc, 'number', 'AAhttp://www.pelias.ioBB'), /invalid regex/, 'regex failure');
t.doesNotThrow(doc.setAddress.bind(doc, 'number', 'http://www.pelias.io'), /invalid regex/);
t.doesNotThrow(doc.setAddress.bind(doc, 'number', 'AAhttp://www.pelias.ioBB'), /invalid regex/);
t.doesNotThrow(doc.setAddress.bind(doc, 'number', 'AA http://www.pelias.io BB'), /invalid regex/);
t.deepEqual(doc.address_parts.number, 'AA BB', 'URLs removed');
t.end();
});
};
Expand Down Expand Up @@ -116,8 +118,10 @@ module.exports.tests.setAddressAlias = function(test) {
});
test('setAddressAlias - http regex', function (t) {
var doc = new Document('mysource', 'mylayer', 'myid');
t.throws(doc.setAddressAlias.bind(doc, 'number', 'http://www.pelias.io'), /invalid regex/, 'regex failure');
t.throws(doc.setAddressAlias.bind(doc, 'number', 'AAhttp://www.pelias.ioBB'), /invalid regex/, 'regex failure');
t.doesNotThrow(doc.setAddressAlias.bind(doc, 'number', 'http://www.pelias.io'), /invalid regex/);
t.doesNotThrow(doc.setAddressAlias.bind(doc, 'number', 'AAhttp://www.pelias.ioBB'), /invalid regex/);
t.doesNotThrow(doc.setAddressAlias.bind(doc, 'number', 'AA http://www.pelias.io BB'), /invalid regex/);
t.deepEqual(doc.address_parts.number, ['AA', 'AA', 'AA BB'], 'URLs removed');
t.end();
});
};
Expand Down
12 changes: 8 additions & 4 deletions test/document/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ module.exports.tests.setName = function(test) {
});
test('setName - http regex', function (t) {
var doc = new Document('mysource', 'mylayer', 'myid');
t.throws(doc.setName.bind(doc, 'default', 'http://www.pelias.io'), /invalid regex/, 'regex failure');
t.throws(doc.setName.bind(doc, 'default', 'AAhttp://www.pelias.ioBB'), /invalid regex/, 'regex failure');
t.doesNotThrow(doc.setName.bind(doc, 'default', 'http://www.pelias.io'), /invalid regex/);
t.doesNotThrow(doc.setName.bind(doc, 'default', 'AAhttp://www.pelias.ioBB'), /invalid regex/);
t.doesNotThrow(doc.setName.bind(doc, 'default', 'AA http://www.pelias.io BB'), /invalid regex/);
t.deepEqual(doc.name.default, 'AA BB', 'URLs removed');
t.end();
});
};
Expand Down Expand Up @@ -106,8 +108,10 @@ module.exports.tests.setNameAlias = function(test) {
});
test('setNameAlias - http regex', function (t) {
var doc = new Document('mysource', 'mylayer', 'myid');
t.throws(doc.setNameAlias.bind(doc, 'default', 'http://www.pelias.io'), /invalid regex/, 'regex failure');
t.throws(doc.setNameAlias.bind(doc, 'default', 'AAhttp://www.pelias.ioBB'), /invalid regex/, 'regex failure');
t.doesNotThrow(doc.setNameAlias.bind(doc, 'default', 'http://www.pelias.io'), /invalid regex/);
t.doesNotThrow(doc.setNameAlias.bind(doc, 'default', 'AAhttp://www.pelias.ioBB'), /invalid regex/);
t.doesNotThrow(doc.setNameAlias.bind(doc, 'default', 'AA http://www.pelias.io BB'), /invalid regex/);
t.deepEqual(doc.name.default, ['AA', 'AA', 'AA BB'], 'URLs removed');
t.end();
});
};
Expand Down
2 changes: 2 additions & 0 deletions util/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ module.exports.toULLR = function( val ) {
max_lon: val.lowerRight.lon
});
};

module.exports.stripURLs = (val) => val.replace(/(?:https?|ftp):\/\/\S*/g, '').trim();

0 comments on commit 1cfe496

Please sign in to comment.