Skip to content
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

Make StringFormatter trim value #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ValueFormatters/StringFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function format( $dataValue ) {
throw new InvalidArgumentException( 'DataValue is not a StringValue.' );
}

return $dataValue->getValue();
return trim( $dataValue->getValue() );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: Can't be null, no check needed.

}

}
17 changes: 10 additions & 7 deletions tests/ValueFormatters/StringFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ class StringFormatterTest extends ValueFormatterTestBase {
*/
public function validProvider() {
$strings = array(
'ice cream',
'cake',
'',
' a ',
' ',
'ice cream' => 'ice cream',
'cake' => 'cake',
'' => '',
' a ' => 'a',
' a' => 'a',
'a ' => 'a',
' ' => '',
' ' => '',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old JavaScript code was trimming a lot more space characters. Please add some more tests:
'\uFEFF\xA0a\uFEFF\xA0' => 'a',
'\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000a\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000' => 'a',
'\t\n\v\f\ra\t\n\v\f\r' => 'a',
'a\u200E' => 'a', //not a space but a very common copy & paste error in MediaWiki
null => null,

We do have a StringNormalizer that does that. I'm not sure if it should used here or if it's more sane to stick with the simple trim().

);

$argLists = array();

foreach ( $strings as $string ) {
$argLists[] = array( new StringValue( $string ), $string );
foreach ( $strings as $value => $expected ) {
$argLists[] = array( new StringValue( $value ), $expected );
}

return $argLists;
Expand Down