Skip to content

Commit dcf926a

Browse files
authored
fix(support): support more to_snake_case edge cases (#1250)
1 parent 25c4aff commit dcf926a

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

packages/support/src/Str/functions.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ function to_snake_case(Stringable|string $string, Stringable|string $delimiter =
6363
return $string;
6464
}
6565

66-
$string = preg_replace('/(.)(?=[A-Z])/u', '$1' . $delimiter, $string);
66+
$string = preg_replace('/(?<=\p{Ll}|\p{N})(\p{Lu})/u', $delimiter . '$1', $string);
67+
$string = preg_replace('/(?<=\p{Lu})(\p{Lu}\p{Ll})/u', $delimiter . '$1', $string);
6768
$string = preg_replace('![^' . preg_quote($delimiter) . '\pL\pN\s]+!u', $delimiter, mb_strtolower($string, 'UTF-8'));
6869
$string = preg_replace('/\s+/u', $delimiter, $string);
6970
$string = trim($string, $delimiter);
7071

71-
return deduplicate($string, $delimiter);
72+
return namespace\deduplicate($string, $delimiter);
7273
}
7374

7475
/**

packages/support/tests/Str/ManipulatesStringTest.php

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,22 +79,27 @@ public function test_kebab(): void
7979
$this->assertTrue(str('foo-bar_baz')->kebab()->equals('foo-bar-baz'));
8080
}
8181

82-
public function test_snake(): void
83-
{
84-
$this->assertTrue(str('')->snake()->equals(''));
85-
$this->assertTrue(str('foo bar')->snake()->equals('foo_bar'));
86-
$this->assertTrue(str('foo - bar')->snake()->equals('foo_bar'));
87-
$this->assertTrue(str('foo__bar')->snake()->equals('foo_bar'));
88-
$this->assertTrue(str('_foo__bar')->snake()->equals('foo_bar'));
89-
$this->assertTrue(str('-foo__bar')->snake()->equals('foo_bar'));
90-
$this->assertTrue(str('fooBar')->snake()->equals('foo_bar'));
91-
$this->assertTrue(str('foo_bar')->snake()->equals('foo_bar'));
92-
$this->assertTrue(str('foo_bar1')->snake()->equals('foo_bar1'));
93-
$this->assertTrue(str('1foo_bar')->snake()->equals('1foo_bar'));
94-
$this->assertTrue(str('1foo_bar11')->snake()->equals('1foo_bar11'));
95-
$this->assertTrue(str('1foo_1bar1')->snake()->equals('1foo_1bar1'));
96-
$this->assertTrue(str('foo-barBaz')->snake()->equals('foo_bar_baz'));
97-
$this->assertTrue(str('foo-bar_baz')->snake()->equals('foo_bar_baz'));
82+
#[TestWith(['', ''])]
83+
#[TestWith(['foo bar', 'foo_bar'])]
84+
#[TestWith(['foo - bar', 'foo_bar'])]
85+
#[TestWith(['foo__bar', 'foo_bar'])]
86+
#[TestWith(['_foo__bar', 'foo_bar'])]
87+
#[TestWith(['-foo__bar', 'foo_bar'])]
88+
#[TestWith(['fooBar', 'foo_bar'])]
89+
#[TestWith(['foo_bar', 'foo_bar'])]
90+
#[TestWith(['foo_bar1', 'foo_bar1'])]
91+
#[TestWith(['1foo_bar', '1foo_bar'])]
92+
#[TestWith(['1foo_bar11', '1foo_bar11'])]
93+
#[TestWith(['1foo_1bar1', '1foo_1bar1'])]
94+
#[TestWith(['foo-barBaz', 'foo_bar_baz'])]
95+
#[TestWith(['foo-bar_baz', 'foo_bar_baz'])]
96+
#[TestWith(['Application URI', 'application_uri'])]
97+
#[TestWith(['URI', 'uri'])]
98+
#[TestWith(['XMLHTTPRequest', 'xmlhttp_request'])]
99+
#[TestWith(['HTTPRequest', 'http_request'])]
100+
public function test_snake(string $input, string $output): void
101+
{
102+
$this->assertEquals($output, str($input)->snake()->toString());
98103
}
99104

100105
#[TestWith([0])]

0 commit comments

Comments
 (0)