8000 Feature: doesntStartWith() and doesntEndWith() string methods by balboacodes · Pull Request #56168 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

Feature: doesntStartWith() and doesntEndWith() string methods #56168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,18 @@ public static function endsWith($haystack, $needles)
return false;
}

/**
* Determine if a given string doesn't end with a given substring.
*
* @param string $haystack
* @param string|iterable<string> $needles
* @return bool
*/
public static function doesntEndWith($haystack, $needles)
{
return ! static::endsWith($haystack, $needles);
}

/**
* Extracts an excerpt from text that matches the first instance of a phrase.
*
Expand Down Expand Up @@ -1659,6 +1671,18 @@ public static function startsWith($haystack, $needles)
return false;
}

/**
* Determine if a given string doesn't start with a given substring.
*
* @param string $haystack
* @param string|iterable<string> $needles
* @return bool
*/
public static function doesntStartWith($haystack, $needles)
{
return ! static::startsWith($haystack, $needles);
}

/**
* Convert a value to studly caps case.
*
Expand Down
48 changes: 48 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,17 @@ public function endsWith($needles)
return Str::endsWith($this->value, $needles);
}

/**
* Determine if a given string doesn't end with a given substring.
*
* @param string|iterable<string> $needles
* @return bool
*/
public function doesntEndWith($needles)
{
return Str::doesntEndWith($this->value, $needles);
}

/**
* Determine if the string is an exact match with the given value.
*
Expand Down Expand Up @@ -932,6 +943,17 @@ public function startsWith($needles)
return Str::startsWith($this->value, $needles);
}

/**
* Determine if a given string doesn't start with a given substring.
*
* @param string|iterable<string> $needles
* @return bool
*/
public function doesntStartWith($needles)
{
return Str::doesntStartWith($this->value, $needles);
}

/**
* Convert a value to studly caps case.
*
Expand Down Expand Up @@ -1143,6 +1165,19 @@ public function whenEndsWith($needles, $callback, $default = null)
return $this->when($this->endsWith($needles), $callback, $default);
}

/**
* Execute the given callback if the string doesn't end with a given substring.
*
* @param string|iterable<string> $needles
* @param callable $callback
* @param callable|null $default
* @return static
*/
public function whenDoesntEndWith($needles, $callback, $default = null)
{
return $this->when($this->doesntEndWith($needles), $callback, $default);
}

/**
* Execute the given callback if the string is an exact match with the given value.
*
Expand Down Expand Up @@ -1231,6 +1266,19 @@ public function whenStartsWith($needles, $callback, $default = null)
return $this->when($this->startsWith($needles), $callback, $default);
}

/**
* Execute the given callback if the string doesn't start with a given substring.
*
* @param string|iterable<string> $needles
* @param callable $callback
* @param callable|null $default
* @return static
*/
public function whenDoesntStartWith($needles, $callback, $default = null)
{
return $this->when($this->doesntStartWith($needles), $callback, $default);
}

