Skip to content

Commit

Permalink
normalize underscores in snake()
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Jun 29, 2024
1 parent 065a961 commit a382cfb
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion AbstractUnicodeString.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function reverse(): parent
public function snake(): parent
{
$str = clone $this;
$str->string = str_replace(' ', '_', mb_strtolower(preg_replace(['/(\p{Lu}+)(\p{Lu}\p{Ll})/u', '/([\p{Ll}0-9])(\p{Lu})/u'], '\1 \2', $str->string), 'UTF-8'));
$str->string = preg_replace('/[ _]+/', '_', mb_strtolower(preg_replace(['/(\p{Lu}+)(\p{Lu}\p{Ll})/u', '/([\p{Ll}0-9])(\p{Lu})/u'], '\1 \2', $str->string), 'UTF-8'));

return $str;
}
Expand Down
2 changes: 1 addition & 1 deletion ByteString.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function slice(int $start = 0, ?int $length = null): parent
public function snake(): parent
{
$str = clone $this;
$str->string = str_replace(' ', '_', strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1 \2', $str->string)));
$str->string = preg_replace('/[ _]+/', '_', strtolower(preg_replace(['/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'], '\1 \2', $str->string)));

return $str;
}
Expand Down
3 changes: 3 additions & 0 deletions Tests/AbstractAsciiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,9 @@ public static function provideSnake()
['symfony', 'SYMFONY'],
['symfony_is_great', 'SYMFONY IS GREAT'],
['symfony_is_great', 'SYMFONY_IS_GREAT'],
['symfony_is_great', 'symfony is great'],
['symfony_is_great', 'SYMFONY IS GREAT'],
['symfony_is_great', 'SYMFONY _ IS _ GREAT'],
];
}

Expand Down

0 comments on commit a382cfb

Please sign in to comment.