Bug 1820522 - Implement heuristics to determine whether two postal code/zip are the same, different, or mergeable r=mtigley

Differential Revision: https://phabricator.services.mozilla.com/D173807
This commit is contained in:
Dimi 2023-05-17 08:44:39 +00:00
Родитель a036e89931
Коммит dc76a806c7
3 изменённых файлов: 100 добавлений и 1 удалений

Просмотреть файл

@ -0,0 +1,57 @@
"use strict";
const VALID_TESTS = [
{ region: "US" },
["1234", false], // too short
["12345", true],
["123456", false], // too long
["1234A", false], // contain non-digit character
["12345-123", false],
["12345-1234", true],
["12345-12345", false],
["12345-1234A", false],
["12345 1234", true], // Do we want to allow this?
["12345_1234", false], // Do we want to allow this?
{ region: "CA" },
["M5T 1R5", true],
["M5T1R5", true], // no space between the first and second parts is allowed
["S4S 6X3", true],
["M5T", false], // Only the first part
["1R5", false], // Only the second part
["D1B 1A1", false], // invalid first character, D
["M5T 1R5A", false], // extra character at the end
["M5T 1R5-", false], // extra character at the end
["M5T-1R5", false], // hyphen in the wrong place
["MT5 1R5", false], // missing letter in the first part
["M5T 1R", false], // missing letter in the second part
["M5T 1R55", false], // extra digit at the end
["M5T 1R", false], // missing digit in the second part
["M5T 1R5Q", false], // invalid second-to-last letter, Q
];
const COMPARE_TESTS = [
{ region: "US" },
["12345", "12345", SAME],
["M5T 1R5", "m5t 1r5", SAME],
["12345-1234", "12345 1234", SAME],
["12345-1234", "12345", A_CONTAINS_B],
["12345-1234", "12345#1234", SAME], // B is invalid
["12345-1234", "1234", A_CONTAINS_B], // B is invalid
];
const TEST_FIELD_NAME = "PostalCode";
add_setup(async () => {});
add_task(async function test_isValid() {
runIsValidTest(VALID_TESTS, TEST_FIELD_NAME, value => {
return { "postal-code": value };
});
});
add_task(async function test_compare() {
runCompareTest(COMPARE_TESTS, TEST_FIELD_NAME, value => {
return { "postal-code": value };
});
});

Просмотреть файл

@ -18,6 +18,8 @@ head = head_addressComponent.js
head = head_addressComponent.js
[test_addressComponent_organization.js]
head = head_addressComponent.js
[test_addressComponent_postal_code.js]
head = head_addressComponent.js
[test_addressComponent_state.js]
head = head_addressComponent.js
[test_addressComponent_tel.js]

Просмотреть файл

@ -229,7 +229,47 @@ class StreetAddress extends AddressField {}
* A postal code / zip code
* See autocomplete="postal-code"
*/
class PostalCode extends AddressField {}
class PostalCode extends AddressField {
constructor(value, region) {
super(value, region);
}
isValid() {
const { postalCodePattern } = lazy.FormAutofillUtils.getFormFormat(
this.region
);
const regexp = new RegExp(`^${postalCodePattern}$`);
return regexp.test(this.userValue);
}
equals(other) {
const options = {
ignore_case: true,
remove_whitespace: true,
remove_punctuation: true,
};
return (
this.normalizeUserValue(options) == other.normalizeUserValue(options)
);
}
contains(other) {
const options = {
ignore_case: true,
remove_whitespace: true,
remove_punctuation: true,
};
const self_normalized_value = this.normalizeUserValue(options);
const other_normalized_value = other.normalizeUserValue(options);
return (
self_normalized_value.endsWith(other_normalized_value) ||
self_normalized_value.startsWith(other_normalized_value)
);
}
}
/**
* City name.