From 1cfe49610692b2a4d488465872e622a438aafd93 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Wed, 11 Dec 2024 12:47:02 +0100 Subject: [PATCH] feat(Document): strip URLs from name/address --- Document.js | 12 ++++++++---- test/document/address.js | 12 ++++++++---- test/document/name.js | 12 ++++++++---- util/transform.js | 2 ++ 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/Document.js b/Document.js index 2ae9702..8a35348 100644 --- a/Document.js +++ b/Document.js @@ -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; @@ -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 ) ){ @@ -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; @@ -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 ) ){ diff --git a/test/document/address.js b/test/document/address.js index 2f10d91..4327643 100644 --- a/test/document/address.js +++ b/test/document/address.js @@ -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(); }); }; @@ -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(); }); }; diff --git a/test/document/name.js b/test/document/name.js index ddddfc3..9e0ee70 100644 --- a/test/document/name.js +++ b/test/document/name.js @@ -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(); }); }; @@ -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(); }); }; diff --git a/util/transform.js b/util/transform.js index 22223bd..7ca143b 100644 --- a/util/transform.js +++ b/util/transform.js @@ -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(); \ No newline at end of file