Skip to content

Commit

Permalink
feat(structured): make string replacements case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
missinglink committed Jan 23, 2025
1 parent a93894b commit 6112fad
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 2 deletions.
9 changes: 7 additions & 2 deletions controller/structured_libpostal.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ function setup(libpostalService, should_execute) {
req.clean.parsed_text.housenumber = house_number_field.value;

// remove the first instance of the number and trim whitespace
req.clean.parsed_text.street = _.trim(_.replace(req.clean.parsed_text.address, req.clean.parsed_text.housenumber, ''));
req.clean.parsed_text.street = _.trim(replaceIgnoreCase(req.clean.parsed_text.address, req.clean.parsed_text.housenumber, ''));

// If libpostal have parsed unit then add it for search
const unit_field = findField(response, 'unit');
if(unit_field) {
req.clean.parsed_text.unit = unit_field.value;
// Removing unit from street and trim
req.clean.parsed_text.street = _.trim(_.replace(req.clean.parsed_text.street, req.clean.parsed_text.unit, ''));
req.clean.parsed_text.street = _.trim(replaceIgnoreCase(req.clean.parsed_text.street, req.clean.parsed_text.unit, ''));
}

} else {
Expand All @@ -83,4 +83,9 @@ function setup(libpostalService, should_execute) {
return controller;
}

function replaceIgnoreCase(str, match, replacement) {
if (!_.isString(str) || !str.length) { return str; }
return str.replace(new RegExp(match, 'i'), replacement);
}

module.exports = setup;
137 changes: 137 additions & 0 deletions test/unit/controller/structured_libpostal.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,143 @@ module.exports.tests.success_conditions = (test, common) => {

});

test('as above, case insensitive', t => {
const service = (req, callback) => {
const response = [
{
label: 'road',
value: 'the street'
},
{
label: 'house_number',
value: '22'
},
{
label: 'unit',
value: '1 th'
}
];

callback(null, response);
};

const controller = libpostal(service, () => true);

const req = {
clean: {
parsed_text: {
address: 'the street 22 1 TH'
}
},
errors: []
};

controller(req, undefined, () => {
t.deepEquals(req, {
clean: {
parsed_text: {
street: 'the street',
housenumber: '22',
unit: '1 th'
}
},
errors: []
}, 'req should have been modified');

t.end();

});

});

test('service returning house_number and road should set req.clean.parsed_text.street', t => {
const service = (req, callback) => {
const response = [
{
label: 'road',
value: 'kinkerstraat'
},
{
label: 'house_number',
value: '175f'
}
];

callback(null, response);
};

const controller = libpostal(service, () => true);

const req = {
clean: {
parsed_text: {
address: 'kinkerstraat 175f'
}
},
errors: []
};

controller(req, undefined, () => {
t.deepEquals(req, {
clean: {
parsed_text: {
street: 'kinkerstraat',
housenumber: '175f'
}
},
errors: []
}, 'req should have been modified');

t.end();

});

});

test('as above, case insensitive', t => {
const service = (req, callback) => {
const response = [
{
label: 'road',
value: 'kinkerstraat'
},
{
label: 'house_number',
value: '175f'
}
];

callback(null, response);
};

const controller = libpostal(service, () => true);

const req = {
clean: {
parsed_text: {
address: 'kinkerstraat 175F'
}
},
errors: []
};

controller(req, undefined, () => {
t.deepEquals(req, {
clean: {
parsed_text: {
street: 'kinkerstraat',
housenumber: '175f'
}
},
errors: []
}, 'req should have been modified');

t.end();

});

});

// test('service returning valid response should convert and append', t => {
// const service = (req, callback) => {
// const response = [
Expand Down

0 comments on commit 6112fad

Please sign in to comment.