Skip to content

Commit aaf005c

Browse files
committed
Fixes #1021 undefined when abbreviate
1 parent f8775f1 commit aaf005c

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

src/internal.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,15 @@ export const cleanupInput = (input, replacements = [], options = {}) => {
214214
if (options.abbreviate && input.country_code && country2lang[input.country_code]) {
215215
for (let i = 0; i < country2lang[input.country_code].length; i++) {
216216
const lang = country2lang[input.country_code][i];
217-
for (let j = 0; j < abbreviations[lang].length; j++) {
218-
if (input[abbreviations[lang][j].component]) {
219-
for (let k = 0; k < abbreviations[lang][j].replacements.length; k++) {
220-
input[abbreviations[lang][j].component] = input[abbreviations[lang][j].component].replace(
221-
new RegExp(`(^|\\s)${abbreviations[lang][j].replacements[k].src}\\b`),
222-
`$1${abbreviations[lang][j].replacements[k].dest}`
223-
);
217+
if (abbreviations[lang]) {
218+
for (let j = 0; j < abbreviations[lang].length; j++) {
219+
if (input[abbreviations[lang][j].component]) {
220+
for (let k = 0; k < abbreviations[lang][j].replacements.length; k++) {
221+
input[abbreviations[lang][j].component] = input[abbreviations[lang][j].component].replace(
222+
new RegExp(`(^|\\s)${abbreviations[lang][j].replacements[k].src}\\b`),
223+
`$1${abbreviations[lang][j].replacements[k].dest}`
224+
);
225+
}
224226
}
225227
}
226228
}

test/internal.spec.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@ describe('address-formatter', () => {
295295
}, [], { abbreviate: true });
296296
expect(converted).toHaveProperty('platz', 'Bonn');
297297
});
298+
299+
it('should not crash for undefined abbreviations', () => {
300+
const converted = addressFormatterInternals.cleanupInput({
301+
country_code: 'JP'
302+
}, [], { abbreviate: true });
303+
expect(converted).toHaveProperty('country_code', 'JP');
304+
});
298305
});
299306

300307
describe('findTemplate', () => {

0 commit comments

Comments
 (0)