Skip to content

Commit

Permalink
Merge pull request #291 from Shopify/handle-multi-word-joiner
Browse files Browse the repository at this point in the history
Handle multiple word joiners when splitting addresses
  • Loading branch information
DominiqueFlaaa authored Oct 15, 2024
2 parents 2e53778 + 2c2b6f1 commit e795287
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Nil.
---

## [1.12.0] - 2024-10-11
- Update Address splitting methods to split on the first delimiter [#291](https://github.com/Shopify/worldwide/pull/291)

## [1.11.1] - 2024-10-02
- Configure regexp timeout in Worldwide#Phone [#290](https://github.com/Shopify/worldwide/pull/290)

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GIT
PATH
remote: .
specs:
worldwide (1.11.1)
worldwide (1.12.0)
activesupport (>= 7.0)
i18n
phonelib (~> 0.8)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/worldwide': patch
---

Handle input having multiple word joiners in address-splitting functions
7 changes: 7 additions & 0 deletions lang/typescript/src/extended-address/splitAddress1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ describe('splitAddress1', () => {
});
});

test('splits the address on the first delimiter', () => {
expect(splitAddress1('CL', 'Main \u2060123\u2060abc', false)).toEqual({
streetName: 'Main',
streetNumber: '123\u2060abc',
});
});

test('returns full address object when separated by delimiter and decorator', () => {
expect(splitAddress1('BR', 'Main, \u2060123', false)).toEqual({
streetName: 'Main',
Expand Down
7 changes: 7 additions & 0 deletions lang/typescript/src/extended-address/splitAddress2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ describe('splitAddress2', () => {
});
});

test('splits the address on the first delimiter', () => {
expect(splitAddress2('CL', 'dpto 4 \u2060Centro\u2060abc')).toEqual({
line2: 'dpto 4',
neighborhood: 'Centro\u2060abc',
});
});

describe('language override', () => {
test("returns default format if language override doesn't match", () => {
expect(splitAddress2('TW', '30號\u2060大甲區')).toEqual({
Expand Down
19 changes: 12 additions & 7 deletions lang/typescript/src/utils/address-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@ export function splitAddressField(
fieldDefinition: FieldConcatenationRule[],
concatenatedAddress: string,
): Partial<Address> {
const values = concatenatedAddress.split(RESERVED_DELIMITER);
const [firstField, ...rest] = concatenatedAddress.split(RESERVED_DELIMITER);
const secondField = rest.join(RESERVED_DELIMITER);
const values = [firstField, secondField];

const parsedAddressObject = values.reduce((obj, value, index) => {
if (value !== '') {
const parsedAddressObject = fieldDefinition.reduce((obj, field, index) => {
if (values[index]) {
// Decorator is included as a suffix in the previous sub-field value,
// so we need to strip it from the current field by looking ahead at the
// next field's definition
Expand All @@ -57,12 +59,15 @@ export function splitAddressField(
const fieldValue =
nextFieldDecorator &&
nextFieldDecorator.length > 0 &&
value.endsWith(nextFieldDecorator)
? value.substring(0, value.length - nextFieldDecorator.length)
: value;
values[index].endsWith(nextFieldDecorator)
? values[index].substring(
0,
values[index].length - nextFieldDecorator.length,
)
: values[index];
return {
...obj,
[fieldDefinition[index].key]: fieldValue,
[field.key]: fieldValue,
};
}

Expand Down
6 changes: 4 additions & 2 deletions lib/worldwide/address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,15 @@ def concatenate_address2

def split_address1
additional_fields = region.combined_address_format.dig(script_from_string(address1), "address1") || []
split_fields_arr = address1&.split(RESERVED_DELIMITER) || []
number_of_fields = additional_fields.size
split_fields_arr = address1&.split(RESERVED_DELIMITER, number_of_fields) || []
split_fields(additional_fields, split_fields_arr)
end

def split_address2
additional_fields = region.combined_address_format.dig(script_from_string(address2), "address2") || []
split_fields_arr = address2&.split(RESERVED_DELIMITER) || []
number_of_fields = additional_fields.size
split_fields_arr = address2&.split(RESERVED_DELIMITER, number_of_fields) || []
split_fields(additional_fields, split_fields_arr)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/worldwide/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Worldwide
VERSION = "1.11.1"
VERSION = "1.12.0"
end
14 changes: 14 additions & 0 deletions test/worldwide/address_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,13 @@ class AddressTest < ActiveSupport::TestCase
assert_equal expected_hash, address.split_address1
end

test "split_address1 splits on the first delimiter" do
address = Address.new(address1: "Main Street⁠123⁠ n° 145", country_code: "BR")
expected_hash = { "street_name" => "Main Street", "street_number" => "123⁠ n° 145" }

assert_equal expected_hash, address.split_address1
end

test "split_address1 returns only street number if no string is present before the delimiter" do
cl_address = Address.new(address1: "⁠123", country_code: "CL")
br_address = Address.new(address1: "⁠123", country_code: "BR")
Expand Down Expand Up @@ -349,6 +356,13 @@ class AddressTest < ActiveSupport::TestCase
assert_equal expected_hash, br_address.split_address2
end

test "split_address2 splits on the first delimiter" do
address = Address.new(address2: "1樓⁠士林區⁠123", country_code: "TW")
expected_hash = { "line2" => "1樓", "neighborhood" => "士林區⁠123" }

assert_equal expected_hash, address.split_address2
end

test "split_address2 returns line2 and neighborhood when both values are present and separated by a delimiter" do
cl_address = Address.new(address2: "1樓⁠士林區", country_code: "TW")
expected_hash = { "line2" => "1樓", "neighborhood" => "士林區" }
Expand Down

0 comments on commit e795287

Please sign in to comment.