/**
* Execute the given callback if the string matches the given pattern.
*
Expand Down
68 changes: 68 additions & 0 deletions tests/Support/SupportStrTest.php
8000
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,41 @@ public function testStartsWith()
$this->assertFalse(Str::startsWith('你好', 'a'));
}

public function testDoesntStartWith()
{
$this->assertFalse(Str::doesntStartWith('jason', 'jas'));
$this->assertFalse(Str::doesntStartWith('jason', 'jason'));
$this->assertFalse(Str::doesntStartWith('jason', ['jas']));
$this->assertFalse(Str::doesntStartWith('jason', ['day', 'jas']));
$this->assertFalse(Str::doesntStartWith('jason', collect(['day', 'jas'])));
$this->assertTrue(Str::doesntStartWith('jason', 'day'));
$this->assertTrue(Str::doesntStartWith('jason', ['day']));
$this->assertTrue(Str::doesntStartWith('jason', null));
$this->assertTrue(Str::doesntStartWith('jason', [null]));
$this->assertTrue(Str::doesntStartWith('0123', [null]));
$this->assertFalse(Str::doesntStartWith('0123', 0));
$this->assertTrue(Str::doesntStartWith('jason', 'J'));
$this->assertTrue(Str::doesntStartWith('jason', ''));
$this->assertTrue(Str::doesntStartWith('', ''));
$this->assertTrue(Str::doesntStartWith('7', ' 7'));
$this->assertFalse(Str::doesntStartWith('7a', '7'));
$this->assertFalse(Str::doesntStartWith('7a', 7));
$this->assertFalse(Str::doesntStartWith('7.12a', 7.12));
$this->assertTrue(Str::doesntStartWith('7.12a', 7.13));
$this->assertFalse(Str::doesntStartWith(7.123, '7'));
$this->assertFalse(Str::doesntStartWith(7.123, '7.12'));
$this->assertTrue(Str::doesntStartWith(7.123, '7.13'));
$this->assertTrue(Str::doesntStartWith(null, 'Marc'));
// Test for multibyte string support
$this->assertFalse(Str::doesntStartWith('Jönköping', 'Jö'));
$this->assertFalse(Str::doesntStartWith('Malmö', 'Malmö'));
$this->assertTrue(Str::doesntStartWith('Jönköping', 'Jonko'));
$this->assertTrue(Str::doesntStartWith('Malmö', 'Malmo'));
$this->assertFalse(Str::doesntStartWith('你好', '你'));
$this->assertTrue(Str::doesntStartWith('你好', '好'));
$this->assertTrue(Str::doesntStartWith('你好', 'a'));
}

public function testEndsWith()
{
$this->assertTrue(Str::endsWith('jason', 'on'));
Expand Down Expand Up @@ -219,6 +254,39 @@ public function testEndsWith()
$this->assertFalse(Str::endsWith('你好', 'a'));
}

public function testDoesntEndWith()
{
$this->assertFalse(Str::doesntEndWith('jason', 'on'));
$this->assertFalse(Str::doesntEndWith('jason', 'jason'));
$this->assertFalse(Str::doesntEndWith('jason', ['on']));
$this->assertFalse(Str::doesntEndWith('jason', ['no', 'on']));
$this->assertFalse(Str::doesntEndWith('jason', collect(['no', 'on'])));
$this->assertTrue(Str::doesntEndWith('jason', 'no'));
$this->assertTrue(Str::doesntEndWith('jason', ['no']));
$this->assertTrue(Str::doesntEndWith('jason', ''));
$this->assertTrue(Str::doesntEndWith('', ''));
$this->assertTrue(Str::doesntEndWith('jason', [null]));
$this->assertTrue(Str::doesntEndWith('jason', null));
$this->assertTrue(Str::doesntEndWith('jason', 'N'));
$this->assertTrue(Str::doesntEndWith('7', ' 7'));
$this->assertFalse(Str::doesntEndWith('a7', '7'));
$this->assertFalse(Str::doesntEndWith('a7', 7));
$this->assertFalse(Str::doesntEndWith('a7.12', 7.12));
$this->assertTrue(Str::doesntEndWith('a7.12', 7.13));
$this->assertFalse(Str::doesntEndWith(0.27, '7'));
$this->assertFalse(Str::doesntEndWith(0.27, '0.27'));
$this->assertTrue(Str::doesntEndWith(0.27, '8'));
$this->assertTrue(Str::doesntEndWith(null, 'Marc'));
// Test for multibyte string support
$this->assertFalse(Str::doesntEndWith('Jönköping', 'öping'));
$this->assertFalse(Str::doesntEndWith('Malmö', 'mö'));
$this->assertTrue(Str::doesntEndWith('Jönköping', 'oping'));
$this->assertTrue(Str::doesntEndWith('Malmö', 'mo'));
$this->assertFalse(Str::doesntEndWith('你好', '好'));
$this->assertTrue(Str::doesntEndWith('你好', '你'));
$this->assertTrue(Str::doesntEndWith('你好', 'a'));
}

public function testStrExcerpt()
{
$this->assertSame('...is a beautiful morn...', Str::excerpt('This is a beautiful morning', 'beautiful', ['radius' => 5]));
Expand Down
108 changes: 108 additions & 0 deletions tests/Support/SupportStringableTest.php
6DB6 9E12
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,31 @@ public function testWhenEndsWith()
}));
}

public function testWhenDoesntEndWith()
{
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenDoesntEndWith('ark', function ($stringable) {
return $stringable->studly();
}, function ($stringable) {
return $stringable->title();
}));

$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenDoesntEndWith(['kra', 'ark'], function ($stringable) {
return $stringable->studly();
}, function ($stringable) {
return $stringable->title();
}));

$this->assertSame('tony stark', (string) $this->stringable('tony stark')->whenDoesntEndWith(['xxx'], function ($stringable) {
return $stringable;
}));

$this->assertSame('TonyStark', (string) $this->stringable('tony stark')->whenDoesntEndWith(['tony', 'xxx'], function ($stringable) {
return $stringable->studly();
}, function ($stringable) {
return $stringable->title();
}));
}

public function testWhenExactly()
{
$this->assertSame('Nailed it...!', (string) $this->stringable('Tony Stark')->whenExactly('Tony Stark', function ($stringable) {
Expand Down Expand Up @@ -437,6 +462,31 @@ public function testWhenStartsWith()
}));
}

public function testWhenDoesntStartWith()
{
$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenDoesntStartWith('ton', function ($stringable) {
return $stringable->studly();
}, function ($stringable) {
return $stringable->title();
}));

$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenDoesntStartWith(['ton', 'not'], function ($stringable) {
return $stringable->studly();
}, function ($stringable) {
return $stringable->title();
}));

$this->assertSame('tony stark', (string) $this->stringable('tony stark')->whenDoesntStartWith(['xxx'], function ($stringable) {
return $stringable;
}));

$this->assertSame('Tony Stark', (string) $this->stringable('tony stark')->whenDoesntStartWith(['tony', 'xxx'], function ($stringable) {
return $stringable->studly();
}, function ($stringable) {
return $stringable->title();
}));
}

public function testWhenEmpty()
{
tap($this->stringable(), function ($stringable) {
Expand Down Expand Up @@ -598,6 +648,36 @@ public function testStartsWith()
$this->assertFalse($this->stringable('Malmö')->startsWith('Malmo'));
}

public function testDoesntStartWith()
{
$this->assertFalse($this->stringable('jason')->doesntStartWith('jas'));
$this->assertFalse($this->stringable('jason')->doesntStartWith('jason'));
$this->assertFalse($this->stringable('jason')->doesntStartWith(['jas']));
$this->assertFalse($this->stringable('jason')->doesntStartWith(['day', 'jas']));
$this->assertFalse($this->stringable('jason')->doesntStartWith(collect(['day', 'jas'])));
$this->assertTrue($this->stringable('jason')->doesntStartWith('day'));
$this->assertTrue($this->stringable('jason')->doesntStartWith(['day']));
$this->assertTrue($this->stringable('jason')->doesntStartWith(null));
$this->assertTrue($this->stringable('jason')->doesntStartWith([null]));
$this->assertTrue($this->stringable('0123')->doesntStartWith([null]));
$this->assertFalse($this->stringable('0123')->doesntStartWith(0));
$this->assertTrue($this->stringable('jason')->doesntStartWith('J'));
$this->assertTrue($this->stringable('jason')->doesntStartWith(''));
$this->assertTrue($this->stringable('7')->doesntStartWith(' 7'));
$this->assertFalse($this->stringable('7a')->doesntStartWith('7'));
$this->assertFalse($this->stringable('7a')->doesntStartWith(7));
$this->assertFalse($this->stringable('7.12a')->doesntStartWith(7.12));
$this->assertTrue($this->stringable('7.12a')->doesntStartWith(7.13));
$this->assertFalse($this->stringable(7.123)->doesntStartWith('7'));
$this->assertFalse($this->stringable(7.123)->doesntStartWith('7.12'));
$this->assertTrue($this->stringable(7.123)->doesntStartWith('7.13'));
// Test for multibyte string support
$this->assertFalse($this->stringable('Jönköping')->doesntStartWith('Jö'));
$this->assertFalse($this->stringable('Malmö')->doesntStartWith('Malmö'));
$this->assertTrue($this->stringable('Jönköping')->doesntStartWith('Jonko'));
$this->assertTrue($this->stringable('Malmö')->doesntStartWith('Malmo'));
}

public function testEndsWith()
{
$this->assertTrue($this->stringable('jason')->endsWith('on'));
Expand Down Expand Up @@ -626,6 +706,34 @@ public function testEndsWith()
$this->assertFalse($this->stringable('Malmö')->endsWith('mo'));
}

public function testDoesntEndWith()
{
$this->assertFalse($this->stringable('jason')->doesntEndWith('on'));
$this->assertFalse($this->stringable('jason')->doesntEndWith('jason'));
$this->assertFalse($this->stringable('jason')->doesntEndWith(['on']));
$this->assertFalse($this->stringable('jason')->doesntEndWith(['no', 'on']));
$this->assertFalse($this->stringable('jason')->doesntEndWith(collect(['no', 'on'])));
$this->assertTrue($this->stringable('jason')->doesntEndWith('no'));
$this->assertTrue($this->stringable('jason')->doesntEndWith(['no']));
$this->assertTrue($this->stringable('jason')->doesntEndWith(''));
$this->assertTrue($this->stringable('jason')->doesntEndWith([null]));
$this->assertTrue($this->stringable('jason')->doesntEndWith(null));
$this->assertTrue($this->stringable('jason')->doesntEndWith('N'));
$this->assertTrue($this->stringable('7')->doesntEndWith(' 7'));
$this->assertFalse($this->stringable('a7')->doesntEndWith('7'));
$this->assertFalse($this->stringable('a7')->doesntEndWith(7));
$this->assertFalse($this->stringable('a7.12')->doesntEndWith(7.12));
$this->assertTrue($this->stringable('a7.12')->doesntEndWith(7.13));
$this->assertFalse($this->stringable(0.27)->doesntEndWith('7'));
$this->assertFalse($this->stringable(0.27)->doesntEndWith('0.27'));
$this->assertTrue($this->stringable(0.27)->doesntEndWith('8'));
// Test for multibyte string support
$this->assertFalse($this->stringable('Jönköping')->doesntEndWith('öping'));
$this->assertFalse($this->stringable('Malmö')->doesntEndWith('mö'));
$this->assertTrue($this->stringable('Jönköping')->doesntEndWith('oping'));
$this->assertTrue($this->stringable('Malmö')->doesntEndWith('mo'));
}

public function testExcerpt()
{
$this->assertSame('...is a beautiful morn...', (string) $this->stringable('This is a beautiful morning')->excerpt('beautiful', ['radius' => 5]));
Expand Down
0