Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge 39832bb into f14d991
Browse files Browse the repository at this point in the history
  • Loading branch information
edigu committed Sep 26, 2017
2 parents f14d991 + 39832bb commit 3b2f053
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/Word/CamelCaseToSeparator.php
Expand Up @@ -26,10 +26,18 @@ public function filter($value)
}

if (StringUtils::hasPcreUnicodeSupport()) {
$pattern = ['#(?<=(?:\p{Lu}))(\p{Lu}\p{Ll})#', '#(?<=(?:\p{Ll}|\p{Nd}))(\p{Lu})#'];
$replacement = [$this->separator . '\1', $this->separator . '\1'];
/**
* First: Match right after a lowercase letter or digit following a capital letter or
* before a capital letter with a lowercese letter on it's right.
*
* Second: Match right after a lowercase letter following zero or more digits
* or a capital letter
*/
$pattern = ['#(?<=\p{Ll}|\p{Nd})(?=\p{Lu})|(?<=\p{Lu})(?=\p{Lu}\p{Ll})#', '#(?<=(?:\p{Ll}))(\p{Lu}|(\p{Nd}+))#'];
$replacement = [ $this->separator . '\1', $this->separator . '\1',
];
} else {
$pattern = ['#(?<=(?:[A-Z]))([A-Z]+)([A-Z][a-z])#', '#(?<=(?:[a-z0-9]))([A-Z])#'];
$pattern = [ '#(?<=[a-z0-9])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])#', '#(?<=(?:[a-z]))([A-Z]|([0-9]+))#' ];
$replacement = ['\1' . $this->separator . '\2', $this->separator . '\1'];
}

Expand Down
27 changes: 27 additions & 0 deletions test/Word/CamelCaseToDashTest.php
Expand Up @@ -23,4 +23,31 @@ public function testFilterSeparatesCamelCasedWordsWithDashes()
$this->assertNotEquals($string, $filtered);
$this->assertEquals('Camel-Cased-Words', $filtered);
}

/**
* @dataProvider camelizedStrings
*/
public function testFilterSeparatesCamelCasedWordsContainingNumbersWithDashes($camel, $dashed)
{
$filter = new CamelCaseToDashFilter();
$filtered = $filter($camel);
$this->assertNotEquals($camel, $filtered);
$this->assertEquals($dashed, $filtered);
}

/**
* Provides CamelizedStrings to test
*
* @return array
*/
public function camelizedStrings()
{
return [
[ 'CamelCasedWith2016Numbers', 'Camel-Cased-With-2016-Numbers' ],
[ '10NumbersAsPrefix', '10-Numbers-As-Prefix' ],
[ 'NumberSuffix42', 'Number-Suffix-42' ],
[ 'lower50Upper', 'lower-50-Upper' ],
[ 'dashed-2016Bar', 'dashed-2016-Bar'],
];
}
}
4 changes: 2 additions & 2 deletions test/Word/CamelCaseToUnderscoreTest.php
Expand Up @@ -38,13 +38,13 @@ public function testFilterSeperatingNumbersToUnterscore()
$filtered = $filter($string);

$this->assertNotEquals($string, $filtered);
$this->assertEquals('Pa2_Title', $filtered);
$this->assertEquals('Pa_2_Title', $filtered);

$string = 'Pa2aTitle';
$filter = new CamelCaseToUnderscoreFilter();
$filtered = $filter($string);

$this->assertNotEquals($string, $filtered);
$this->assertEquals('Pa2a_Title', $filtered);
$this->assertEquals('Pa_2a_Title', $filtered);
}
}

0 comments on commit 3b2f053

Please sign in to comment.