-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(post): add zero_prefixed_house_numbers script
- Loading branch information
1 parent
1cfe496
commit 3a71c2d
Showing
5 changed files
with
122 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Zero prefixed house number post-processing script strips leading zeros from | ||
* house numbers. eg. house number `001` -> `1`. | ||
* | ||
* This functionality was previously handled in elasticsearch | ||
* using a `removeAllZeroNumericPrefix` filter, see: | ||
* - https://github.com/pelias/schema/issues/503 | ||
*/ | ||
|
||
const _ = require('lodash'); | ||
|
||
function housenumber(doc) { | ||
|
||
// ensure housenumber is set | ||
let houseno = doc.getAddress('number'); | ||
if( !_.isString(houseno) || _.isEmpty(houseno) ){ return; } | ||
|
||
// trim leading/trailing whitespace | ||
houseno = houseno.trim(); | ||
|
||
// only applies to housenumbers prefixed with a zero | ||
if ( houseno.length < 2 || !houseno.startsWith('0') ) { return; } | ||
|
||
// trim leading zeros | ||
doc.setAddress('number', houseno.replace(/^0+/, '')); | ||
} | ||
|
||
module.exports = housenumber; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const Document = require('../../Document'); | ||
const housenumber = require('../../post/zero_prefixed_house_numbers'); | ||
|
||
module.exports.tests = {}; | ||
|
||
module.exports.tests.noop = function(test) { | ||
test('noop: house number not set', function(t) { | ||
const doc = new Document('mysource', 'mylayer', 'myid'); | ||
|
||
housenumber(doc); | ||
|
||
// no action taken | ||
t.equal(doc.getAddress('number'), undefined, 'not set'); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('noop: house no leading zero', function(t) { | ||
const doc = new Document('mysource', 'mylayer', 'myid'); | ||
doc.setAddress('number', '10'); | ||
|
||
housenumber(doc); | ||
|
||
// no action taken | ||
t.equal(doc.getAddress('number'), '10', 'no change'); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('noop: house no is literally zero', function(t) { | ||
const doc = new Document('mysource', 'mylayer', 'myid'); | ||
doc.setAddress('number', '0'); | ||
|
||
housenumber(doc); | ||
|
||
// no action taken | ||
t.equal(doc.getAddress('number'), '0', 'no change'); | ||
|
||
t.end(); | ||
}); | ||
}; | ||
|
||
module.exports.tests.strip_prefix = function(test) { | ||
test('strip: house number with zero prefix', function(t) { | ||
const doc = new Document('mysource', 'mylayer', 'myid'); | ||
doc.setAddress('number', '010'); | ||
|
||
housenumber(doc); | ||
|
||
// prefix removed | ||
t.equal(doc.getAddress('number'), '10', 'prefix removed'); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('strip: house number with multiple zero prefix', function(t) { | ||
const doc = new Document('mysource', 'mylayer', 'myid'); | ||
doc.setAddress('number', '000000001000'); | ||
|
||
housenumber(doc); | ||
|
||
// prefix removed | ||
t.equal(doc.getAddress('number'), '1000', 'prefix removed'); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('strip: house number with multiple zero prefix plus multiple whitespace', function(t) { | ||
const doc = new Document('mysource', 'mylayer', 'myid'); | ||
doc.setAddress('number', ' 000000009990'); | ||
|
||
housenumber(doc); | ||
|
||
// prefix removed | ||
t.equal(doc.getAddress('number'), '9990', 'prefix removed'); | ||
|
||
t.end(); | ||
}); | ||
}; | ||
|
||
module.exports.all = function (tape, common) { | ||
|
||
function test(name, testFunction) { | ||
return tape('post/zero_prefixed_house_numbers: ' + name, testFunction); | ||
} | ||
|
||
for( var testCase in module.exports.tests ){ | ||
module.exports.tests[testCase](test, common); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters