Skip to content

Commit

Permalink
Merge pull request #26 from michael-rubel/fix/full-name-mckenzie-case
Browse files Browse the repository at this point in the history
Update `FullName` formatting behavior to handle names like `McKenzie`
  • Loading branch information
michael-rubel authored Dec 2, 2022
2 parents d4465d3 + e830cf7 commit 135a320
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/Collection/FullNameFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ public function __construct(public ?string $name)
*/
public function format(): string
{
$words = str($this->name)->split('/\s/');

$this->name = $words->map(function (string $word) {
return str($word)->ucfirst();
})->join(' ');

return str($this->name)
->replaceMatches('/\p{C}+/u', '')
->squish()
->title()
->value();
}
}
7 changes: 4 additions & 3 deletions tests/FullNameFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,18 @@ public function testCanFormatLastName()

public function testCanFormatFirstAndLastName()
{
$name = format(FullNameFormatter::class, 'michaeL rubéL');

$name = format(FullNameFormatter::class, 'michael rubél');
$this->assertSame('Michael Rubél', $name);
$name = format(FullNameFormatter::class, 'michael mcKenzie');
$this->assertSame('Michael McKenzie', $name);
}

public function testCanFormatNameWithMinusSeparators()
{
$name = format(FullNameFormatter::class, 'Anna Nowak-Kowalska');
$this->assertSame('Anna Nowak-Kowalska', $name);
$name = format(FullNameFormatter::class, ' anna nowak-kowalska ');
$this->assertSame('Anna Nowak-Kowalska', $name);
$this->assertSame('Anna Nowak-kowalska', $name);
}

public function testCutsSpaceBeforeAndAfter()
Expand Down

0 comments on commit 135a320

Please sign in to comment.