1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-12 08:14:06 +02:00

Add removeLeft() and removeRight()

This commit is contained in:
Daniel St. Jules
2013-07-27 17:57:43 -04:00
parent 6d927fbf5d
commit a93955162e
6 changed files with 162 additions and 6 deletions

View File

@@ -44,8 +44,12 @@ Note: The methods listed below are subject to change until we reach a 1.0.0 rele
* [at](#at)
* [first](#first)
* [last](#last)
* [ensureLeft](#ensureLeft)
* [ensureRight](#ensureRight)
* [ensureLeft](#ensureleft)
* [ensureRight](#ensureright)
* [chompLeft](#chompleft)
* [chompRight](#chompright)
* [removeLeft](#removeleft)
* [removeRight](#removeright)
* [Tests](#tests)
* [License](#license)
@@ -674,6 +678,32 @@ S::create('foobar')->ensureRight('.com');
S::ensureRight('foobar', '.com'); // 'foobar.com'
```
##### removeLeft
$stringy->removeLeft(string $substring)
S::removeLeft(string $str, string $substring [, string $encoding ])
Removes the prefix $substring if present.
```php
S::create('fòô bàř', 'UTF-8')->removeLeft('fòô ');
S::removeLeft('fòô bàř', 'fòô ', 'UTF-8'); // 'bàř'
```
##### removeRight
$stringy->removeRight(string $substring)
S::removeRight(string $str, string $substring [, string $encoding ])
Removes the suffix $substring if present.
```php
S::create('fòô bàř', 'UTF-8')->removeRight(' bàř');
S::removeRight('fòô bàř', ' bàř', 'UTF-8'); // 'fòô'
```
## TODO
**count** => substr_count
@@ -698,10 +728,6 @@ S::ensureRight('foobar', '.com'); // 'foobar.com'
**isBlank**
**chompLeft**
**chompRight**
## Tests
[![Build Status](https://travis-ci.org/danielstjules/Stringy.png)](https://travis-ci.org/danielstjules/Stringy)

View File

@@ -550,4 +550,30 @@ class StaticStringy
{
return Stringy::create($str, $encoding)->ensureRight($substring)->str;
}
/**
* Removes the prefix $substring if present.
*
* @param string $str String from which to remove the prefix
* @param string $substring The prefix to remove
* @param string $encoding The character encoding
* @return string The string without the prefix $substring
*/
public static function removeLeft($str, $substring, $encoding = null)
{
return Stringy::create($str, $encoding)->removeLeft($substring)->str;
}
/**
* Removes the suffix $substring if present.
*
* @param string $str String from which to remove the suffix
* @param string $substring The suffix to remove
* @param string $encoding The character encoding
* @return string The string without the suffix $substring
*/
public static function removeRight($str, $substring, $encoding = null)
{
return Stringy::create($str, $encoding)->removeRight($substring)->str;
}
}

View File

@@ -852,4 +852,36 @@ class Stringy
return $this;
}
/**
* Removes the prefix $substring if present.
*
* @param string $substring The prefix to remove
* @return Stringy Object having a $str without the prefix $substring
*/
public function removeLeft($substring)
{
if ($this->startsWith($substring)) {
$substringLength = mb_strlen($substring, $this->encoding);
$this->substr($substringLength);
}
return $this;
}
/**
* Removes the suffix $substring if present.
*
* @param string $substring The suffix to remove
* @return Stringy Object having a $str without the suffix $substring
*/
public function removeRight($substring)
{
if ($this->endsWith($substring)) {
$substringLength = mb_strlen($substring, $this->encoding);
$this->substr(0, $this->length() - $substringLength);
}
return $this;
}
}

View File

@@ -669,6 +669,42 @@ class CommonTest extends PHPUnit_Framework_TestCase
return $testData;
}
public function stringsForRemoveLeft()
{
$testData = array(
array('foo bar', 'foo bar', ''),
array('oo bar', 'foo bar', 'f'),
array('bar', 'foo bar', 'foo '),
array('foo bar', 'foo bar', 'oo'),
array('foo bar', 'foo bar', 'oo bar'),
array('fòô bàř', 'fòô bàř', '', 'UTF-8'),
array('òô bàř', 'fòô bàř', 'f', 'UTF-8'),
array('bàř', 'fòô bàř', 'fòô ', 'UTF-8'),
array('fòô bàř', 'fòô bàř', 'òô', 'UTF-8'),
array('fòô bàř', 'fòô bàř', 'òô bàř', 'UTF-8')
);
return $testData;
}
public function stringsForRemoveRight()
{
$testData = array(
array('foo bar', 'foo bar', ''),
array('foo ba', 'foo bar', 'r'),
array('foo', 'foo bar', ' bar'),
array('foo bar', 'foo bar', 'ba'),
array('foo bar', 'foo bar', 'foo ba'),
array('fòô bàř', 'fòô bàř', '', 'UTF-8'),
array('fòô bà', 'fòô bàř', 'ř', 'UTF-8'),
array('fòô', 'fòô bàř', ' bàř', 'UTF-8'),
array('fòô bàř', 'fòô bàř', 'bà', 'UTF-8'),
array('fòô bàř', 'fòô bàř', 'fòô bà', 'UTF-8')
);
return $testData;
}
// A test is required so as not to throw an error
// This is a lot cleaner than using PHPUnit's mocks to spy
public function test() {

View File

@@ -372,4 +372,22 @@ class StaticStringyTestCase extends CommonTest
$result = S::ensureRight($str, $substring, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForRemoveLeft
*/
public function testRemoveLeft($expected, $str, $substring, $encoding = null)
{
$result = S::removeLeft($str, $substring, $encoding);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForRemoveRight
*/
public function testRemoveRight($expected, $str, $substring, $encoding = null)
{
$result = S::removeRight($str, $substring, $encoding);
$this->assertEquals($expected, $result);
}
}

View File

@@ -354,4 +354,22 @@ class StringyTestCase extends CommonTest
$result = S::create($str, $encoding)->ensureRight($substring);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForRemoveLeft
*/
public function testRemoveLeft($expected, $str, $substring, $encoding = null)
{
$result = S::create($str, $encoding)->removeLeft($substring);
$this->assertEquals($expected, $result);
}
/**
* @dataProvider stringsForRemoveRight
*/
public function testRemoveRight($expected, $str, $substring, $encoding = null)
{
$result = S::create($str, $encoding)->removeRight($substring);
$this->assertEquals($expected, $result);
}
}